在Swift中展开一个简短的URL [英] Expand a short URL in Swift

查看:82
本文介绍了在Swift中展开一个简短的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有一个简短的网址 https://itun.es/us/JB7h_ ,您如何扩展它进入完整的URL?

Given a short URL https://itun.es/us/JB7h_, How do you expand it into the full URL?

推荐答案

分机



Extension

extension NSURL {
    func expandURLWithCompletionHandler(completionHandler: (NSURL?) -> Void) {
        let dataTask = NSURLSession.sharedSession().dataTaskWithURL(self, completionHandler: {
            _, response, _ in
            if let expandedURL = response?.URL {
                completionHandler(expandedURL)
            }
        })
        dataTask.resume()
    }
}



示例



Example

let shortURL = NSURL(string: "https://itun.es/us/JB7h_")

shortURL?.expandURLWithCompletionHandler({
expandedURL in
    print("ExpandedURL:\(expandedURL)")
    //https://itunes.apple.com/us/album/blackstar/id1059043043
})

这篇关于在Swift中展开一个简短的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆