iOS 通过商品短链接跳转京东商品详情页

数据

{
    "code": 1,
    "msg": "操作成功",
    "data": {
        "url": "https://u.jd.com/q6FopAY",
        "short_url": "https://u.jd.com/q6FopAY",
    }
}

通过商品短链接跳转京东商品详情页

func goJD(_ url : String){
    let jdDict = ["category":"jump", "des":"getCoupon", "url":url ]
    guard let jdStr = jdDict.formatJSON() else { return }
    let urlStr = "openapp.jdmobile://virtual?params=\(jdStr)"
    let utf8Str = urlStr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
    var openSuccess = false
    if let jumpURL = URL(string: utf8Str ?? "") {
        let isCanOpen = UIApplication.shared.canOpenURL(jumpURL)
        if isCanOpen {
            UIApplication.shared.open(jumpURL, options: [:], completionHandler: nil)
            openSuccess = true
        }
    }
    if (openSuccess == false) {
        UIApplication.shared.open(URL(string: url)!, options: [:], completionHandler: nil)
    }
}

拓展,Dictionary与JSON字符串相互转换

import Foundation

extension Dictionary {


    /// EZSE: Unserialize JSON string into Dictionary
    public static func constructFromJSON (json: String) -> Dictionary? {
        if let data = (try? JSONSerialization.jsonObject(
            with: json.data(using: String.Encoding.utf8,
                            allowLossyConversion: true)!,
            options: JSONSerialization.ReadingOptions.mutableContainers)) as? Dictionary {
            return data
        } else {
            return nil
        }
    }

    /// EZSE: Serialize Dictionary into JSON string
    public func formatJSON() -> String? {
        if let jsonData = try? JSONSerialization.data(withJSONObject: self, options: JSONSerialization.WritingOptions()) {
            let jsonStr = String(data: jsonData, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))
            return String(jsonStr ?? "")
        }
        return nil
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值