又到了必須適配App至最新的iOS 14,iOS SDK每年都有一定程度的大躍進,Apple有意無意讓開發者要不斷往前進,若不與時俱進者,則很快就會被淘汰囉~今年最大的適配項目是隱私權。
對於我們家快樂印App來說,這次更新最主要的項目在於相片權限 (Photos Authorization)。乍聽之下沒什麼,不過流程就必須要適當調整,讓用戶能知道相片為何無法被我們App存取。
一旦更新到iOS 14,習以為常的相片權限詢問就會跳出提示,可是此刻的選項變得不一樣!?可讓用戶選擇授權「全部相片」或是「部分相片」,若是選全部相片,一切流程就照舊,那如果選擇部分相片,則App僅能存取用戶選擇的相片。這種規則一開始會讓用戶很不習慣,所以App勢必要做好UI/UX引導用戶。
用文字說明,可能還有用戶不知道要去哪裡設定,那此時可以提供,點擊某處即能到該處設定。
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
想知道用戶先前相片權限選擇何者?下方程式碼可以查到~
/** Theme: Photos Authorization IDE: Xcode 12 Language: Objective C Date: 109/10/07 Author: HappyMan Blog: https://cg2010studio.com/ */ if (@available(iOS 14, *)) { // 查詢權限 PHAccessLevel level = PHAccessLevelReadWrite; PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatusForAccessLevel:level]; switch (status) { case PHAuthorizationStatusLimited: NSLog(@"limited"); break; case PHAuthorizationStatusDenied: NSLog(@"denied"); break; case PHAuthorizationStatusAuthorized: NSLog(@"authorized"); break; default: break; } // 請求權限,需注意limited權限在accessLevel為readAndWrite時生效 [PHPhotoLibrary requestAuthorizationForAccessLevel:level handler:^(PHAuthorizationStatus status) { switch (status) { case PHAuthorizationStatusLimited: NSLog(@"limited"); break; case PHAuthorizationStatusDenied: NSLog(@"denied"); break; case PHAuthorizationStatusAuthorized: NSLog(@"authorized"); break; default: break; } }]; } else { // Fallback on earlier versions }
iOS14中當用戶選擇「部分相片」時,如果未進行適配,有可能會在每次觸發相簿功能時,都跳出詢問用戶是否需要修改照片權限。
對於這種情況可藉由在Info.plist中設定「PHPhotoLibraryPreventAutomaticLimitedAccessAlert」的值為YES,來阻止該提示反覆跳出,並且可透過下面這個API來主動控制,何時彈出PHPickerViewController進行照片選擇
[[PHPhotoLibrary sharedPhotoLibrary] presentLimitedLibraryPickerFromViewController:self];
若到了最後要把相片上傳,發現照片遺失,此時可以做個提示⋯⋯
可以稍微看一下相簿權限定義如何~
typedef NS_ENUM(NSInteger, PHAuthorizationStatus) { PHAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application PHAuthorizationStatusRestricted, // This application is not authorized to access photo data. // The user cannot change this application’s status, possibly due to active restrictions // such as parental controls being in place. PHAuthorizationStatusDenied, // User has explicitly denied this application access to photos data. PHAuthorizationStatusAuthorized, // User has authorized this application to access photos data. PHAuthorizationStatusLimited API_AVAILABLE(ios(14)), // User has authorized this application for limited photo library access. Add PHPhotoLibraryPreventAutomaticLimitedAccessAlert = YES to the application's Info.plist to prevent the automatic alert to update the users limited library selection. Use -[PHPhotoLibrary(PhotosUISupport) presentLimitedLibraryPickerFromViewController:] from PhotosUI/PHPhotoLibrary+PhotosUISupport.h to manually present the limited library picker. };
參考:
隨意留個言吧:)~