UIApplication.sharedApplication() is unavailable
Asked Answered
G

6

16

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.

Gravelly answered 16/9, 2015 at 13:18 Comment(4)
Is this a normal app you are writing?Velvavelvet
it is a keyboard extension. Bu the code sent is from the containing app of the extension. I have the settings screens for the keyboard in the containing app.Gentianaceous
#46240931Jobless
#46240931 this is workJobless
G
3

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.

Gravelly answered 17/9, 2015 at 18:57 Comment(1)
Did you try self.extensionContext?.open(URL(string: "http://...")!) ?Bolin
T
29

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

enter image description here

Trustful answered 8/2, 2017 at 3:42 Comment(1)
The problem with this answer is that you are telling the compiler it's OK tomato use of a lot of calls that are not safe to use from an extension. A few may work but generally calls that are marked as unavailable from an extension will not work, so just disabling the compiler error will not help you. You need to find a valid way to do what you want from within an extension instead of using calls that are not safe.Acrocarpous
G
22

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.

Geller answered 17/9, 2015 at 15:18 Comment(1)
It is the containing app not the extension. I used to call with no problem in Xcode 6.4.Gentianaceous
R
10

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

Roque answered 25/7, 2016 at 23:25 Comment(0)
E
6

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://
Entrails answered 1/10, 2016 at 19:48 Comment(1)
@MathewS I just change my post because I am made a mistake. I manage to have it work in a widget and not in iMessage. iMessage app are very limited you should be able to call url pointing inside your app. https://mcmap.net/q/589477/-send-url-with-imessage-appEntrails
G
3

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.

Gravelly answered 17/9, 2015 at 18:57 Comment(1)
Did you try self.extensionContext?.open(URL(string: "http://...")!) ?Bolin
P
2

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

Pardner answered 11/4, 2018 at 8:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.