ios - 在 Swift 中链接到 iTunes 音乐

标签 ios swift swift3

我喜欢在用户单击按钮时将应用转发到 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 方案。

LSApplicationQueriesSchemes

关于ios - 在 Swift 中链接到 iTunes 音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43030242/

相关文章:

iphone - 我如何编写一个使用 iOS5 推特框架的 iphone 应用程序,但它会在 iOS4 上正常降级?

ios - 如何处理本地化 swift 中的特殊情况

ios - 如何在 swift 3 中使用 NSPredicate 过滤数组

ios - locationManager 在 Swift 3 中被弃用了吗?

ios - 无法以编程方式设置 UIPickerView 数据源/委托(delegate)

objective-c - 使用不同的二进制文件进行应用内购买

Swift 3 和 EKEventStoreRequestAccessCompletionHandler 抛出异常

ios - 从 Swift 2.2 更新后 switch 和 case 语句不起作用

ios - 如何在 inputView 中设置默认日期值

ios - 如何正确使用NSCache?