Thursday, 23 May 2019

unable to get the video on web browser using GCDWebServer in swift

I created a small demo app where app access the image and videos from iPhone using UIImagePickerController. When I select any image or video, app creates a copy of it( image or video) in document directory. And create a web server on iPhone using GCDWebserver and need to expose the selected image or video. But it does not work.

Here is the sample code, not sure where I could be wrong.

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var btnselect: UIButton!
let videoPicker = UIImagePickerController()

@IBAction func btnSelect(_ sender: Any) {

        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
            let myPickerController = UIImagePickerController()
            myPickerController.delegate = self;
            myPickerController.sourceType = .photoLibrary
            myPickerController.allowsEditing = false
            self.present(myPickerController, animated: true, completion: nil)
        }

    }

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        self.dismiss(animated: true, completion: nil)
    }

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    // 1. saving video to documents directory

      let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as! NSURL
            let videoData = NSData(contentsOf: videoURL as URL)
            let path = try! FileManager.default.url(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask, appropriateFor: nil, create: false)

            let newPath = path.appendingPathComponent("/myTestVideo.mp4")
            do {
                try videoData?.write(to: newPath)
            } catch {
                print(error)
            }


 // 2. Create web server on iPhone using GCDWebServer

        let webServer = GCDWebServer()
        webServer.addDefaultHandler(forMethod: "GET", request: GCDWebServerRequest.self, processBlock: {request in
        return GCDWebServerDataResponse(newPath)

       })
       webServer.start(withPort: 8080, bonjourName: "GCD Web Server")

}
}

I need this selected (image or video) to be hosted/accessible by this GCD Web Server.



from unable to get the video on web browser using GCDWebServer in swift

No comments:

Post a Comment