Action Sheet issue on iPad – Swift iOS Programming
Error Got in XCode 7 while using Swift2.x:
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.’
I faced above error on iPad while application was working fine on the iPhone. Client complained when he clicked the option to upload an image, application crashed.
Information from client was sufficient to help me debug where exactly the issue can be. On click to upload image, my application has options to select source of image, either use camera to click a photo or use phone’s library to select an existing image. So show these options, I was using ‘UIAlertController‘ and parameter of ‘PreferredStyle‘ was selected as ‘ActionSheet‘. It was new for me to know ‘ActionSheet’ was causing error in iPad and it caused application to crash.
I changed the parameter value of ‘preferredStyle‘ to ‘UIAlertControllerStyle.Alert‘ and it fixed the error that was caused by ‘UIAlertControllerStyle.ActionSheet‘.
I hope this will help someone to fix the ipad issue. If there is some other reason for your app to crash on ipad or iphone or you want to write something about this post, please write in the comments.