【发布时间】:2017-07-06 13:28:37
【问题描述】:
我在通过 Safari 从链接打开我的应用程序时遇到问题(稍后它将来自我自己的应用程序)
如果应用程序已打开,则没有问题。如果应用程序未打开,则在通过 URL 打开时会崩溃。我了解到这是因为我在 Appdelegate 中调用了 2 种不同的方法 - didFinishLaunchingWithOptions 和 open url
当应用程序打开时,我通过打开的 url 打开没有问题,但是如果我在关闭的应用程序上通过 url 打开应用程序会崩溃。我了解到didFinishLauchingWithOptions 中的 url 参数可能为空,但是您如何测试它。当我在模拟器上关闭应用程序并且 iPad XCode 终止连接时
编辑 - 现在可以在 Sachin Vas 发表评论后进行调试。 在 XCode 调试中得到这个 - 但无法扩展有效负载
我的函数是这样的
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
}
}
【问题讨论】: