Using LSApplicationWorkspace in Swift

2019-01-25 22:55发布

I've added MobileCoreServices.framework to my Xcode project, and have the statement import MobileCoreServices in top of my file.

I have the line of code

let test = LSApplicationWorkspace.defaultWorkSpace()

And xcode said Unresolved Identifier LSApplicationWorkspace

I tried cleaning and rebuilding the project. Any ideas?

2条回答
做自己的国王
2楼-- · 2019-01-25 23:28

Point 1: LSApplicationWorkspace is private api, so if you use this and will upload your app to app store, it will get rejected.

Point 2: If you are having any in-house app and still want to use this in your app, then below is the way to use it.

  1. Add MobileCoreServices Framework in your bundle
  2. Create LSApplicationWorkspace.h file with the code exactly same as the code provided at here "https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/MobileCoreServices.framework/LSApplicationWorkspace.h".
  3. Now add this LSApplicationWorkspace.h file into your bundle
  4. Create bridging header for your swift app
  5. Add #import "LSApplicationWorkspace.h" in your bridging header
  6. Now add import MobileCoreServices in your current file and add your code let test = LSApplicationWorkspace.defaultWorkSpace(), it will work fine.

NOTE: For using any private header, you must have to include its .h file into your code. You can find any private headers by searching "runtime headers" in google. You will get all runtime headers. And to include that header in your swift code, you need to go via bridging-header.

查看更多
地球回转人心会变
3楼-- · 2019-01-25 23:49

The Swift compiler is telling you it has not idea about a class called LSApplicationWorkspace. This is because it is a private class not described in any headers available to you as a 3rd party developer. Should you be caught using LSApplicationWorkspace APIs during App Store review, your submission will be rejected. Given this class contains APIs that change between OS versions (by virtue of being undocumented and private), and APIs which are for good privacy reasons not available to 3rd party devs, using it is almost certainly a really bad idea, even if you are technically able to do so with either of the the following methods.

  1. Create a private module map file where you import a header you somehow acquired for the private framework that contains LSApplicationWorkspace. This way of calling to LSApplicationWorkspace (or indeed any method involving using the headers for Mobile CoreServices – i.e. any method except for performSelector mentioned below) would almost certainly get your app submission booted from App Store review, because this method of (ultimately) Objective-C method calls would be visible to static analysis methods Apple runs on your code as part of App Store review.

  2. Use NSClassFromString:

let LSApplicationWorkspace_class:AnyObject = NSClassFromString("LSApplicationWorkspace")! as AnyObject
let workspace = LSApplicationWorkspace_class.perform(NSSelectorFromString("defaultWorkspace"))! as AnyObject 

With the above code you now get an instance of LSApplicationWorkspace with which you can perform code with performSelector – should you know what selectors it responds to (… in the particular operating system version you are running on). Again though, doing anything with LSApplicationWorkspace is probably a bad idea.

查看更多
登录 后发表回答