![](/img/trans.png)
[英]NSNotificationCenter AppDelegate post notification problems
[英]Appdelegate problems
我在通过Safari通过链接打开应用程序时遇到问题(以后将来自我自己的应用程序)如果该应用程序已打开,则没有问题。 如果应用未打开,则通过URL打开时会崩溃。 我已经学会了,那是因为我调用的appdelegate 2种不同的方法- didFinishLaunchingWithOptions
和open url
打开应用程序时,通过打开的URL打开我没有问题,但是如果我通过关闭的应用程序中的URL打开,则应用程序崩溃。 我了解到, didFinishLauchingWithOptions
url参数可能为null,但是如何对其进行测试。 当我同时在模拟器和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
}
}
您可以检查url的主机不为null,以及是否让url如下所示
if url.host == nil
{
return true;
}
if let sUrl = url
{
urlString = url.absoluteString
}
希望能帮助到你!
更新
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.