0

I am trying to save google drive videos in my photo gallery.

The code I am using:

@objc func saveVideosToPhone(videoURL:URL,viewController:UIViewController){
    PHPhotoLibrary.requestAuthorization
    { (status) -> Void in
        switch (status)
        {
        case .authorized:
            // Permission Granted
            print("Write your code here")

            let urlData = NSData.init(contentsOf:videoURL)
            if(urlData != nil)
            {
                let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0];
                let filePath = String.init(format:"\(documentsPath)/%@.mp4",self.randomString(length:5))
                urlData?.write(toFile: filePath, atomically: true);
                PHPhotoLibrary.shared().performChanges({
                    PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: NSURL(fileURLWithPath: filePath) as URL)
                }) { completed, error in
                    if completed {
                        MBProgressHUD.hide(for:viewController.view, animated:false)
                        UtilityMethods.sharedInstance.showAlert(title:"Save", message:"Video is saved!", viewController:viewController)
                        print("Video is saved!")

                    }else{
                        MBProgressHUD.hide(for:viewController.view, animated:false)
                        UtilityMethods.sharedInstance.showAlert(title:"Error", message:(error?.localizedDescription)!, viewController:viewController)
                        print("error from gd",error?.localizedDescription)
                    }
                }}
        case .denied:
            // Permission Denied
            print("User denied")
        default:
            print("Restricted")
        }
}

But,first time when the app asks for permission, and user gives permission, after that it shows the following error:

Photo access not allowed,authorization status 0

And, second time when user tries to save video to photo gallery ,it shows the following error:

The operation couldn't be completed

Gaurav Bharti
  • 1,065
  • 1
  • 14
  • 22
Pratz_iOS
  • 67
  • 1
  • 8
  • Where's your code to request permission to access the photo library? Where's your code to check the current permission? – rmaddy Mar 10 '18 at 03:48
  • For requesting permission,I have added the required fields in info.plist.Not checked any current permission. – Pratz_iOS Mar 10 '18 at 04:02
  • You need to write actual code to request and check permission. The entries in Info.plist are just one small step. – rmaddy Mar 10 '18 at 04:03
  • Can you share any sample or link for that? – Pratz_iOS Mar 10 '18 at 04:06
  • Look at the documentation for `PHPhotoLibrary`. – rmaddy Mar 10 '18 at 04:10
  • Sir ,it shows a field authorizationstatus...which tells about the permission thing but what if the permission is not granted.I ahve used the same code to download videos from facebook and it is working fine but it doesn't work in case of google drive only. – Pratz_iOS Mar 10 '18 at 04:36
  • It has nothing to do with Google drive or Facebook. In order to access a user's photo library you need to check the current authorization status using `PHPhotoLibrary`. If the status is not determined you need to request permission. If the status is denied, you can't access the photo library. – rmaddy Mar 10 '18 at 04:41
  • I have added modified code,where permission status is .authorized but still getting the same problem. – Pratz_iOS Mar 10 '18 at 06:29
  • This is not a duplicate question and the problem was with google drive,you cant save videos of google drive using url ...I just passed the file.data and saved videos.Maybe I could be wrong but this worked for me. – Pratz_iOS Mar 10 '18 at 07:28

0 Answers0