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)
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:
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With