Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LSApplicationWorkspace in Swift

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?

like image 630
blee908 Avatar asked Dec 25 '22 08:12

blee908


2 Answers

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.

like image 51
Mehul Thakkar Avatar answered Jan 08 '23 07:01

Mehul Thakkar


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.

like image 32
mz2 Avatar answered Jan 08 '23 07:01

mz2