[英]Appdelegate problems
I'm having problems opening my app from a link through Safari (later it will be from my own app) If the app is open, there are no problems. 我在通过Safari通过链接打开应用程序时遇到问题(以后将来自我自己的应用程序)如果该应用程序已打开,则没有问题。 If the app is not open, it crashes, when opening through a URL. 如果应用未打开,则通过URL打开时会崩溃。 I've learned that It is because I call 2 different methods in Appdelegate - didFinishLaunchingWithOptions
and open url
我已经学会了,那是因为我调用的appdelegate 2种不同的方法- didFinishLaunchingWithOptions
和open url
I have no problems opening through open url when the app is open, but the app crashes if I am opening through url on closed app. 打开应用程序时,通过打开的URL打开我没有问题,但是如果我通过关闭的应用程序中的URL打开,则应用程序崩溃。 I've learned that my url parameter in didFinishLauchingWithOptions
is probably null, but how do you test it. 我了解到, didFinishLauchingWithOptions
url参数可能为null,但是如何对其进行测试。 When I close the app on both simulator and iPad XCode terminates the connection 当我同时在模拟器和iPad上关闭应用程序时,XCode终止连接
EDIT - Now able to debug, after Sachin Vas' comment. 编辑 -在Sachin Vas评论后,现在可以调试了。 Getting this in XCode debug - but can't expand the payload 在XCode调试中获取此内容-但无法扩展有效负载
My functions look like this 我的功能看起来像这样
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nav = storyBoard.instantiateViewController(withIdentifier: "HomeNavController") as! UINavigationController
let home = storyBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
if let url = launchOptions?[UIApplicationLaunchOptionsKey.url] as? NSURL {
if let itemid = getQueryStringParameter(url: url.absoluteString!, param: "itemid"){
NetworkService().getSpecificExercise(id: itemid) { response, error in
let exercise = response! as VoiceExercise
let vc = storyBoard.instantiateViewController(withIdentifier: "ExerciseViewController") as! ExerciseViewController
vc.exercise = exercise
switch exercise.type {
case "STRENGTH":
vc.exercisetype = .strength
case "RANGE":
vc.exercisetype = .range
case "COMBINED":
vc.exercisetype = .combined
default:
print(exercise.type)
}
nav.pushViewController(vc, animated: false)
self.window?.rootViewController = nav
//self.window?.makeKeyAndVisible()
}
return true
}
}else{
//nav.pushViewController(home, animated: true)
self.window?.rootViewController = home
self.window?.makeKeyAndVisible()
}
nav.pushViewController(home, animated: true)
self.window?.rootViewController = nav
self.window?.makeKeyAndVisible()
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if let itemid = getQueryStringParameter(url: url.absoluteString, param: "itemid"){
NetworkService().getSpecificExercise(id: itemid) { response, error in
let exercise = response! as VoiceExercise
let vc = storyBoard.instantiateViewController(withIdentifier: "ExerciseViewController") as! ExerciseViewController
vc.exercise = exercise
switch exercise.type {
case "STRENGTH":
vc.exercisetype = .strength
case "RANGE":
vc.exercisetype = .range
case "COMBINED":
vc.exercisetype = .combined
default:
print(exercise.type)
}
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()
}
return true
}else{
return false
}
}
You can check url's host to be not null and if let for url like below 您可以检查url的主机不为null,以及是否让url如下所示
if url.host == nil
{
return true;
}
if let sUrl = url
{
urlString = url.absoluteString
}
Hope it helps! 希望能帮助到你!
UPDATED 更新
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let launchOptions = launchOptions {
if #available(iOS 9.0, *) {
if let url = launchOptions[UIApplicationLaunchOptionsKey.url] as? UIApplicationShortcutItem {
print("url: \(url)")
}
}
}
return true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.