Links

UIActionSheet in iOS 8

UIActionSheet and UIActionSheetDelegate is deprecated in iOS 8. To create and manage action sheets in iOS 8 / later, we need to use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet because if we have used the same in our previous code it will simply not appear in iOS 8. Don't panic! It won't make a crash ;)
PFB - A quick adaptation for UIActionSheet/Delegate

till iOS 7


#pragma mark
#pragma mark Show options to pick image
-(void)pickImage:(UIButton*)sender{
    UIActionSheet *changeImageAction = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:
                            @"Capture Photo",
                            @"Select from Gallery",
                            nil];
    [changeImageAction showInView:[UIApplication sharedApplication].keyWindow];
}

#pragma mark
#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)popup clickedButtonAtIndex:(NSInteger)buttonIndex {

            switch (buttonIndex) {
                case 0:
                    NSLog(@"Open camera");
                    if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ){
                        [self getImage:UIImagePickerControllerSourceTypeCamera];
                    }
                    break;
                case 1:
                    NSLog(@"Open gallery");
                    [self getImage:UIImagePickerControllerSourceTypePhotoLibrary];
                    break;
                default:
                    break;
            }

}

from iOS 8


#pragma mark
#pragma mark Show options to pick image
-(void)pickImage:(UIButton*)sender{

    
    
    UIAlertController *alertController = [UIAlertController
                                          alertControllerWithTitle:@"Change image"
                                          message:@""
                                          preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancelAction = [UIAlertAction
                                   actionWithTitle:@"Capture Photo"
                                   style:UIAlertActionStyleDefault
                                   handler:^(UIAlertAction *action)
                                   {
                                       NSLog(@"Open camera");
                                       if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ){
                                           [self getImage:UIImagePickerControllerSourceTypeCamera];
                                       }
                                   }];
    
    UIAlertAction *okAction = [UIAlertAction
                               actionWithTitle:@"Select from Gallery"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction *action)
                               {
                                   NSLog(@"Open gallery");
                                   [self getImage:UIImagePickerControllerSourceTypePhotoLibrary];
                               }];
    
    
    
    
    UIPopoverPresentationController *popover = alertController.popoverPresentationController;
    
    if (popover)
    {
        popover.sourceView = sender;
        popover.sourceRect = sender.bounds;
        popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
    }
    
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

Post a Comment