9

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?

bdash
  • 18,110
  • 1
  • 59
  • 91
Ashish Agarwal
  • 14,555
  • 31
  • 86
  • 125
  • Did you use a package manager to install realm like pods? Also does your project compile if you uninstall Realm? and Also please specify what version of Xcode you use – ProblemSlover Dec 02 '15 at 03:23
  • I am using Cocoapods and Xcode 7 – Ashish Agarwal Dec 02 '15 at 03:33
  • UIApplication.sharedApplication() has nothing to do with Realm, right? It seems you are using extension? [UIApplication sharedApplication] is not available to extensions. – Wingzero Dec 02 '15 at 03:56
  • Which version of CocoaPods are you using? – bdash Dec 02 '15 at 06:04
  • I'm using version 0.38.2 – Ashish Agarwal Dec 02 '15 at 08:16
  • 4
    This isn't a duplicate of http://stackoverflow.com/questions/32609776/uiapplication-sharedapplication-is-unavailable. The issue here is that an old version of CocoaPods is being used, and [a bug with it](https://github.com/CocoaPods/CocoaPods/issues/3906) is causing settings from the Realm target to be applied to the user's application target. Updating to CocoaPods v0.39 or newer should fix this. – bdash Dec 08 '15 at 17:33

3 Answers3

4

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.

Karlos
  • 1,653
  • 18
  • 36
1

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.

bdash
  • 18,110
  • 1
  • 59
  • 91
  • This was an issue with a 3.5 year old version of Realm / CocoaPods. It’s incredibly unlikely that whatever problem you’re seeing is related to this issue. I’d suggest you ask a new question and provide sufficient detail for someone else to assist you. – bdash May 12 '19 at 15:38
1

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.

samwize
  • 25,675
  • 15
  • 141
  • 186