Skip to content

Instantly share code, notes, and snippets.

@aeromusek
Last active December 24, 2016 01:28
Show Gist options
  • Save aeromusek/308d643b9a501aefa233826251f20f62 to your computer and use it in GitHub Desktop.
Save aeromusek/308d643b9a501aefa233826251f20f62 to your computer and use it in GitHub Desktop.
Comparative examples for Branch and Firebase
import UIKit
import Branch
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Set up the SDK
let branch: Branch = Branch.getInstance()
// Get a bunch of extra logging output
branch.setDebug()
// No need to manually configure any transitions, because the Branch SDK can do that automatically
// Of course, you can do it manually too if you need more flexibility
let controller = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "roomDetails")
branch.registerDeepLinkController(controller, forKey: "room_name")
// Start the session and get all the link data. This occurs on every launch, even if a link was not opened
// This is also where the link data from a new install is captured
branch.initSession(launchOptions: launchOptions, automaticallyDisplayDeepLinkController: true) { (params, error) in
if (error == nil) {
// We can look at the contents of the link, and a bunch of other interesting things
dump(params)
}
}
return true
}
// Here, we check for URI scheme links
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
// If we find one, pass it back to the deep link handler for unpacking
Branch.getInstance().handleDeepLink(url);
return true
}
// Also check for Universal Links
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
// If we find one, it goes back to the deep link handler for unpacking too
return Branch.getInstance().continue(userActivity)
}
}
import UIKit
import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let customURLScheme = "branchmaps" // Hmmm...why?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Set up the SDK
FIROptions.default().deepLinkURLScheme = self.customURLScheme
FIRApp.configure()
return true
}
// This extra call to the open url method often causes confusion...but both are necessary!
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
return application(app, open: url, sourceApplication: nil, annotation: [:])
}
// Here, we handle URI scheme links, and (more importantly) the initial launch after a new install
// This can also cause confusion, because the initial launch is not a URI link and yet is still handled here
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
let dynamicLink = FIRDynamicLinks.dynamicLinks()?.dynamicLink(fromCustomSchemeURL: url)
if let dynamicLink = dynamicLink {
// We can see the URL of the link...
dump(dynamicLink.url)
// ...and then pull it apart to use the pieces
let deepLink = URLComponents(url: dynamicLink.url!, resolvingAgainstBaseURL: false)!
let deepLinkQueryString = deepLink.queryItems
// Filtering through the results to see if they contain the parameter we want
if let roomID = deepLinkQueryString!.filter({$0.name == "roomID"}).first?.value {
// Set up the transition
let destination = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "roomDetails") as! ViewController
destination.roomToShow = String(describing: roomID)
self.window?.rootViewController?.present(destination, animated: true, completion: nil)
}
return true
}
return false
}
// Aaaaand now do it all again for Universal Links
@available(iOS 8.0, *)
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
guard let dynamicLinks = FIRDynamicLinks.dynamicLinks() else {
return false
}
let handled = dynamicLinks.handleUniversalLink(userActivity.webpageURL!) { (dynamicLink, error) in
dump(dynamicLink!.url)
let deepLink = URLComponents(url: dynamicLink!.url!, resolvingAgainstBaseURL: false)!
let deepLinkQueryString = deepLink.queryItems
if let roomID = deepLinkQueryString!.filter({$0.name == "roomID"}).first?.value {
let destination = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "roomDetails") as! ViewController
destination.roomToShow = String(describing: roomID)
self.window?.rootViewController?.present(destination, animated: true, completion: nil)
}
}
return handled
}
}
@IBAction func shareButton(_ sender: UIButton) {
// Set up some basic info about the link we are going to create
let linkProperties = BranchLinkProperties()
linkProperties.feature = "sharing"
// Define a container to store link data
let branchUniversalObject = BranchUniversalObject()
if let selectedRoom = (RoomData.allRoomsArray().filter{ $0.roomName == roomToShow }).first {
// Insert all the link data...these are just few of the options
// Many configuration items can be omitted because they are inherited from the main Branch app config
branchUniversalObject.canonicalIdentifier = "room/\(selectedRoom.roomID)" // This one lets us dedup the same piece of content across many links
branchUniversalObject.title = selectedRoom.roomName
branchUniversalObject.addMetadataKey("room_name", value: selectedRoom.roomName) // We can have as many of these as we want
// Use the pre-packaged share sheet method
branchUniversalObject.showShareSheet(with: linkProperties, andShareText: nil, from: self, completion: { (activity, finished) in
print("Generated sharing link for \(selectedRoom.roomName)")
})
}
}
@IBAction func shareButton(_ sender: UIButton) {
if let selectedRoom = (RoomData.allRoomsArray().filter{ $0.roomID == roomToShow }).first {
// We need to specify everything for each link...so let's start with the base URL on the web
// Also need to manually append any custom data params we want passed through
let roomLink = "https://branch.io/room?roomID=\(selectedRoom.roomID)"
// Next, wrap that URL into the Firebase link and add required control params
let firebaseLink = "https://x2z5q.app.goo.gl/?link=\(roomLink.addingPercentEncoding(withAllowedCharacters: .alphanumerics)!)&ibi=io.branch.branchmap"
// Configure and display the stock iOS share sheet
let shareSheet = UIActivityViewController(activityItems: [ firebaseLink ], applicationActivities: nil)
shareSheet.popoverPresentationController?.sourceView = self.view
self.present(_: shareSheet, animated: true, completion: {
print("Generated \(firebaseLink) sharing link for \(selectedRoom.roomID)")
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment