Thursday, 12 March 2015

UIImagePickerController has iPad issue in iOS 8 : Attempt to present on which is already presenting (null)


I think this is because in iOS 8, alert views and action sheets are actually presented view controllers (UIAlertController). So, if you're presenting a new view controller in response to an action from the UIAlertView, it's being presented while the UIAlertController is being dismissed. I worked around this by delaying the presentation of the UIImagePickerController until the next iteration of the runloop, by doing this:

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    // just add picker code here and it will work fine.
}];



- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        if (buttonIndex==0)
        {
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                NSArray *media = [UIImagePickerController
                                  availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
                
                if ([media containsObject:(NSString*)kUTTypeImage] == YES)
                {
                    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                    [picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]];
                    picker.delegate = (id)self;
                    [self presentViewController:picker animated:YES completion:nil];
                    
                }
            }
            else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unsupported!"
                                                                message:@"Camera does not support photo capturing."
                                                               delegate:nil
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil];
                [alert show];
            }
            
            
        }
        else if (buttonIndex==1)
        {
            
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
            {
                NSArray *media = [UIImagePickerController
                                  availableMediaTypesForSourceType: UIImagePickerControllerSourceTypePhotoLibrary];
                
                if ([media containsObject:(NSString*)kUTTypeImage] == YES)
                {
                    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
                    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    [picker setMediaTypes:[NSArray arrayWithObject:(NSString *)kUTTypeImage]];
                    picker.delegate = (id)self;
                    [self presentViewController:picker animated:YES completion:nil];
                    
                }
                
                
            }
            else
            {
                [msgDic setValue:@"Sorry" forKey:@"title"];
                [msgDic setValue:@"Your device doesn't posses Photo Library." forKey:@"message"];
                
                [self performSelectorOnMainThread:@selector(showAlert:) withObject:msgDic waitUntilDone:NO];
            }
        }
    }];
    
    
}



Here is the SO answer.

1 comment:

  1. so many thanks to you . it is realy help full. er.ravindrapal@gmail.com

    ReplyDelete