使用 UIImagePickerController 时,iOS10错误[ access ] < private >

我正在使用 XCode 8,并在测试 iOS 10.2 Beta。

我已经添加了 Photos、 PhotoUI 和 MobileCoreServices 框架来进行项目。

非常简单的代码:

#import <Photos/Photos.h>
#import <PhotosUI/PhotosUI.h>
#import <MobileCoreServices/MobileCoreServices.h>


@interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, PHLivePhotoViewDelegate>


@property (strong, nonatomic) IBOutlet UIImageView *imageview;


@end

及推行:

- (IBAction)grab:(UIButton *)sender{
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsEditing = NO;
picker.delegate = self;


// make sure we include Live Photos (otherwise we'll only get UIImages)
NSArray *mediaTypes = @[(NSString *)kUTTypeImage, (NSString *)kUTTypeLivePhoto];
picker.mediaTypes = mediaTypes;


// bring up the picker
[self presentViewController:picker animated:YES completion:nil];
}

我一点击这个按钮,这个应用程序就崩溃了,出现了一个毫无意义的错误:

[access] <private>

就这样,没别的了。

使用 break 语句,该应用程序似乎在“ presViewController”处崩溃。

这是一个全新的应用程序,除了抓取按钮,我在用户界面中没有其他东西。

另外,在 iOS9.3上测试,这个工作得很好。我是否遗漏了一些在 iOS10中可能会改变的东西?

60161 次浏览

You may need to put the NSPhotoLibraryUsageDescription in your plist. Like

<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) uses photos</string>

Check all the usage descriptions here.

In iOS10, Before you access privacy-sensitive data like Camera, Contacts, and so on, you must ask for the authorization, or your app will crash when you access them.Then Xcode will log like:

This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSContactsUsageDescription key with a string value explaining to the user how the app uses this data.

How to deal with this?

Open the file in your project named info.plist, right click it, opening as Source Code, paste this code below to it. Or you can open info.plist as Property List by default, click the add button, Xcode will give you the suggest completions while typing Privacy - with the help of keyboard ⬆️ and ⬇️.

Remember to write your description why you ask for this authorization, between <string> and </string>, or your app will be rejected by apple:

<!-- 🖼 Photo Library -->
<key>NSPhotoLibraryUsageDescription</key>
<string>$(PRODUCT_NAME) photo use</string>


<!-- 📷 Camera -->
<key>NSCameraUsageDescription</key>
<string>$(PRODUCT_NAME) camera use</string>


<!-- 🖼 Write To Image Gallery>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>$(PRODUCT_NAME) save phots in gallry</string>




<!-- 🎤 Microphone -->
<key>NSMicrophoneUsageDescription</key>
<string>$(PRODUCT_NAME) microphone use</string>


<!-- 📍 Location -->
<key>NSLocationUsageDescription</key>
<string>$(PRODUCT_NAME) location use</string>


<!-- 📍 Location When In Use -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) location use</string>


<!-- 📍 Location Always -->
<key>NSLocationAlwaysUsageDescription</key>
<string>$(PRODUCT_NAME) always uses location </string>


<!-- 📆 Calendars -->
<key>NSCalendarsUsageDescription</key>
<string>$(PRODUCT_NAME) calendar events</string>


<!-- ⏰ Reminders -->
<key>NSRemindersUsageDescription</key>
<string>$(PRODUCT_NAME) reminder use</string>


<!-- 📒 Contacts -->
<key>NSContactsUsageDescription</key>
<string>$(PRODUCT_NAME) contact use</string>


<!-- 🏊 Motion -->
<key>NSMotionUsageDescription</key>
<string>$(PRODUCT_NAME) motion use</string>


<!-- 💊 Health Update -->
<key>NSHealthUpdateUsageDescription</key>
<string>$(PRODUCT_NAME) heath update use</string>


<!-- 💊 Health Share -->
<key>NSHealthShareUsageDescription</key>
<string>$(PRODUCT_NAME) heath share use</string>


<!-- ᛒ🔵 Bluetooth Peripheral -->
<key>NSBluetoothPeripheralUsageDescription</key>
<string>$(PRODUCT_NAME) Bluetooth Peripheral use</string>


<!-- 🎵 Media Library -->
<key>NSAppleMusicUsageDescription</key>
<string>$(PRODUCT_NAME) media library use</string>


<!-- 📱 Siri -->
<key>NSSiriUsageDescription</key>
<string>$(PRODUCT_NAME) siri use</string>


<!-- 🏡 HomeKit -->
<key>NSHomeKitUsageDescription</key>
<string>$(PRODUCT_NAME) home kit use</string>


<!-- 📻 SpeechRecognition -->
<key>NSSpeechRecognitionUsageDescription</key>
<string>$(PRODUCT_NAME) speech use</string>


<!-- 📺 VideoSubscriber -->
<key>NSVideoSubscriberAccountUsageDescription</key>
<string>$(PRODUCT_NAME) tvProvider use</string>

If it does not works, try to ask for the the background authorization:

<key>UIBackgroundModes</key>
<array>
<!-- something you should use in background -->
<string>location</string>
</array>

Or go to target -> Capabilities -> Background Modes -> open the background Modes:

enter image description here

then clean your Project, run it.

Go to here for more information: iOS10AdaptationTips .

in iOS 10 you need to add the key mentioned in below image if you are using camera or photo gallery in your app

.plist image

You need the add the new privacy settings to you info.plist.

Don't forget to add the value describing why the app need to access the service.

enter image description here

In iOS 10, Apple has changed how you can access any user private data types.

You need to add the Privacy - Photo Library Usage Description key to your app’s Info.plist and their usage information.

For more information please find the below GIF.

GIF

Or if you want to add via info.plist then you need to add NSPhotoLibraryUsageDescription key.

Just copy and paste below string in info.plist.

<key>NSPhotoLibraryUsageDescription</key>
<string>Take the photo</string>

Please find the below GIF for more information.

GIF