IOS: crash when sharing with the Message option

Our application supports portrait mode only. Represents the operation of the UIActivityViewController.

However, sharing with the "Message" option causes the application to crash:

*** Application termination due to the uncaught exception "UIApplicationInvalidInterfaceOrientation", reason: "Supported orientations do not have a common orientation with the application, and [MFMessageComposeViewController shouldAutorotate] returns YES '

Another option works, for example, Facebook Messenger.

Solutions from similar SO issues like this one do not work, as they offer support for all orientations. We want to support only a portrait.

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

2) Why can we support the "Message" option in other applications only with portrait orientation in Info.plist, but not with this? Where should we look for debugging goals?

// 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) 
+6
source share
4 answers

I tried to reproduce this error for some time and could not destroy it. Finally, I managed to get this exact crash when I returned the UIInterfaceOrientationPortrait , when I had to return the UIInterfaceOrientationMaskPortrait for one of the orientation functions. Check the controller implementation of your view supportedInterfaceOrientations and your implementation of application:supportedInterfaceOrientationsForWindow:

+1
source

All your individual controllers can only support portrait by implementing supportedInterfaceOrientations to return a UIInterfaceOrientationPortrait .

But your application, i.e. Info.plist or application delegate application(_:supportedInterfaceOrientationsFor:) , should support all orientations.

This will allow the runtime to present the MFMessageComposeViewController the way it wants, but all your controllers will still only be in the portrait, which is what you need.

+1
source

Here is a very hacky solution that should work for an application that is presented only in the portrait.

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

You may need to create this category in Objective-C to get it for compilation, but it will work.

 @interface MFMessageComposeViewController (NoRotation) @end @implementation MFMessageComposeViewController (NoRotation) - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } - (BOOL)shouldAutorotate { return NO; } 
+1
source

Wandering code (poor developer, poor developer, poor developer!) Elsewhere in the application caused an error.

Extending the UINavigationController elsewhere in the code overrode supportedInterfaceOrientations and made everyone here waste time on a stupid mistake. Sorry I hope our negligence will benefit the future user.

Removing the extension fixed the error.

If SO ever decides to recognize and reward the worst developers around, we will be happy to stand for the nomination. :)

0
source

Source: https://habr.com/ru/post/1013318/


All Articles