简体   繁体   中英

Appdelegate problems

I'm having problems opening my app from a link through Safari (later it will be from my own app) If the app is open, there are no problems. If the app is not open, it crashes, when opening through a URL. I've learned that It is because I call 2 different methods in Appdelegate - didFinishLaunchingWithOptions and open url

I have no problems opening through open url when the app is open, but the app crashes if I am opening through url on closed app. I've learned that my url parameter in didFinishLauchingWithOptions is probably null, but how do you test it. When I close the app on both simulator and iPad XCode terminates the connection

EDIT - Now able to debug, after Sachin Vas' comment. Getting this in XCode debug - but can't expand the payload 在此处输入图片说明

My functions look like this

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
    }

}

You can check url's host to be not null and if let for url like below

if url.host == nil
{
  return true;
}

if let sUrl = url
 {
   urlString = url.absoluteString
 }

Hope it helps!

UPDATED

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM