Search code examples
swiftxcodevlc

How can I open vlc with a URL scheme


How can I open vlc with URL scheme? I currently have a video file stored locally with the name video and I receive an error:

         "Unexpectedly found nil while unwrapping an Optional value"  

The Code

           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)

Solution

  • Try adding encoding to the URL with addingPercentEncoding(withAllowedCharacters:).

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