iOS 10 error [access] when using UIImage

2020-01-25 13:16发布

问题:

I am using XCode 8 and testing with iOS 10.2 Beta.

I have added the Photos, PhotosUI and MobileCoreServices frameworks to project.

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>

That's it. Nothing else.

Using break statements, the app seems to crash at "presentViewController".

This is a brand new app and I don't have anything else in the UI other than the grab button.

Also, testing on iOS 9.3, this works fine. Am I missing something which might be changed in iOS 10?

回答1:

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.



回答2:

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:

<!--