UIImagePickerController crash only on iOS 7 - iPad

前端 未结 3 1840
无人共我
无人共我 2020-12-13 21:04

I\'m getting hundreds of crashes from one of my live apps ever since people started upgrading to iOS 7. Has anyone else seen this problem? Nothing reproduces on my iPad 3 wi

3条回答
  •  被撕碎了的回忆
    2020-12-13 21:45

    Collectively we've come to the conclusion that this is a bug in iOS 7 on iPad. It occurs when you attempt to show a UIImagePickerController in a UIPopoverControl from a UIBarButtonItem for the first time. After the user grants permission to their photo album the crash happens. It appears the solution for now is to request permission to photos before opening the UIPopoverControl. Here is how I implemented my solution:

    // Photo Library
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        void(^blk)() =  ^() {
            UIImagePickerController* picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            if (NIIsPad()) {
                UIPopoverController* popover = [[UIPopoverController alloc] initWithContentViewController:picker];
                [popover presentPopoverFromBarButtonItem:self.popoverAnchor permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
            } else {
                [self.navigationController presentModalViewController:picker animated:YES];
            }
        };
    
        // Make sure we have permission, otherwise request it first
        ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc] init];
        ALAuthorizationStatus authStatus;
        if (IOS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0"))
            authStatus = [ALAssetsLibrary authorizationStatus];
        else
            authStatus = ALAuthorizationStatusAuthorized;
    
        if (authStatus == ALAuthorizationStatusAuthorized) {
            blk();
        } else if (authStatus == ALAuthorizationStatusDenied || authStatus == ALAuthorizationStatusRestricted) {
            [[UIAlertView alertViewWithTitle:@"Grant photos permission" message:@"Grant permission to your photos. Go to Settings App > Privacy > Photos."] show];
        } else if (authStatus == ALAuthorizationStatusNotDetermined) {
            [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                // Catch the final iteration, ignore the rest
                if (group == nil)
                    dispatch_async(dispatch_get_main_queue(), ^{
                        blk();
                    });
                *stop = YES;
            } failureBlock:^(NSError *error) {
                // failure :(
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[UIAlertView alertViewWithTitle:@"Grant photos permission" message:@"Grant permission to your photos. Go to Settings App > Privacy > Photos."] show];
                });
            }];
        }
    }
    

    Don't forget to add AssetsLibrary.framework to your project.

提交回复
热议问题