问题
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?
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/34034271/swift-sharedapplication-is-unavailable-use-view-controller-based-solutions