Swift - 'sharedApplication不可用。'相反,使用基于视图控制器的解决方案 [英] Swift - 'sharedApplication is unavailable.' Use view controller based solutions where appropriate instead

查看:576
本文介绍了Swift - 'sharedApplication不可用。'相反,使用基于视图控制器的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下行在我的Swift应用中打开外部URL:

I am opening an external URL in my Swift app using the line:

UIApplication.sharedApplication().openURL(url)

这一点工作正常,直到我通过CocoaPods将Realm添加到我的项目中。那时,它开始给我下面两个编译错误:

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()'不可用:使用基于$ b的视图控制器相应的$ b解决方案。

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

'openURL'不可用

'openURL' is unavailable

这些错误表明API不适用于Application Extensions,但我的代码是在正常的应用程序中,而不是扩展。为什么添加Realm导致编译器认为它在App扩展中?

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?

推荐答案

你不能使用 appApplication 来自应用扩展程序。

You can't use sharedApplication from an app extension.

但请注意,Apple文档说明:

But note that the apple documentation states:

重要

Apple允许任何Today小部件使用
openURL:completionHandler:方法打开包含$ b的小部件$ b app。

Apple allows any Today widget to use the openURL:completionHandler: method to open the widget’s own containing app.

ExtensionOverview。

这篇关于Swift - 'sharedApplication不可用。'相反,使用基于视图控制器的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆