NSInternalInconsistencyException: 只能有一个 UIApplication 实例
NSInternalInconsistencyException: There can only be one UIApplication instance
描述:
正在尝试使用 UIApplication
class 在我的应用程序中打开 Youtube URL。
let url = URL(string: "https://www.youtube.com/watch?v=smOp5aK-_h0")!
let app = UIApplication()
if app.canOpenURL(url){
//Crash here
app.openURL(url)
}
问题:
为什么当我尝试打开 url 时我的应用程序崩溃?
错误:
* Assertion failure in -[UIApplication init],
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'There can only be one
UIApplication instance.'
编辑:不崩溃但不打开 link:
if UIApplication.shared.canOpenURL(url){
print("Can open shared application url.")
if #available(tvOS 10.0, *) {
print("tvOS 10.0 detected")
UIApplication.shared.open(url){res in
//res is false...
print("Result..." + String(res))
}
} else {
// Fallback on earlier versions
UIApplication.shared.openURL(url)
}
}
错误说的很清楚。您必须使用默认应用程序。 UIApplication.shared.canOpenURL(url)
错误是说:
There can only be one UIApplication instance
意味着应该调用一个实例,当使用 UIApplication
时,它应该是 shared
(单例)实例。如UIApplication's Documentation所述:
The UIApplication class provides a centralized point of control and
coordination for apps running in iOS. Every app has exactly one
instance of UIApplication (or, very rarely, a subclass of
UIApplication). When an app is launched, the system calls the
UIApplicationMain(::::) function; among its other tasks, this
function creates a Singleton UIApplication object. Thereafter you
access the object by calling the shared() class method.
共享():
All UIApplication notifications are posted by the app instance
returned by shared().
那么你需要做的就是把let app = UIApplication()
改成let app = UIApplication.shared
:
let url = URL(string: "https://www.youtube.com/watch?v=smOp5aK-_h0")!
let app = UIApplication.shared
if app.canOpenURL(url) {
app.open(url, options: [:], completionHandler: nil)
}
或:
// for being safe, I suggest also to do "optional-binding" for the url:
if let url = URL(string: "https://www.youtube.com/watch?v=smOp5aK-_h0") {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}
描述:
正在尝试使用 UIApplication
class 在我的应用程序中打开 Youtube URL。
let url = URL(string: "https://www.youtube.com/watch?v=smOp5aK-_h0")!
let app = UIApplication()
if app.canOpenURL(url){
//Crash here
app.openURL(url)
}
问题:
为什么当我尝试打开 url 时我的应用程序崩溃?
错误:
* Assertion failure in -[UIApplication init], * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'There can only be one UIApplication instance.'
编辑:不崩溃但不打开 link:
if UIApplication.shared.canOpenURL(url){
print("Can open shared application url.")
if #available(tvOS 10.0, *) {
print("tvOS 10.0 detected")
UIApplication.shared.open(url){res in
//res is false...
print("Result..." + String(res))
}
} else {
// Fallback on earlier versions
UIApplication.shared.openURL(url)
}
}
错误说的很清楚。您必须使用默认应用程序。 UIApplication.shared.canOpenURL(url)
错误是说:
There can only be one UIApplication instance
意味着应该调用一个实例,当使用 UIApplication
时,它应该是 shared
(单例)实例。如UIApplication's Documentation所述:
The UIApplication class provides a centralized point of control and coordination for apps running in iOS. Every app has exactly one instance of UIApplication (or, very rarely, a subclass of UIApplication). When an app is launched, the system calls the UIApplicationMain(::::) function; among its other tasks, this function creates a Singleton UIApplication object. Thereafter you access the object by calling the shared() class method.
共享():
All UIApplication notifications are posted by the app instance returned by shared().
那么你需要做的就是把let app = UIApplication()
改成let app = UIApplication.shared
:
let url = URL(string: "https://www.youtube.com/watch?v=smOp5aK-_h0")!
let app = UIApplication.shared
if app.canOpenURL(url) {
app.open(url, options: [:], completionHandler: nil)
}
或:
// for being safe, I suggest also to do "optional-binding" for the url:
if let url = URL(string: "https://www.youtube.com/watch?v=smOp5aK-_h0") {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
}