UIApplication.sharedApplication() is unavailable

2019-01-20 08:14发布

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.

6条回答
在下西门庆
2楼-- · 2019-01-20 08:38

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.

查看更多
SAY GOODBYE
3楼-- · 2019-01-20 08:41

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://
查看更多
劳资没心,怎么记你
4楼-- · 2019-01-20 08:47

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

查看更多
放荡不羁爱自由
5楼-- · 2019-01-20 08:51

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

enter image description here

查看更多
我命由我不由天
6楼-- · 2019-01-20 08:54

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

查看更多
Anthone
7楼-- · 2019-01-20 08:55

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.

查看更多
登录 后发表回答