iOS 10 错误 [访问] <private> 使用 UIImagePickerController 时

iOS 10 error [access] <private> when using UIImagePickerController

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

我已将 Photos、PhotosUI 和 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 语句,应用似乎在 "presentViewController" 处崩溃。

这是一个全新的应用程序,UI 除了抓取按钮,我没有其他任何东西。

另外,在 iOS 9.3 上测试,这工作正常。我是否遗漏了一些可能会在 iOS 10 中更改的内容?

您可能需要将 NSPhotoLibraryUsageDescription 放入您的 plist 中。 喜欢

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

查看所有使用说明here

在iOS10中,在访问privacy-sensitive相机、联系人等数据之前,您必须请求授权,否则您的应用程序会在您访问them.Then时崩溃Xcode 将记录为:

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.

如何处理?

在您的项目中打开名为 info.plist 的文件,右键单击它,以 Source Code 打开,将下面的代码粘贴到其中。或者您可以默认将 info.plist 打开为 Property List,单击添加按钮,Xcode 将在键盘 ⬆️ 和 ⬇️ 的帮助下输入 Privacy - 时为您提供建议补全。

记得在 <string></string> 之间写下您请求此授权的原因,否则您的应用将被苹果拒绝:

<!--  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>

如果不行,请尝试请求后台授权:

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

或前往target -> Capabilities -> Background Modes -> open the background Modes:

然后清理你的项目,运行它。

前往此处获取更多信息:iOS10AdaptationTips .

在 iOS10 中,如果您在应用中使用相机或照片库,则需要添加下图中提到的密钥

您需要为您添加新的隐私设置 info.plist。

不要忘记添加描述应用程序需要访问服务的原因的值。

在 iOS10 中,Apple 更改了访问任何用户私有数据类型的方式。

您需要将 Privacy - Photo Library Usage Description 键添加到您应用的 Info.plist 及其使用信息中。

更多信息请查看下面的 GIF。

或者,如果您想通过 info.plist 添加,则需要添加 NSPhotoLibraryUsageDescription 键。

只需复制并粘贴到 info.plist 中的字符串下方即可。

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

请找到下面的 GIF 以获取更多信息。