【问题标题】:Appdelegate problemsAppdelegate 问题
【发布时间】:2017-07-06 13:28:37
【问题描述】:

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

}

【问题讨论】:

标签: ios swift xcode


【解决方案1】:

您可以检查 url 的主机是否不为空,如果让 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
}

【讨论】:

  • 好吧,至少我可以看到 url 实际上是 nil,这是我的想法。我很遗憾地说,我实际上认为当我写if let url = launchOptions?[UIApplicationLaunchOptionsKey.url] as? NSURL这意味着我有一个网址。没有url的情况下,为什么还要进入作用域?
  • 感谢 Zalal - 它引导我朝着正确的方向前进,即 if let url = launchOptions[UIApplicationLaunchOptionsKey.url] as? URL - 我使用的是 NSURL
猜你喜欢
  • 2011-06-07
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多