Fix for UIAlertViewController ActionSheet Crash in iPad

This is a common issue with UIAlertViewController in iOS. UIAlertViewController behaves differently in iPad compared to the iPhones. In iPhones UIAlertViewController comes as ActionSheet from below the screen. But in iPad, you have to anchor it the Button that triggered the ActionSheet.

iPad Screenshot

iPhone Screenshot

So you have to add some additional code for iPad to fix it.

here is a sample code that shows two buttons in a ActionSheet.

-(void) showActionSheet :(id)sender {
    
    self.likeActionSheet = [UIAlertController alertControllerWithTitle:LIKE_TITLE message:LIKE_MESSAGE preferredStyle:UIAlertControllerStyleActionSheet];
    
    [self.likeActionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        [self dismissViewControllerAnimated:YES completion:^{}];
    }]];
    
    [self.likeActionSheet addAction:[UIAlertAction actionWithTitle:@"Like" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        [self dismissViewControllerAnimated:YES completion:^{}];
    }]];
    
    [self.likeActionSheet addAction:[UIAlertAction actionWithTitle:@"Not Now" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self dismissViewControllerAnimated:YES completion:^{}];
    }]];
    
    // Check if the Device is an iPhone/iPad //
    if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone) {
        [self.likeActionSheet.popoverPresentationController setPermittedArrowDirections:UIPopoverArrowDirectionUp];
        CGRect rect =  [sender bounds];
        rect.origin.x = self.view.frame.size.width;
        rect.origin.y = rect.origin.y + 50;
        self.likeActionSheet.popoverPresentationController.sourceView = self.view;
        self.likeActionSheet.popoverPresentationController.sourceRect = rect;
    }
   
    // Present action sheet.
    [self presentViewController:self.likeActionSheet animated:YES completion:nil];
}

So for iPad we have to add this extra code to fix the Crash

if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone) {
    [self.likeActionSheet.popoverPresentationController setPermittedArrowDirections:UIPopoverArrowDirectionUp];
    CGRect rect =  [sender bounds];
    rect.origin.x = self.view.frame.size.width;
    rect.origin.y = rect.origin.y + 50;
    self.likeActionSheet.popoverPresentationController.sourceView = self.view;
    self.likeActionSheet.popoverPresentationController.sourceRect = rect;
}