Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: crash if sharing with "Message" option

Our app only supports portrait mode. Presenting a UIActivityViewController works.

However, sharing with the "Message" option crashes the app:

*** Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [MFMessageComposeViewController shouldAutorotate] is returning YES'

Sharing with another option, such as Facebook Messenger, works.

Solutions from similar SO questions like this one do not work since they suggest supporting all orientations. We only want to support portrait.

1) How can we support the "Message" share option while only supporting portrait orientation, that is while only supporting portrait orientation in Info.plist?

2) Why are we able to support the "Message" share option in other apps with only portrait orientation in Info.plist but not this one? Where should we look for debugging purposes?

    // Define share objects
    let objectsToShare = ["test message"] as [Any]

    // Configure UIActivityViewController
    let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
    activityViewController.excludedActivityTypes =
        [UIActivityType.addToReadingList,
         UIActivityType.assignToContact,
         UIActivityType.print,
         UIActivityType.copyToPasteboard]

    // Define completion handler
    activityViewController.completionWithItemsHandler = doneSharingHandler

    // Show UIActivityViewController
    present(activityViewController, animated: true, completion: nil)
like image 728
Crashalot Avatar asked Dec 20 '16 01:12

Crashalot


3 Answers

I tried for a while to reproduce this bug and could not get it to crash. Finally I was able to get this exact crash when I returned UIInterfaceOrientationPortrait when I should have been returning UIInterfaceOrientationMaskPortrait for one of the orientation functions. Check your view controller's implementation of supportedInterfaceOrientations and your implementation of application:supportedInterfaceOrientationsForWindow:

like image 54
Jon Rose Avatar answered Oct 18 '22 19:10

Jon Rose


Here is a very hackish solution that should work for an app that only presents in portrait.

Create a category over MFMessageComposeViewController and override supportedInterfaceOrientations and shouldAutorotate to only support portrait.

You may need to create this category in Objective C to actually get it to compile, but it will work.

@interface MFMessageComposeViewController (NoRotation) @end
@implementation MFMessageComposeViewController (NoRotation)

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {
    return NO;
}
like image 1
Léo Natan Avatar answered Oct 18 '22 19:10

Léo Natan


All of your individual view controllers can support only portrait, by implementing supportedInterfaceOrientations to return UIInterfaceOrientationPortrait.

But your app, meaning the Info.plist or the app delegate's application(_:supportedInterfaceOrientationsFor:), should support all orientations.

This will allow the runtime to present this MFMessageComposeViewController the way it wants to, but all of your view controller will still be in portrait only, which is what you want.

like image 1
matt Avatar answered Oct 18 '22 17:10

matt