15

I am using UIActivityViewController to share a article url, using the below code,

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:shareItemArray applicationActivities:nil];

activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAirDrop]; 

[self presentViewController:activityVC animated:YES completion:nil];

The above code working in iOS 9 & 10, but it is not working in iOS 11

I debugged it, the code is executing fine, after that i am getting a error log in console like below

[ShareSheet] ERROR: timed out waiting to establish a connection to the ShareUI view service extension.

Is there any people facing this problem? Is there a workaround?

EDIT: After clean and run the project, i tried to do the same, after 2 or 3 times the UIActivityViewController got opened, again i tried, for the first time i am getting the same above error, second time only the UIActivityViewController is getting opened.

R. Mohan
  • 2,182
  • 17
  • 30
  • clean and run once, are you sure u faced the issue of `timed out waiting to establish a connection to the ShareUI view service extension.` of this after used the `UIActivityViewController` – Anbu.Karthik Sep 25 '17 at 07:12
  • can you show the `shareItemArray` – Anbu.Karthik Sep 25 '17 at 07:15
  • clean and runned it bro, i got the same timed out error 2 or 3 times, after that uiactivityviewcontroller is opening now. – R. Mohan Sep 25 '17 at 07:24
  • In that shareItemArray i am having the below NSUrl only "http://www.60secondsnow.com/ta/tamil-nadu/opening-water-from-mullaperiyar-dam-627610.html" – R. Mohan Sep 25 '17 at 07:27
  • i think is the problem is somewhere else – Anbu.Karthik Sep 25 '17 at 07:28
  • i tried https://github.com/versluis/Activity-Demo this project too in iOS 11, in that project also i got the same issue. After that only i posted this question. – R. Mohan Sep 25 '17 at 07:31
  • ok i wil check this – Anbu.Karthik Sep 25 '17 at 07:34
  • Can anyone clarify why the error is logged, but the UI simply appears to not respond? Assuming you want to keep it in the main thread, is there something missing, that would trap the error? – benc Jul 16 '18 at 08:04
  • @benc just add the code into the main thread as per the answer, it will work. – R. Mohan Jul 16 '18 at 08:49

4 Answers4

8

All code that modifies the user interface needs to be run in the main queue, that's by design. So all you need to do is this:

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 little more explanation: dispatch_async() will enqueue the block you pass to it to run in a near future when it will be safe to update the UI. If you don't do that on the main queue, weird things can happen (sometimes the UI simply doesn't change, or old changes can be applied after newer ones, or stuff like that).

Methods that are messaged from user actions in the UI (for example, in IBAction when you tap a button) already run on the main queue, so dispatch_async() is not necessary there (of course, assuming you're not calling those IBActions manually from another queue).

Note that alloc/init and setExcludedActivityTypes: don't update the UI, so there's no need to call those on the main queue.

Alejandro Iván
  • 3,969
  • 1
  • 21
  • 30
  • Hey @Alejandro Iván I'm getting crash when open shareing view and getting this error in log, timed out waiting to establish a connection to the ShareUI view service extension. Can you please help here? – Samkit Shah Dec 01 '17 at 07:10
  • @SamkitShah I have no idea, I would assume some system-level services are failing. Try rebooting your device (Power+Home on iPhone 6s or fewer, Power+VolumeDown on iPhone 7 or newer, let the buttons go when you see the Apple startup logo). – Alejandro Iván Dec 05 '17 at 19:13
8

For swift 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);
}
vp2698
  • 1,783
  • 26
  • 28
1

Try this:

    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 same problem, but in Swift3 and it works fine.

Shaunak Shukla
  • 2,347
  • 2
  • 18
  • 29
0

I was with the same problem, and I figured out that I was not calling in UI Thread. Try to run in UI Thread and see if fix the problem