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)
Community
  • 1
  • 1
Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • How hackish are you willing to go? See a hackish proposal in my answer. – Léo Natan Dec 28 '16 at 22:00
  • @LeoNatan hahaha a question you never want to hear on SO ... :) will check your answer and comment there. thanks for your help! – Crashalot Dec 28 '16 at 22:21
  • 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. I – pranjal Dec 27 '16 at 02:04
  • Great thanks. The project is complex, trying to see how to simplify the code to make debugging easier. – Crashalot Dec 27 '16 at 04:47

4 Answers4

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • @LeoNatan question was clarified to specifically ask how the goal can be accomplished without supporting other orientations in Info.plist – Crashalot Dec 28 '16 at 21:23
  • @Crashalot. Okay, then don't support other orientation in the _Info.plist_. Support them in the app delegate's `application(_:supportedInterfaceOrientationsFor:)` instead. – matt Dec 28 '16 at 22:42
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:

Jon Rose
  • 8,373
  • 1
  • 30
  • 36
  • 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. – Crashalot Dec 27 '16 at 21:10
  • This is a comment, not an answer. – Léo Natan Dec 28 '16 at 18:47
  • @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. – Crashalot Dec 29 '16 at 00:41
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;
}
Léo Natan
  • 56,823
  • 9
  • 150
  • 195
  • 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. – Crashalot Dec 28 '16 at 22:22
  • 1
    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! – Crashalot Dec 29 '16 at 00:43
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. :)

Crashalot
  • 33,605
  • 61
  • 269
  • 439