问题
This is how I prepare and share branch link to the users:
let branch = BranchUniversalObject(canonicalIdentifier: UUID().uuidString)
branch.title = self.territory.name
branch.metadata = self.territory.dictionary
let properties = BranchLinkProperties()
properties.controlParams = self.territory.dictionary
branch.getShortUrl(with: properties) { string, error in
if let string = string, let url = URL(string: string) {
let controller = UIActivityViewController.controller(activityItem: url, from: sender, traitCollection: self.traitCollection)
alert.dismiss(animated: true)
self.present(controller, animated: true)
}
}
Then I would like to access parameters I passed to that link, but I do not know how... Any help?
回答1:
When you integrate Branch into your Xcode project (as described in the Branch docs here: https://dev.branch.io/getting-started/sdk-integration-guide/guide/ios/) you add code to initialize the Branch SDK in the AppDelegate.swift file's didFinishLaunchingWithOptions (here is an example: https://github.com/BranchMetrics/ios-branch-deep-linking/blob/master/Branch-TestBed-Swift/TestBed-Swift/AppDelegate.swift#L43-L74).
A "params" dictionary will be available within the deepLinkHandler callback of this initSession call. This params dictionary contains the key value pairs associated with the Branch link that was used to open the app, assuming of course that the app was opened via a Branch link click. You can determine if the app opened because of a Branch link click by checking the params dictionary's +clicked_branch_link parameter.
This Branch callback that is registered in didFinishLaunchingWithOptions will also be called whenever the app becomes active and the appropriate Branch calls are triggered in the AppDelegate's openURL and continueUserActivity functions.
In addition to returning the params dictionary in the init callback, the Branch SDK will save the params in the device's local storage. These saved params can be accessed for the life of the session by calling getLatestReferringParams (described here: https://github.com/BranchMetrics/ios-branch-deep-linking#retrieve-session-install-or-open-parameters).
For code examples, check the TestBed-Swift app that is included in the SDK:
- https://github.com/BranchMetrics/ios-branch-deep-linking/tree/master/Branch-TestBed-Swift
- This sample app is also available on the App Store: https://testbed-swift.app.link/appstore
If you are checking +clicked_branch_link in the init callback but finding that it is 0 or "false", there are a number of possible causes:
- Usually when this happens it is because the Branch key used to create the link is different from the Branch key that the app has been configured to use (i.e. a Live vs. Test key mismatch)
- After being opened from a Branch link, the app may trigger the display of an alert or other activity that results in the session closing. When the app becomes active again, the new session will not have been triggered by a Branch link click and the params dictionary will therefore be empty.
- Sometimes partners test with URLs that are not actually Branch links. The different ways to create valid links are described in the docs here: https://dev.branch.io/getting-started/creating-links/overview/
- If a link is not properly triggered it will not function correctly. Branch links function as Universal Links on iOS and are therefore subject to Apple's strictures on link behavior. Most importantly, they must be tapped on by the end user. Universal Links cannot be triggered by pasting them into browser address bars; by redirecting to them from another link; or by wrapping them in other links.
- The app where the link is being tapped may not support Universal Linking. Google and Facebook Ads, and apps that use captive webviews, often interfere with Universal Linking. Be sure to test links by first pasting them into Notes on a physical device and then by long-pressing them and selecting the "Open in App" option from the pop-up menu.
来源:https://stackoverflow.com/questions/42955474/how-do-i-access-metadata-when-branch-link-was-clicked-and-opened-my-app