5

So, in my app, I want to share something using the UIActivityViewController.

To make sure that the sharing activity was successful, I have this code:

UIActivityViewController *controller =  [[UIActivityViewController alloc]
                                                     initWithActivityItems:@[text, shortURL, image]
                                                     applicationActivities:nil];
[controller setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
          if (! completed){
            // Here I do some stuff irrelevant to the question
        }
    }];

Since I have copied (and modified) this code, I don't want to claim that I completely understand what is happening here.

What I do know, and this is my question, is that the code above runs fine on iOS 8 but not on iOS 7, hardware or simulator.

I dearly hope that somebody can explain to me what is going on here.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Sjakelien
  • 2,255
  • 3
  • 25
  • 43
  • 1
    Did you get any errors? – AstroCB Jan 21 '15 at 21:26
  • Yes, it says: '-[UIActivityViewController setCompletionWithItemsHandler:]: unrecognized selector sent to instance 0x7beb0730' – Sjakelien Jan 21 '15 at 21:27
  • That property isn't available on iOS 7 – Carl Veazey Jan 21 '15 at 21:30
  • I guess you mean iOS7 – Sjakelien Jan 21 '15 at 21:30
  • What specific property, and is there an iOS 7 alternative? – Sjakelien Jan 21 '15 at 21:31
  • I found this: http://stackoverflow.com/questions/26425077/uiactivityviewcontroller-completion-handler-is-completed-when-using-airdrop – Sjakelien Jan 21 '15 at 21:32
  • Which suggests that this should work in both OSs, no? – Sjakelien Jan 21 '15 at 21:33
  • 2
    The property in question is `completionWithItemsHandler` - this exists as of iOS8. iOS7 uses `completionHandler`. Reference: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIActivityViewController_Class/index.html#//apple_ref/occ/instp/UIActivityViewController/completionHandler – Krumelur Jan 21 '15 at 21:35
  • I'm scrambling to apply this knowledge to my piece of code. This question seems to indicate that my syntax should work: http://stackoverflow.com/questions/25999553/determine-which-share-extension-was-used – Sjakelien Jan 21 '15 at 21:41
  • WHOA! Somebody REALLY went anal on the spelling and grammar of my question, by editing out spaces and caps. I think that is pretty rude, especially, since it didn't enhance (the phrasing of) my question in any way. Don't people have anything better to do? – Sjakelien Jan 21 '15 at 23:30

2 Answers2

9

The completionWithItemsHandler property is not available in iOS 7, as it was introduced in iOS 8.

What you're looking for is the now-deprecated completionHandler property; if your deployment target is below iOS 8, you can just use this, but if you want to be future-proof, you can check whether the new handler is supported and, if not, use the old handler:

if([[UIApplication sharedApplication] respondsToSelector:(@selector(setCompletionWithItemsHandler:))]){
        [controller setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
            if(!completed){
                // Out of scope of question
            }
        }];
    }else{
        [controller setCompletionHandler:^(NSString *activityType, BOOL completed) {
            if(!completed){
                // Out of scope of question
            }
        }];
    }
}

Also, you may have omitted this for brevity, but it's important that you actually present the view controller after initializing it:

[self presentViewController:controller animated:YES completion:nil];
AstroCB
  • 12,337
  • 20
  • 57
  • 73
1

OK, so this is what I did. Most likely, Kremelur answered it in general terms, but I am too much of a novice to understand that. So, I copied and pasted some stuff, after some cross-googling. I hope this is of any use to someone.

[controller setCompletionHandler:^(NSString *activityType, BOOL completed) {
        NSLog(@"completed dialog - activity: %@ - finished flag: %d", activityType, completed);
        if (! completed){
            // Out of scope of question
        }
    }];

This code seems to run fine in both iOS7 and iOS8.

Sjakelien
  • 2,255
  • 3
  • 25
  • 43