user1960279
user1960279

Reputation: 514

FileManager always gives false when check mediaUrl of video exist to Photo Library in swift

I have a condition to save the video path selected from the iPhone photo library. ImagePicker Controller is used to save media Url. I have successfully selected the video and saved the path to User defaults but when failed to Identify later if the same media file exists in the Photo library or not. I am adding the code below.

The below class is to select any video from the Photo library and saved the path via the View Model function.

class LinkVideoViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    func openGallery() {
        
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            let myPickerController = UIImagePickerController()
            myPickerController.delegate = self
            myPickerController.sourceType = .photoLibrary
            myPickerController.mediaTypes = ["public.movie"]
            myPickerController.videoQuality = .typeHigh
            self.present(myPickerController, animated: true, completion: nil)
        }
    }
    
}
extension LinkVideoViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
        if let videoUrl = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
            let urlString = videoUrl.relativePath
            print(FileManager.default.fileExists(atPath: videoUrl.relativePath))
            viewModel.selectVideo(selectedVideoPath: urlString)
            self.dismiss(animated: true, completion: nil)
        }
    }
    
    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        self.dismiss(animated: true, completion: nil)
    }
}

Below class where we detect if the same file exists in the Photo library or not.

override func start() {
    if let videoPath = UserDefaultValues.videoGuidedPath, !videoPath.isEmpty, let url = URL(string: videoPath) {
        if FileManager.default.fileExists(atPath: url.relativePath) {
            
           // if file exist then play video
        } else {
          //  show video does not exist error view
        }
    } else {
       // if not path is saved then probably user has not selected, so give an option for request permission
    }
}

I have checked, value is same still FileManager file Exist function gives a false value every time. I did not get the reason behind it. I have used path also in place of relative Path but the error is the same.

Upvotes: 0

Views: 353

Answers (1)

Deep Gandhi
Deep Gandhi

Reputation: 395

file manager can be tricky some times,

 FileManager.default.fileExists(atPath: "file:///Users/Mac/Library/Developer/CoreSimulator/Devices/A1AA0615-4AE4-4710-9E15-A6973BFFA9B8/data/Media/DCIM/100APPLE/IMG_0007.MP4") 
 // return false

 FileManager.default.fileExists(atPath: "Users/Mac/Library/Developer/CoreSimulator/Devices/A1AA0615-4AE4-4710-9E15-A6973BFFA9B8/data/Media/DCIM/100APPLE/IMG_0007.MP4") 
 // return true

Upvotes: 0

Related Questions