在iOS上的SWIFT中从短URL获取完整URL

Get full URL from short URL in Swift on iOS(在iOS上的SWIFT中从短URL获取完整URL)
本文介绍了在iOS上的SWIFT中从短URL获取完整URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个简短的URLhttps://itun.es/us/JB7h_,如何将其展开为完整的URL?例如https://music.apple.com/us/album/blackstar/1059043043

推荐答案

扩展

   extension URL {
    func getExpandedURL() async -> URL? {
        var request = URLRequest(url: self)
        request.httpMethod = "HEAD"
        let data = try? await URLSession.shared.data(for: request)
        return data?.1.url
    }
}

示例

struct ContentView: View {
    let shortURL = URL(string: "https://itun.es/us/JB7h_")
    @State var expandedURL: URL?
    
    var body: some View {
        Form {
            Section("Short URL") {
                Text(shortURL?.description ?? "")
            }
            
            Section("Long URL") {
                Text(expandedURL?.description ?? "")
            }
        }
        
        .task {
            expandedURL = await shortURL?.getExpandedURL()
        }
    }
    
}

这篇关于在iOS上的SWIFT中从短URL获取完整URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)