Making App share link with data using Branch in iOS
Posted By : Aditya Kumar Sharma | 29-Mar-2017
To integrate Branch into project you have to sign up for your app at https://dashboard.branch.io. where App information has to be entered like App name, Bundle Identifier.
Now to integrate Branch in project add following line in Podfile:
pod 'Branch'
and run pod install from command
<key>branch_key</key>
<dict>
<key>live</key>
<string>key_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</string>
<key>test</key>
<string>key_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</string>
</dict>
<key>branch_app_domain</key>
<string>myapp.app.link</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string></string>
</array>
</dict>
</array>
Next we have to Enable Associated Domains in project’s Capabilities tab and add following Domains section:
applinks:myapp.app.link applinks:myapp-alternate.app.link
Now we have to following code in App Delegate as Branch session needs to run every single time whenever app opens, to see whether user came from a link and if so, then callback method returns any deep link parameters that are attached to it.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { let branch: Branch = Branch.getInstance() branch?.initSession(launchOptions: launchOptions, automaticallyDisplayDeepLinkController: true, deepLinkHandler: { params, error in if error == nil { // params are the deep linked params associated with the link that the user clicked -> was re-directed to this app // params will be empty if no data found // ... insert custom logic here ... print("params: %@", params.description) } }) } // Respond to URI scheme links func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { // pass the url to the handle deep link call Branch.getInstance().handleDeepLink(url); // do other deep link routing for the Facebook SDK, Pinterest SDK, etc return true } // Respond to Universal Links func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool { // pass the url to the handle deep link call Branch.getInstance().continue(userActivity) return true }
If we have want to attach referral id number in the app share link. So that whenever any user installs app with this link, it can be seen on dashboard.
Now for creating app share link with referral id we need to go on the sharing view controller.
1. Import branch in the view controller.
import Branch
2. Now you have to set the properties of the BranchLink
func setViewingMonster(_ monster: BranchUniversalObject) { self.branchUniversalObject = monster let linkProperties = BranchLinkProperties() linkProperties.feature = "monster_sharing" linkProperties.channel = "twitter" monster.title = “MyApp” monster.contentDescription = “Install MyApp and become happy” monster.imageUrl = “Your_App_Icon_URL” monster.addMetadataKey("referralId", value: “44444”) self.branchUniversalObject.getShortUrl(with: linkProperties, andCallback: {(_ url: String, _ error: Error?) -> Void in if error == nil { print("new monster url created: \(url)") } }) }
3. In this the meta data key is the parameters which can be attached with share link with a key and can be fetched when app opens with this key only. We have to initialise it in viewDidLoad().And at share button action call this method:
func shareSheet() { self.branchUniversalObject.showShareSheet(withShareText: “Install MyApp and become happy”, completion: nil) }
4. It was the task of making app share link with our data. Now its time to fetch the data which was sent in the app share link. For this you have to fetch parameter with the same key as they were attached while making link. So go to didFinishLaunchingWithOptions: and in branch.initSession replace by this
let branch: Branch = Branch.getInstance() branch.initSession(launchOptions: launchOptions, andRegisterDeepLinkHandler: {params, error in if error == nil { // params are the deep linked params associated with the link that the user clicked -> was re-directed to this app // params will be empty if no data found // ... insert custom logic here ... print("params:********** ", params["referralId"] as Any) /*If it was shared link it, referralId will be present.*/ let alert = UIAlertController(title: "Alert", message: String(describing: params), preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.default, handler: nil)) self.window?.rootViewController?.present(alert, animated: true, completion: nil) } })
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Aditya Kumar Sharma
Aditya is a bright iOS developer, have knowledge of objective C, swift, swift 3, JSON, Core data and iPhone development. Apart from that he loves to travel and explore new things.