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.
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];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With