12
13

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 5 years have passed since last update.

UIImagePickerController使うときのエラー処理(というか気をつけること)

Posted at

いつも忘れる

必要

  • AssetsLibrary

コード

SourceTypeがUIImagePickerControllerSourceTypeCamera (カメラを使う)

camera
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
	UIImagePickerController *picker = [[UIImagePickerController alloc] init];
	picker.sourceType = UIImagePickerControllerSourceTypeCamera;
	[self presentViewController:picker animated:YES completion:nil];
} else {
	// Error:君のiPhoneカメラ使えないよ
}

SourceTypeがUIImagePickerControllerSourceTypePhotoLibrary (ライブラリを使う)

library
// iOS6以上でプライバシーの項目が追加されている
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_5_1) {
	ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
	if (status == ALAuthorizationStatusRestricted || status == ALAuthorizationStatusDenied){
		// ペアレンタルコントロールで許可されてない or 許可が'いいえ'
		// Error:設定的にライブラリアクセス出来ないよ
	} else if (status == ALAuthorizationStatusAuthorized || status == ALAuthorizationStatusNotDetermined){
		// 許可 or まだ許可ダイアログ出たことない
		if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
			UIImagePickerController *picker = [[UIImagePickerController alloc] init];
			picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
			[self presentViewController:picker animated:YES completion:nil];
		} else {
			// Error:君のiPhoneライブラリ使えないよ
		}
	}
} else {
	if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
		UIImagePickerController *picker = [[UIImagePickerController alloc] init];
		picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
		[self presentViewController:picker animated:YES completion:nil];
	} else {
		// Error:君のiPhoneライブラリ使えないよ
	}
}

わかりやすさを優先して一部イケてないコードになっているので、適宜直して使ってください。

直にカメラにアクセスしたいときなどは、フロントカメラ持ってる?とか
フラッシュ使える?とかもっと色々確認事項多いです。

まとめ

カレンダーとか位置情報とかも同じ感じでめんどくさい(はず)。
必要になったタイミングでまとめよう〜

12
13
1

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
12
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?