简体   繁体   中英

How to create a Firebase Dynamic Link programmatically (iOS)?

I read through all the available documentation and tried out the Firebase sample code, but nothing seems to work.

I have set up a Dynamic Link through the Console, and it works just fine.

Can someone explain me how to create the most basic dynamic link so that a user can eg can send links that lead directly to his profile? Which are the required minimum components?

(Would be great if someone can post a basic code in Objective-C)

I assume that you want to send some key value pairs, a dictionary in the body of your URL too.
Here's how to compose it Convert dictionary to query string in swift? .
So first step is to create that URL.
Next, you can compose your firebase dynamic link manually https://firebase.google.com/docs/dynamic-links/create-manually
Also you can find a small tutorial at https://firebase.googleblog.com/2017/06/making-dynamic-links-easier.html

 @IBAction func createGroupAction(_ sender: Any) {

    let dynamicLinkDomain = "a5481.app.goo.gl"  // <- don't add https scheme to this string
    let appBundleID = "com.vividapartments.myStr.com"

    let params:[String : String] = [
        "ibi": appBundleID,
        "isi": "1222420082",
        "efr":"1",
        "groupID":"-LDSfWKKbWjyrXiFljT3",
        "chatID":"-LDSfWKMbm9mSE2a6EIe"
    ]

    // general link params
    let urlParams = params.flatMap ({ (key,value) -> String in
        return "\(key)=\(value)"
    }).joined(separator: "&")


    guard let deepLink = URL(string:"https://www.mystr.com/groups?\(urlParams)") else {return}

    print("deepLink is \(deepLink)")
    //prints:
    //deepLink is https://www.mystr.com/groups?isi=1222420082&chatID=-LDSfWKMbm9mSE2a6EIe&efr=1&ibi=com.vividapartments.myStr.com&groupID=-LDSfWKKbWjyrXiFljT3

    let components = DynamicLinkComponents(link: deepLink, domain: dynamicLinkDomain)

    let iOSParams = DynamicLinkIOSParameters(bundleID: appBundleID)
    iOSParams.minimumAppVersion = "1.5"
    components.iOSParameters = iOSParams

    //1. Build the dynamic long link
    let longlLink = components.url
    print("The long link is \(longlLink!)")
    //prints:
     //The long link is https://a5481.app.goo.gl/?link=https%3A%2F%2Fwww%2Emystr%2Ecom%2Fgroups%3Fisi%3D1222420082%26chatID%3D%2DLDSfWKMbm9mSE2a6EIe%26efr%3D1%26ibi%3Dcom%2Evividapartments%2EmyStr%2Ecom%26groupID%3D%2DLDSfWKKbWjyrXiFljT3&ibi=com%2Evividapartments%2EmyStr%2Ecom&imv=1%2E5

    //Set the length of a short Dynamic Link
    let options = DynamicLinkComponentsOptions()
    options.pathLength = .unguessable
    components.options = options

    //2. Or create a shortened dynamic link
    components.shorten { (shortURL, warnings, error) in
        if let error = error {
            print("error is \(error.localizedDescription)")
            return
        }

        // TODO: Handle shortURL.
        print("shortURL is \(String(describing: shortURL))")
    }



}

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