iOS 10 错误 [access] <private> 使用 UIImagePickerController 时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38236723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS 10 error [access] <private> when using UIImagePickerController
提问by Pranoy C
I am using XCode 8 and testing with iOS 10.2 Beta.
我正在使用 XCode 8 并使用 iOS 10.2 Beta 进行测试。
I have added the Photos, PhotosUI and MobileCoreServices frameworks to project.
我在项目中添加了 Photos、PhotosUI 和 MobileCoreServices 框架。
Very simple code:
非常简单的代码:
#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
and implementation:
和实施:
- (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];
}
As soon as I tap the button, the app crashes with very useless error:
一旦我点击按钮,应用程序就会崩溃并出现非常无用的错误:
[access] <private>
[access] <private>
That's it. Nothing else.
就是这样。没有其他的。
Using break statements, the app seems to crash at "presentViewController".
使用 break 语句,应用程序似乎在“presentViewController”处崩溃。
This is a brand new app and I don't have anything else in the UI other than the grab button.
这是一个全新的应用程序,除了抓取按钮之外,我在 UI 中没有其他任何东西。
Also, testing on iOS 9.3, this works fine. Am I missing something which might be changed in iOS 10?
此外,在 iOS 9.3 上测试,这工作正常。我是否错过了可能在 iOS 10 中更改的内容?
回答by rockdaswift
回答by ElonChan
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:
在iOS10中,在访问相机、联系人等隐私敏感数据之前,必须先请求授权,否则访问时应用程序会崩溃。然后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 必须包含一个
NSContactsUsageDescription
带有字符串值的键,向用户解释应用程序如何使用这些数据。
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 ??.
打开项目中名为 的文件info.plist
,右键单击它,打开为Source Code
,将下面的代码粘贴到其中。或者,您可以打开 info.plist
为Property List
默认,单击添加按钮,Xcode中会给你建议的完成,同时打字Privacy -
用键盘的帮助?和 ??。
Remember to write your description why you ask for this authorization, between <string>
and </string>
, or your app will be rejected by apple:
记得在<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>
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
:
或前往target -> Capabilities -> Background Modes -> open the background Modes
:
then clean your Project, run it.
然后清理你的项目,运行它。
Go to here for more information: iOS10AdaptationTips.
转到此处了解更多信息: iOS10AdaptationTips。
回答by Pritesh
回答by Pritesh
回答by Ramkrishna Sharma
In iOS 10, Apple has changed how you can access any user private data types.
在 iOS 10 中,Apple 更改了您访问任何用户私有数据类型的方式。
You need to add the Privacy - Photo Library Usage Descriptionkey to your app's Info.plist
and their usage information.
您需要将Privacy - Photo Library Usage Description键添加到您的应用Info.plist
及其使用信息中。
For more information please find the below GIF.
有关更多信息,请查看以下 GIF。
Or if you want to add via info.plist
then you need to add NSPhotoLibraryUsageDescriptionkey.
或者,如果您想添加 viainfo.plist
那么您需要添加NSPhotoLibraryUsageDescription键。
Just copy and paste below string in info.plist
.
只需将下面的字符串复制并粘贴到info.plist
.
<key>NSPhotoLibraryUsageDescription</key>
<string>Take the photo</string>
Please find the below GIF for more information.
请找到以下 GIF 以获取更多信息。