2

I have an app that has a share button. This share button loads a UIActivityViewController for sharing to Facebook, Twitter, email, text message, etc.

It used to work fine, and I think it still works fine on the simulator, but on devices, the view controller appears with all the right options, and if you click on one, either nothing happens, or if it's Mail, the mail modal view loads and then dismisses itself. Then I get my log "Activity was not performed.", which is when the completion block returns false for completed but the activityType was not null. So it is recognizing the selection, but it isn't loading the activity into the view for some reason

I have checked the stuff I'm trying to share, even replaced it with dummy stuff (as shown below), still no luck. I am using a normal device, I have my Twitter, Mail, and Facebook accounts set up, texting works too. The only thing that works is copy (i.e. when you copy the share contents to the clipboard). In other apps on the same device, the UIActivityViewController and the loading of selected activities works just fine. Same issue observed on other devices running the app as well.

Really don't understand what the issue is here. Very perplexing! Any help or suggestions of things to try would be much appreciated. I don't see any way to debug this issue.

Here's the code: (note I tried removing the image as well, no luck)

- (void)shareTapped {

    NSString *shareText = @"Testing";//[self shareText];

    NSURL *url = [NSURL URLWithString:@"http://www.google.ca"]; //[self shareURL];
    NSArray *activityItems = [NSArray arrayWithObjects:shareText,url, self.shareImage, nil];

    UIActivityViewController *shareDrawer = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
shareDrawer.excludedActivityTypes = @[UIActivityTypePostToWeibo,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll,UIActivityTypePrint];

    shareDrawer.completionHandler = ^(NSString *activityType, BOOL completed) {
        if (completed) {
            NSLog(@"Selected activity was performed.");
        } else {
            if (activityType == NULL) {
                NSLog(@"User dismissed the view controller without making a selection.");
            } else {
                NSLog(@"Activity was not performed.");
            }
        }

        NSString *result = completed ? @"success" : @"fail";
        if (activityType == NULL) {
            result = @"dismissed";
        }
    };
    [self presentViewController:shareDrawer animated:YES completion:nil];
}
shim
  • 9,289
  • 12
  • 69
  • 108
  • have you tried instead of adding the data as separate elements in the array, implementing the UIActivityItemSource protocol on an object that is in the array? not sure if it really makes a different in your case as it sounds like where its failing is in presenting the view, but I guess its worth a shot to change up how the data is fed. – bolnad Jan 30 '14 at 02:09
  • I put pretty much the exact same code into a different, much simpler app I'm working on, and it's working fine... What could be messing this up in my app? – shim Feb 07 '14 at 22:51
  • Also tried putting a UIActivityViewController inside another view controller of my problem app and it did not work. – shim Feb 07 '14 at 23:05
  • I tried adding a blank view controller in that just has a share button and it triggers the UIActivityViewController, and it works fine, so there must be some conflict within my view controllers... – shim Feb 07 '14 at 23:10
  • (i.e. that was setting the blank view controller as the root view) If I set that same view controller to be pushed after the original main one, then the sharing doesn't work again. So something screwy with the stack. – shim Feb 07 '14 at 23:17
  • 1
    I've also noticed strange behaviors from the activity view controller, so i don't doubt that you ran into some wonkiness that can't be explained easily. Also the lack of documentation on it, kind of threw me for a loop for while. Glad you found your solution! – bolnad Feb 09 '14 at 14:50

2 Answers2

1

OK, by process of elimination I finally narrowed down the culprit:

I have a custom UISegmentedControl with a custom font for the text and I manually adjusted the content offset to make it appear properly. Although I have removed these lines and the segmented control actually looks fine.

Here's the code (I've confirmed that it's all three lines that cause the problem)

    [[UISegmentedControl appearance] setContentPositionAdjustment:UIOffsetMake(4, 0) forSegmentType:UISegmentedControlSegmentLeft barMetrics:UIBarMetricsDefault];
    [[UISegmentedControl appearance] setContentPositionAdjustment:UIOffsetMake(0, 0) forSegmentType:UISegmentedControlSegmentCenter barMetrics:UIBarMetricsDefault];
    [[UISegmentedControl appearance] setContentPositionAdjustment:UIOffsetMake(-4, 0) forSegmentType:UISegmentedControlSegmentRight barMetrics:UIBarMetricsDefault];

Now, seeing as the views that popup after you select a share option DO NOT HAVE SEGMENTED CONTROLS in them, I have no clue why this would cause this problem. But it definitely works now that I removed it.

Thanks for those of you who attempted to help. Of course, there's no way you could have possibly guessed this was the issue. Chances are pretty low that anyone would ever encounter this problem in the first place, since adjusting the content position is probably fairly rare.

To debug this issue I followed the following steps:

  1. I tested if the issue had something to do with the properties on the UIActivityViewController that I was setting, or the activity items I was sharing. It did not.
  2. I tested if UIActivityViewController worked properly when called from a different view controller inside my app. It did not.
  3. I made a blank view controller with a button that causes a generic UIActivityViewController to show on press of a bar button. I made that my root view. That worked, thus showing that it was an isolated issue.
  4. Instead of making that view controller the root of my app, I pushed it from my normal main view. It no longer worked, thus determining that the problem probably had something to do with my main view.
  5. I commented out all the code in viewDidLoad, viewDidAppear, and viewWillAppear, except for adding a button to the nav bar which would load my sharing test. That worked. Then I uncommented viweDidAppear and viewWillAppear, still worked. So I uncommented chunks of viewDidLoad until I figured out exactly what the problem was.

What I learned: For weird problems like this (i.e. ones that seem like an iOS bug or something, but you can't find anyone posting about it), you should spend more time debugging before trying to post to stack overflow. (I should really know this by now, but every new problem to debug feels like an exception to this rule)

Please comment below if you know why adjusting the content position of UISegmentedControl would mess up the sharing from UIActivityViewController even though those views don't contain segmented controls

Thanks

shim
  • 9,289
  • 12
  • 69
  • 108
0

Had same issue too, here's what I done to overcome it.

I'd recommend adding the Social.framework to your Link Binary and using

SLComposeViewController *socialShare = [SLComposeViewController composeViewControllerForServiceType:shareType];

Where the shareType can be SLServiceTypeFacebook or SLServiceTypeTwitter.

I created a UIAlertview pop-up to show the share options for the user

    UIAlertView *social = [[UIAlertView alloc]initWithTitle:@"Share" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Twitter", @"Facebook", @"Email", nil];

and depending on which one they picked (using the UIAlertview Delegate methods) then the content can be shared accordingly, and it's a great alternative to UIActivityViewController.

Hope this suggestion helps, cheers, Jim.

Jim Tierney
  • 4,078
  • 3
  • 27
  • 48
  • Thanks but not really a solution. Plus I don't want to limit it to just Facebook and Twitter. – shim Jan 28 '14 at 02:47
  • I understand, it is more limited, though for people looking for FB, Twitter or email (email's not SLC but can be easily added as option to alert view) then I hope this is a good alternative. Hope you managed to get it sorted. Cheers – Jim Tierney Jan 28 '14 at 02:54
  • Doesn't even attempt solve the problem, and I didn't ask for alternatives; asked for things to try (i.e. to fix the problem). – shim Feb 07 '14 at 22:59