Using LSApplicationWorkspace in Swift

我怕爱的太早我们不能终老 提交于 2019-12-18 05:08:09

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/35807764/using-lsapplicationworkspace-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!