2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Swift/iOS13とiOS14でのフォトライブラリー/アルバム/写真対応(.addOnly)のみ

Posted at

本日内容

フォトライブラリーの仕様が変わったから、対応しようとしたら、、、、躓いてしまったので共有です。
ミスしてクラッシュを引き起こしてしまったので、皆さんの参考になればと。。。
またこの実装は、ViewController一画面実装とする。

開発環境

Xcode 14.3
Swift

手順

  1. Info.plistに下記を追加する

Privacy - Photo Library Usage Description
Privacy - Photo Library Additions Usage Description
を追加します。
Info_plist.png

フォトライブラリーを使う利用する理由を記載します。

  1. inportする

必要なViewControllerに、「import Photos」を追加する

  1. 許可するためのソースコードを記載します。
.swift
 if #available(iOS 14, *) {
            switch (PHPhotoLibrary.authorizationStatus(for: .addOnly)) {
            case .notDetermined: // フォトライブラリへのアクセスについてユーザーから明示的な回答を得ていない。
                PHPhotoLibrary.requestAuthorization(for: .addOnly, handler:{ status in
                    switch status {
                    case .authorized:
                        self.showPickerView()
                        break
                    default:
                        break
                    }
                })
                break
            case .authorized, .limited: // フォトライブラリへのアクセスについてユーザーが明示的に「許可」をした。
                self.showPickerView()
                break
            default:
                // 許可されていません
                break
            }
        } else {
            switch (PHPhotoLibrary.authorizationStatus()) {
            case .notDetermined: // フォトライブラリへのアクセスについてユーザーから明示的な回答を得ていない。
                PHPhotoLibrary.requestAuthorization({ status in
                    switch status {
                    case .authorized:
                        self.showPickerView()
                        break
                    default:
                        break
                    }
                })
                break
            case .authorized: // フォトライブラリへのアクセスについてユーザーが明示的に「許可」をした。
                print("authorized")
                self.showPickerView()
                break
            default:
                // 許可されていません
                break
            }
        }

    private func showPickerView() {
        // 写真を選ぶビュー
        DispatchQueue.main.async {
            let pickerView = UIImagePickerController()
            pickerView.sourceType = .photoLibrary
            pickerView.delegate = self
            self.present(pickerView, animated: true)
        }
    }

これで一通り完成です!
間違っていたら、コメントいただると幸いです。
素敵なiOSアプリ開発を〜〜

補足

許可されていない人に対して、端末のアプリ設定画面へ遷移させるソースコードも記載します。
よかったら、参考してください〜!

.swift
   func markAccessSettingAlert() -> UIAlertController {
        // フォトライブラリへのアクセスが許可されていないため、アラートを表示する
        let alert = UIAlertController(title: "アクセスが許可されていません。", message: "フォトライブラリへのアクセスが許可されていません。端末のアプリ設定を開いて、変更する場合は設定を押下してください。", preferredStyle: .alert)
        let settingsAction = UIAlertAction(title: "設定", 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)
    }

参考資料

iOS14以降のPHAuthorizationStatus
https://qiita.com/fuziki/items/87a3a1a8e481a1546b38

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?