まだどこにもないアプリを作る

アプリ開発でつまづいたところなどを中心に記事にして行きます。

Swift-フォトライブラリ許可アラートをキャンセルするとアプリが落ちるios13?

This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.

 

こんなエラーでて落ちました

 

多分再度設定を促すアラートを変なところに入れてたのが原因だと思います

 

以下写真を選択した時に呼ぶメソッドを書きました

 func imagePick(sourceType:UIImagePickerController.SourceType){//写真フォトライブラリ

        

        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) && sourceType == .photoLibrary {

            if PHPhotoLibrary.authorizationStatus() != .authorized {

                //この下の行が呼ばれるとシステムでの許可アラートが出るようだ。//一回しか出ない

                PHPhotoLibrary.requestAuthorization { status in

                    switch status {

                            case .authorized:

                                print("ユーザーによって許可された")

                            case .denied:

                                //最初のアラートでキャンセルするとここが出るみたい

                                print("ユーザーによって拒否された")

                            case .restricted:

                                print("利用不可2")

                            case .notDetermined:

                                print("未定義2")

                            default:

                                print("未定義2")

                            }

                    }

            }else{

                let pickerView = UIImagePickerController() // 写真を選ぶビュー

                pickerView.sourceType = sourceType

                pickerView.delegate = self

                self.present(pickerView, animated: true)// ビューに表示

            }

            

             let status = PHPhotoLibrary.authorizationStatus()

                    switch status {

                    case .authorized:

                        print("ユーザーによって許可されている2")

                    case .denied:

                        print("ユーザーによって拒否されている2")

                    case .restricted:

                        print("利用不可2")

                    case .notDetermined:

                        print("未定義2")

                    default:

                        print("未定義2")

                    }

                    if status != .authorized {

                                // フォトライブラリへのアクセスが許可されていないため、アラートを表示する

                                print("ライブラリへのアクセスが許可されていません")

                                let alert = UIAlertController(title: NSLocalizedString("You are not authorized to access your photo library", comment: ""), message: NSLocalizedString("To change the ranking image using the images stored in the photo library, you need to allow access to the photo library.", comment: ""), preferredStyle: .alert)

                                let settingsAction = UIAlertAction(title: NSLocalizedString("Settings", comment: ""), style: .default, handler: { (_) -> Void in

                                    guard let settingsURL = URL(string: UIApplication.openSettingsURLString ) else {

                                        return

                                    }

                                    UIApplication.shared.open(settingsURL, options: [:], completionHandler: nil)

                                })

            

                                let closeAction: UIAlertAction = UIAlertAction(title: "キャンセル", style: .cancel, handler: nil)

            

                                alert.addAction(settingsAction)

                                alert.addAction(closeAction)

                                self.present(alert, animated: true, completion: nil)

                    }else{//許可されていたら

                        let pickerView = UIImagePickerController() // 写真を選ぶビュー

                            pickerView.sourceType = sourceType

                            pickerView.delegate = self

                            self.present(pickerView, animated: true)// ビューに表示

                    }

        }

    }

 

この書き方に落ち着きました

長いコードですが参考にできたら

参考にしてみてください