How can [UIImagePickerController.InfoKey.phAsset] as? PHAsset be nil

659 Views Asked by At

I sent a user a TestFlight version of my app. She has a video recorded that she made using her iPhone. When the imagePicker was presented and she selected a video it came back as nil. I have an error message with a number 120 that appears in an alert that tells me where the error occurred.

It can only be the asset [UIImagePickerController.InfoKey.phAsset] as? PHAsset is nil. I don't see how that's possible because it's a video that she made using her phone. The odd thing is when she selects a photo everything works fine and when I select videos using iOS 14 and iOS 13 everything works fine.

She's on iOS 15.1, and I'm wondering is that the issue? She's a fellow iOS dev and she said iOS 15 has been causing issues.

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

    let asset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset
    // ...

    if let style = asset?.playbackStyle {

        // ...
        // if it entered here then the below alert would have never appeared

    } else {

        let errorMessage = "Error: 120"
        let alert = UIAlertController(title: "Unknown Error", message: errorMessage, preferredStyle: .alert)
        // ...
    }

    imagePicker?.dismiss(animated: true, completion: nil)
}

FYI afterwards I just tested on iOS 15.1 with some videos and it worked fine.

I tried the PHPickerController but it's very buggy so I'd rather stick with the ImagePicker for now.

1

There are 1 best solutions below

0
Lance Samaria On

I'm not exactly sure how the asset can be nil but according to this answer by @Sh_Kahn I need to handle all possibilities:

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

    if let asset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {

        let style = asset.playbackStyle
        // ...

    } else if let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as? URL {

        // ...

    } else if let imageUrl = info[UIImagePickerController.InfoKey.imageURL] as? URL {

        // ...

    } else {

        let errorMessage = "Error: 120"
        let alert = UIAlertController(title: "Unknown Error", message: errorMessage, preferredStyle: .alert)
        // ...
    }

    imagePicker?.dismiss(animated: true, completion: nil)
}