0

I created UIActivityViewController and check which share type is select. I need to save in my photo library and I check .saveToCameraRoll. If I don't call PHPhotoLibrary.requestAuthorization in the activityVC.completionWithItemsHandler then it always returns status like .notDetermined and shows permission request once. When I call PHPhotoLibrary.requestAuthorization in the caomplitionHandler it shows permission alert twice and when I allow access in the second alert I can check status as expected. My code:


        let sourceRect = rect ?? CGRect(x: frame.minX, y: frame.maxY - 1, width: frame.width, height: 1)

        activityVC.modalPresentationStyle = .popover
        activityVC.popoverPresentationController?.sourceRect = sourceRect
        activityVC.popoverPresentationController?.sourceView = viewController?.view

        activityVC.completionWithItemsHandler = { activity, success, items, error in
            guard activity == .saveToCameraRoll else { return }
            PHPhotoLibrary.requestAuthorization({ _ in
                if PHPhotoLibrary.authorizationStatus() != .authorized {
                    //here I show alert 
                }
            })
        }

        viewController?.present(activityVC, animated: true, completion: nil)```
Rustam Khisamov
  • 151
  • 1
  • 4
  • 8
  • Why do you do `if PHPhotoLibrary.authorizationStatus() != .authorized `, and not use the param that you silenced `_` in the completion block? – Larme Sep 11 '19 at 14:06
  • if the user denied access to the library I'll show the alert that the app needs it to save screenshots – Rustam Khisamov Sep 11 '19 at 14:09
  • I mean shouldn't it be `PHPhotoLibrary.requestAuthorization({ status in if status != .authorized { //here I show alert } })` instead of ``PHPhotoLibrary.requestAuthorization({ _ in if PHPhotoLibrary.authorizationStatus() != .authorized { //here I show alert } })`` – Larme Sep 11 '19 at 14:10
  • Good point. But it still will ask permission twice – Rustam Khisamov Sep 11 '19 at 14:12
  • At the first time I tryed like this ```activityVC.completionWithItemsHandler = { activity, success, items, error in guard activity == .saveToCameraRoll else { return } if PHPhotoLibrary.authorizationStatus() != .authorized { //here I show alert } }``` but it doesn't show the correct status if I denied access and trying to save again – Rustam Khisamov Sep 11 '19 at 14:14
  • How many time is this code called in that closure? Could it be called twice, and since the authorization is async, you have a mismatch? – Larme Sep 11 '19 at 14:15
  • yes it calls async https://developer.apple.com/documentation/photokit/phphotolibrary/1620736-requestauthorization but without `PHPhotoLibrary.requestAuthorization` it calls too – Rustam Khisamov Sep 11 '19 at 14:18

0 Answers0