Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - 'sharedApplication is unavailable.' Use view controller based solutions where appropriate instead

I am opening an external URL in my Swift app using the line:

UIApplication.sharedApplication().openURL(url)

This was working fine until I added Realm to my project via CocoaPods. At that point, it started giving me the two compile errors below:

'sharedApplication()' is unavailable: Use view controller based solutions where appropriate instead.

'openURL' is unavailable

These errors indicate that the API is unavailable to Application Extensions, but my code is in a normal application, not an extension. Why would adding Realm result in the compiler thinking it's in an App Extension?

like image 888
Ashish Agarwal Avatar asked Dec 02 '15 03:12

Ashish Agarwal


3 Answers

You can't use sharedApplication from an app extension.

But note that the apple documentation states:

IMPORTANT

Apple allows any Today widget to use the openURL:completionHandler: method to open the widget’s own containing app.

ExtensionOverview.

like image 118
Karlos Avatar answered Sep 18 '22 13:09

Karlos


This is an issue that can occur when adding Realm to a project that is using a version of CocoaPods prior to v0.39. To fix it, update to the latest version of CocoaPods using:

sudo gem install cocoapods

This problem is due to CocoaPods issue #3906, which results in portions of Realm's configuration settings being applied to your application's targets. One of the settings that Realm enables is "Allow app extension API only", which is why you end up seeing this particular error.

like image 23
bdash Avatar answered Sep 20 '22 13:09

bdash


I still have the same issue, even with CP v1.8.3.

A quick fix is to select the offending pod > Build Settings > Require Only App-Extension-Safe API > Set to No.

like image 31
samwize Avatar answered Sep 20 '22 13:09

samwize