我喜欢在用户单击按钮时将应用转发到 iTunes 或 Apple Music(就像为 Instagram 或 FV 所做的那样),下面的代码给出 found nil 错误。有没有办法做到这一点?或者在我的应用中更好地播放预览。
var url = NSURL(string: "itms://itunes.apple.com/us/album/ne-olacak-dj-funky-c-vs-ogün-dalka-single/id1202943921")
if UIApplication.shared.canOpenURL(url! as URL) {
UIApplication.shared.openURL(url! as URL)
}
最佳答案
This method expects URLString to contain only characters that are allowed in a properly formed URL. All other characters must be properly percent escaped. Any percent-escaped characters are interpreted using UTF-8 encoding.
你需要在将它传递给 NSURL 之前对其进行编码,你可以通过 stringByAddingPercentEncodingWithAllowedCharacters
let url = URL(string: "itms://itunes.apple.com/us/album/ne-olacak-dj-funky-c-vs-ogün-dalka-single/id1202943921".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
if UIApplication.shared.canOpenURL(url! as URL) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}
对于那个错误你需要遵循这个
这是 Apple 在 iOS 9 中构建的所有应用程序上实现的一项新的强制安全措施。
到目前为止,唯一的解决方案是在 info.plist 文件中添加一个条目,其中包含 key LSApplicationQueriesSchemes 并添加“itms”以及您的应用程序将链接到此数组中的任何其他 url 方案。
关于ios - 在 Swift 中链接到 iTunes 音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43030242/