iOS: crash if sharing with "Message" option
Asked Answered
S

4

6

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)
Straka answered 20/12, 2016 at 1:56 Comment(4)
I made a little sample app to test what you're doing (supporting only the portrait screen orientation), and I'm not really able to reproduce the issue you're citing. I have 2 or 3 apps on the App Store that allow users to share things using UIActivityViewController. They're all portrait only apps, and I've never seen this issue before with Messages, Mail, or any other sharing option. So, my suggestion to you would be to further examine where exactly the app crashes. I think you should reexamine your code and make sure you aren't doing something elsewhere in your code to cause this problem. IKarlotte
Great thanks. The project is complex, trying to see how to simplify the code to make debugging easier.Straka
How hackish are you willing to go? See a hackish proposal in my answer.Lamberto
@LeoNatan hahaha a question you never want to hear on SO ... :) will check your answer and comment there. thanks for your help!Straka
D
1

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:

Displode answered 27/12, 2016 at 15:5 Comment(3)
Thanks Jon! Did you try reproducing it when sharing something with text messaging, or how did you reproduce it? None of the view controllers return values orientation values. We rely on the application's plist to set values globally for the entire app, which works for other apps we have built but not this one for some reason.Straka
This is a comment, not an answer.Lamberto
@LeoNatan he actually helped lead to the solution (posted below) by suggesting to check everywhere for supportedInterfaceOrientations ... in short, it was a stupid stupid stupid dev error.Straka
L
1

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.

Lusaka answered 25/12, 2016 at 16:22 Comment(2)
@LeoNatan question was clarified to specifically ask how the goal can be accomplished without supporting other orientations in Info.plistStraka
@Crashalot. Okay, then don't support other orientation in the Info.plist. Support them in the app delegate's application(_:supportedInterfaceOrientationsFor:) instead.Lusaka
D
1

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:

Displode answered 27/12, 2016 at 15:5 Comment(3)
Thanks Jon! Did you try reproducing it when sharing something with text messaging, or how did you reproduce it? None of the view controllers return values orientation values. We rely on the application's plist to set values globally for the entire app, which works for other apps we have built but not this one for some reason.Straka
This is a comment, not an answer.Lamberto
@LeoNatan he actually helped lead to the solution (posted below) by suggesting to check everywhere for supportedInterfaceOrientations ... in short, it was a stupid stupid stupid dev error.Straka
S
1

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;
}
Smetana answered 28/12, 2016 at 21:43 Comment(2)
thanks again for your help. upvoted! what's confusing is in other apps we can only support portrait in info.plist and have no issues with MFMessageComposeViewController so it's possible to accomplish the goal without overriding MFMessageComposeViewController or changing info.plist. trying to understand the potential causes so as to debug the cause in our app.Straka
thanks for your help! finally tracked down the cause, which was an embarrassingly stupid error. see below if you care, but upvoted your answer anyway. thanks again! btw ... 45.K ... impressive rep!Straka
S
0

Stray code (bad developer, bad developer, bad developer!) elsewhere in the app caused the bug.

An extension of UINavigationController elsewhere in the code overrode supportedInterfaceOrientations and caused everyone here to waste time over a stupid bug. Sorry! Hopefully our sloppiness benefits some future user, though.

Removing the extension fixed the bug.

If SO ever decides to recognize and award the worst developers around, we're happy to stand for nomination. :)

Straka answered 29/12, 2016 at 0:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.