![](/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.