如何使用URL方案打开VLC

时间:2020-06-20 17:19:57

标签: swift xcode vlc

如何使用URL方案打开vlc? 我目前有一个本地视频文件,名称为video,并且收到错误消息:

         "Unexpectedly found nil while unwrapping an Optional value"  

代码

           let vlcURL = "vlc://video.mp4"
           let vlcItem = ("vlc", URL(string:vlcURL)!)  //i recive the error her

          var installedvideoApps = [("Vlc", URL(string:vlcURL)!)]

           if UIApplication.shared.canOpenURL(vlcItem.1) {
               installedvideoApps.append(vlcItem)
           }

           let alert = UIAlertController(title: "Selection", message: "Select Video App",   
           preferredStyle: .actionSheet)

           for app in installedvideoApps {
               let button = UIAlertAction(title: app.0, style: .default, handler: { _ in
                   UIApplication.shared.open(app.1, options: [:], completionHandler: nil)
               })
               alert.addAction(button)
           }
           let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
           alert.addAction(cancel)
           present(alert, animated: true)

1 个答案:

答案 0 :(得分:1)

尝试使用URLaddingPercentEncoding(withAllowedCharacters:)添加编码。

let vlcURL = "vlc://video.mp4".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let vlcItem = ("vlc", URL(string:vlcURL)!)