Configure deep links in swift?

2020-07-13 08:27发布

问题:

So I have an app written in swift that I would like to use deep links for. A user would click on one that in the route of the URL had different pieces of information such as the id of the post or the number of likes it has or something like that. I understand that you put this in the AppDelegate:

func application(application: UIApplication,openURL url: NSURL,sourceApplication sourceApplication: String?,annotation annotation: AnyObject?) -> Bool {
    println(url.host as String!)
    return true
}

And that prints everything after appname://. But how can I get url.host to my view controller to be parsed into the information I need. If the declaration of URL was outside of that function then I could use this:

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let InfoFromDeepLink = appDelegate.url.host

but since it's inside of that bulky Objective-C function, I'm helpless. How do you do this? I'm completely baffled. I can't even set the function to return url.host because it doesn't allow that. That's what I would do in Javascript. If you also know Javascript maybe put it in terms that a web developer would understand because I'm pretty new to this. This has to be so simple for a Swift developer right? I feel so stupid.

回答1:

Apple no longer supports Deep Links. It is now called Universal Links and works a bit differently.

Source

Now that Apple no longer supports URI schemes for deep linking, developers must implement Universal Links in order to deep link properly on iOS. If you are already using URI schemes, check out our blog on transitioning to Universal Links.

From: HERE

And HERE is another article on Universal Links and what they are.



回答2:

I would recommend using this cocoapods package called DeepLinkKit HERE IS THE SOURCE CODE

pod "DeepLinkKit"

You can simple route and have an error handling:

// Matches the URL.
router[@"timeline"] = ^{ … }

// Does not match the URL.
router[@"/timeline"] = ^{ … }

Here is a good tutorial that shows you in depth how it works.