Description:
Trying to open a Youtube URL in my application using the UIApplication
class.
let url = URL(string: "https://www.youtube.com/watch?v=smOp5aK-_h0")!
let app = UIApplication()
if app.canOpenURL(url){
//Crash here
app.openURL(url)
}
Question:
Why does my application crash when I attempt to open the url?
Error:
* Assertion failure in -[UIApplication init], * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'There can only be one UIApplication instance.'
Edit: Does not crash but doesn't open the 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)
}
}
The error is saying:
Means that there is one instance should be called, when working with
UIApplication
it should beshared
(singleton) instance. As mentioned in UIApplication's Documentation:shared():
So what do you need to is to change
let app = UIApplication()
tolet app = UIApplication.shared
:Or: