I am working on an app which i run on iPhone works well but when i am trying to run on iPad it crashes

Here is my code:

- (void)parseCountryStates:(NSDictionary *)json
{    
    countryPickerView.hidden = TRUE;
    NSDictionary *listing = [json objectForKey:@"country"];
    countryArray = [listing allValues];
    countryIDArray = [listing allKeys];

    [countryPickerView reloadAllComponents];
    alertController = [UIAlertController
                       alertControllerWithTitle:@"Select Service Type"
                       message:nil
                       preferredStyle:UIAlertControllerStyleActionSheet];
    int count = (int)[countryPickerView numberOfRowsInComponent:0];

    for (int i = 0; i < count; i++)
    {
        UIAlertAction* button = [UIAlertAction
                                 actionWithTitle:[[countryPickerView delegate] pickerView:countryPickerView titleForRow:i forComponent:0]
                                 style:UIAlertActionStyleDefault
                                 handler:^(UIAlertAction * action)
                                 {
                                     countryField.text = [action title];
                                     countryStr = countryField.text;
                                     if ([countryArray containsObject:countryStr]) {
                                         countryidStr = [countryIDArray objectAtIndex:[countryArray indexOfObject:countryStr]];
                                         NSLog(@"CountryidStr %@",countryidStr);
                                         [self getState];
                                     }
                                 }];
        [alertController addAction:button];

    }

    UIAlertAction* cancel = [UIAlertAction
                             actionWithTitle:@"Cancel"
                             style:UIAlertActionStyleCancel
                             handler:^(UIAlertAction * action)
                             {
                                 //  UIAlertController will automatically dismiss the view
                             }];
    [alertController addAction:cancel];
    [self presentViewController:alertController animated:true completion:nil];
}

I am sharing the crash log of it

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController () of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.

3

There are 3 best solutions below

0
On BEST ANSWER

Add source view and source rect to your alertController.

[[alertController popoverPresentationController] setSourceView:self.view];
[[alertController popoverPresentationController] setSourceRect:CGRectMake(0,0,1,1)];
[[alertController popoverPresentationController] setPermittedArrowDirections:UIPopoverArrowDirectionUp];

[self presentViewController:alertController animated:true completion:nil];
0
On

actully on ipad alertcontrollers are not allowed instead you can use pop overs to diaplay kind of alert

Programtically

UIViewController *newViewCont = [[UIViewController  alloc] init];
newViewCont.view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 180, 180)];
newViewCont.modalPresentationStyle = UIModalPresentationPopover;

[self presentViewController:newViewCont animated:YES completion:nil];

UIPopoverPresentationController *pop = [newViewCont popoverPresentationController];
pop.permittedArrowDirections = UIPopoverArrowDirectionAny;

[pop setSourceView:myButton];
[pop setSourceRect:myButton.bounds];

Using storyboards

// grab the view controller we want to show
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"Pop"];

// present the controller
// on iPad, this will be a Popover
// on iPhone, this will be an action sheet
controller.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:controller animated:YES completion:nil];

// configure the Popover presentation controller
UIPopoverPresentationController *popController = [controller popoverPresentationController];
popController.permittedArrowDirections = UIPopoverArrowDirectionUp;
popController.delegate = self;

// in case we don't have a bar button as reference
popController.sourceView = self.view;
popController.sourceRect = CGRectMake(30, 50, 10, 10);

dismiss popover

[self dismissViewControllerAnimated:YES completion:nil];

There’s a new protocol called the UIPopoverPresentationControllerDelegate that is called upon dismissal and position change due to rotation or interface changes. We can even prevent a Popover from being dismissed if we wish. Here are the three methods we can implement:

- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {

// called when a Popover is dismissed
}

- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {

// return YES if the Popover should be dismissed
// return NO if the Popover should not be dismissed
return YES;
}

- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing  _Nonnull *)view {

// called when the Popover changes position
}

Don’t forget to conform to the protocol, and set the delegate to your reacting class.

0
On

UIPopovers are not allowed on iPads, but there is a way you can do this as other answers have indicated. Here is a Swift 5.x version.

let ac = UIAlertController(title: "Some title goes here", message: nil, preferredStyle: .actionSheet)

ac.addAction(UIAlertAction(title: "Some button name", style: .default) {
        [unowned self] _ in
        // stuff to do goes here
        self.doSomething()
    })

// iPad specific code
ac.popoverPresentationController?.sourceView = self.view
let xOrigin = nil // Replace this with one of the lines at the end
let popoverRect = CGRect(x: xOrigin, y: 0, width: 1, height: 1)
ac.popoverPresentationController?.sourceRect = popoverRect
ac.popoverPresentationController?.permittedArrowDirections = .up

present(ac, animated: true)

Replacing the the let xOrigin = nil line with one of the ones below will control where the popover appears below the navigation bar. You can also change x and y to the proper value in the bounds or frame of a different element if you have control that is below the nav bar on an iPad.

Top Left

 let xOrigin = 0

Top Middle

let xOrigin = self.view.bounds.width / 2

Top Right

let xOrigin = self.view.bounds.width

Hope this helps.