UIActivityViewController not working in iOS 11

I am using UIActivityViewController to share the article URL using the code below,

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:shareItemArray applicationActivities:nil]; activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAirDrop]; [self presentViewController:activityVC animated:YES completion:nil]; 

The code above works on iOS 9 and 10, but it does not work on iOS 11

I debugged it, the code runs fine, after that I get an error log in the console, as shown below

[ShareSheet] ERROR: timeout to establish a connection with the ShareUI viewer service extension.

Are there any people faced with this problem? Is there any workaround?

EDIT: after cleaning and starting the project, I tried to do the same thing, after 2 or 3 times the UIActivityViewController was open, again I tried to get the same error the first time above, the second time only the UIActivityViewController opens.

+13
ios objective-c iphone uiactivityviewcontroller xcode9
source share
5 answers

All code that changes the user interface must be run in the main queue, which is by design. So all you have to do is the following:

 UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:shareItemArray applicationActivities:nil]; activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAirDrop]; dispatch_async(dispatch_get_main_queue(), ^{ [self presentViewController:activityVC animated:YES completion:nil]; }); 

A bit more explanation: dispatch_async() will close the block that you pass it to it to start in the near future when it will be safe to update the user interface. If you do not do this in the main queue, strange things may happen (sometimes the user interface just does not change, or old changes can be applied after newer ones, or such things).

Methods that are transferred from user actions in the user interface (for example, to IBAction when a button is clicked) are already executed in the main queue, so dispatch_async() is not required there (of course, unless you manually call them IBAction from another queue).

Note that alloc / init and setExcludedActivityTypes: do not update the user interface, so there is no need to call them in the main queue.

+8
source share

For quick 4.0

 let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [shareItem], applicationActivities:nil) activityViewController.excludedActivityTypes = [.print, .copyToPasteboard, .assignToContact, .saveToCameraRoll, .airDrop] DispatchQueue.main.async { self.present(activityViewController, animated: true, completion: nil); } 
+7
source share

Try the following:

  dispatch_async(dispatch_get_main_queue(), ^{ UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:shareItemArray applicationActivities:nil]; activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAirDrop]; dispatch_async(dispatch_get_main_queue(), ^{ [self presentViewController:activityVC animated:YES completion:nil]; }); }); 

I had the same problem, but in Swift3 and it works fine.

+1
source share

I had the same problem and realized that I was not calling the UI theme. Try running in the user interface thread and see if the problem is fixed

0
source share

Does this work for third party applications? I can not exclude third-party applications.

0
source share

All Articles