从 Branch.io 响应中获取值并设置操作 (Swift)

Get Value and Set Action from Branch.io Response (Swift)

我最近想让我的应用程序与 Deep Link 集成,我发现 Branch.io 是最好的平台。成功读懂我的深linkURL。这是我在 didFinishLaunchingWithOptions.

中读取的代码
let branch: Branch = Branch.getInstance()
branch.initSession(launchOptions: launchOptions, andRegisterDeepLinkHandler: {params, error in
    if error == nil {
        print("params: %@", params as? [String: AnyObject] ?? {})
        //Read Deep Link
        let sessionParams = Branch.getInstance().getLatestReferringParams()
        let installParams = Branch.getInstance().getFirstReferringParams()
        guard let data = params as? [String: AnyObject] else { return }
        guard let options = data["promo_code"] as? String else { return }
        switch options {
        case "testing": self.window?.rootViewController?.present( ArticleViewController(), animated: true, completion: nil)
            print("Berhasil DeepLink")
        default: break
        }
    }
})

这是我得到的回复:

["$marketing_title": Paket Tour, 
"+is_first_session": 0, 
"$canonical_url": http://www.invisee.com, 
"~referring_link": https://invisee.app.link/promo?al_applink_da
ta=%7B%22target_url%22%3A%22https%3A%5C%2F%5C%2Fitunes.apple.com%5C%2FWe
bObjects%5C%2FMZStore.woa%5C%2Fwa%5C%2FredirectToContent%3Fid%3D1401179128%22%2
C%22extras%22%3A%7B%22deeplink_context%22%3A%22%7B%5C%22promo_code%5C%22%3A%5C%2
2testing%5C%22%7D%22%7D%2C%22referer_app_link%22%3A%7B%22url%22%3A%22fb%3A%5C%2F
%5C%2F%5C%2F%22%2C%22app_name%22%3A%22Facebook%22%7D%7D, 
"+clicked_branch_link": 1, 
"$og_title": INVISEE | Investment is easy, 
"al_applink_data": 
    {"target_url":"https:\/\/itunes.apple.com\/WebObjects\/MZStore.woa\/wa\/redirectToContent?id=1401179128",
        "extras":
            {"deeplink_context":"{\"promo_code\":\"testing\"}"},
        "referer_app_link":{"url":"fb:\/\/\/","app_name":"Facebook"}}, 
"$one_time_use": 0, "+match_guaranteed": 1, "$og_description": , 
"~id": 631024823602657926, 
"~creation_source": 1, 
"+click_timestamp": 1551695392, 
"~marketing": 1, 
"~campaign": reksadana, 
"~feature": ads, 
"~channel": facebook]

我的问题是,我想解析 "promo_code" 及其值。怎么做?正如您在我的代码中看到的那样,有一个 switch case,我希望我可以在那里放置一个事件。任何人都知道如何解决这个问题?非常感谢!

if let data =  al_appLink2.data(using: .utf8) {
do {
    let appLinkObject = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any]
            let extras = appLinkObject?["extras"] as? [String : Any]
            let promoCodeString = extras?["deeplink_context"] as? String
            if let promoString = promoCodeString, let data =  promoString.data(using: .utf8) {
                let promoCodeObject = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: String]
                let promoCode = promoCodeObject?["promo_code"]
                print(promoCode ?? "No Promo Code")
            }

        }
        catch(let error) {
            print(error.localizedDescription)
        }
    }
}}

试试上面的代码。您必须首先将 "al_applink_data" 作为字典获取。然后从中得到"extras"。然后是 "deeplink_context" 值。 "deeplink_context" 的值是一个代表 JSON 字符串表示的字符串。那么就得通过JSON序列化得到JSON对象,得到promo_code的值。我对 nil values.But 进行了一些编辑,确保你正确地测试它。谢谢