Ahmet Akkök
Ahmet Akkök

Reputation: 486

UIApplication.sharedApplication() is unavailable

I just upgraded from XCode 6.4 to Xcode 7 GM and get started to change the code to be compliant with Swift 2. I could not come over the following errors.

The project is a keyboard extension and the snippet is from the containing app.

    let s = "https://itunes.apple.com/"
    UIApplication.sharedApplication().openURL(NSURL(string : s)!)

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

Error 2: 'openURL' is unavailable.

This might be something to do with Xcode and AppDelegate, I might have screwed up my project.

Upvotes: 16

Views: 23700

Answers (6)

Ahmet Akkök
Ahmet Akkök

Reputation: 486

The classes in containing app somehow went in to the compile resources list in the extension Build Phases. I deleted them, it is ok now.

I did not do that. Obviously upgrade to Xcode 7 GM process somehow did it.

Upvotes: 3

Manish Mahajan
Manish Mahajan

Reputation: 2092

Hi I solved this problem.

Set to Allow API Extension to No to particular framework who gives this e error like IQKeyboardManager

Consider you facing this issue for IQKeyboardManager and you installed this framework using Pod. Then your project click on Pod -> CLick on IqkeyboardManager.

And set Allow API Extension to No

enter image description here

Upvotes: 2

Stephen Chen
Stephen Chen

Reputation: 3057

While I was making a framework. I faced this problem. Manually set it to NO solved my problem.

enter image description here

Upvotes: 29

tolbard
tolbard

Reputation: 1273

Swift 3.0

In my case I had this message inside a Widget (that's an extension), the code was a copy/paste from my app. And like Tom Roggero said I had to replace

UIApplication.shared.open(NSURL(string:"https://

by

self.extensionContext?.open(NSURL(string:"https://

Upvotes: 6

Senseful
Senseful

Reputation: 91911

Your target may have the "Allow app extension API only" option checked.

Check your target's general settings to see if that option is enabled in Deployment Info > App Extensions.

enter image description here

Upvotes: 10

Scott Austin
Scott Austin

Reputation: 356

You can't access every API from an extension. From Apple's Extension dev guide: Some APIs Are Unavailable to App Extensions

Because of its focused role in the system, an app extension is ineligible to participate in certain activities. An app extension cannot:

Access a sharedApplication object, and so cannot use any of the methods on that object

etc, etc.

Upvotes: 22

Related Questions