OpenURL 无法打开一些非常具体的 URL 并显示错误"Download Failed"



我想用苹果设备的Safari浏览器打开一些URL。我的 openURL 函数大部分时间都工作正常。但是,当我尝试打开此URL时,它失败并出现以下错误

下载

失败的野生动物园无法下载文件。

以下是失败的 URL:https://www.youtube.com/v/QH2-TGUlwu4?version=3&autohide=1

这是我的代码:

let url = "https://www.youtube.com/v/QH2-TGUlwu4?version=3&autohide=1"
if let nsurl = NSURL(string: url){
    if UIApplication.sharedApplication().canOpenURL(nsurl){
        UIApplication.sharedApplication().openURL(nsurl)
    }else{
        print("Cannot open this NSURL.")
    }
}else{
    print("Cannot convert String to NSURL.")
}

你提到的网址https://www.youtube.com/v/QH2-TGUlwu4?version=3&autohide=1包含一个SWF文件,即冲击波闪存文件。

Flash无法在基于iOS的设备上播放(链接)。

1)您需要向用户显示适当的错误消息。

2)或者您可以将用户重定向到YouTube应用程序(而不是Safari),用户可以在其中流式传输和观看视频。请参阅此 Apple 文档链接,了解如何将用户重定向到 YouTube 应用。

您需要

在使用 URL 之前对其进行编码,如下所示:

let urlString = "https://www.youtube.com/v/QH2-TGUlwu4?version=3&autohide=1"
var escapedString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())
if let nsurl = NSURL(string: escapedString){
    if UIApplication.sharedApplication().canOpenURL(nsurl){
        UIApplication.sharedApplication().openURL(nsurl)
    }else{
        print("Cannot open this NSURL.")
    }
}else{
    print("Cannot convert String to NSURL.")
}

最新更新