Appdelegate问题

时间:2017-02-17 06:27:12

标签: ios swift xcode

我在通过Safari链接打开我的应用时遇到问题(稍后它将来自我自己的应用) 如果应用程序已打开,则没有问题。如果应用程序未打开,则在通过URL打开时会崩溃。我了解到这是因为我在Appdelegate中调用了两种不同的方法 - didFinishLaunchingWithOptionsopen url

当应用程序打开时我打开打开的网址没有问题,但是如果我在关闭的应用程序上打开网址,应用程序崩溃了。我已经知道didFinishLauchingWithOptions中的url参数可能为null,但是你如何测试呢。当我在模拟器和iPad上关闭应用程序XCode终止连接

编辑 - 现在能够在Sachin Vas发表评论之后进行调试。 在XCode调试中获取此信息 - 但无法扩展有效负载enter image description here

我的功能看起来像这样

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
    }

}

1 个答案:

答案 0 :(得分:2)

您可以检查网址的主机是否为空,如果是,请查看以下网址

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
}