How to detect when Facebook share has been cancelled from UIActivityViewController setCompletionWithItemsHandler - ios

The completion handler seems to work correctly for all the other share types (Twitter, Mail) but for Facebook, the complete BOOL is YES even when you cancel the share (having first tapped the Facebook icon).
UIActivityViewController *shareActivity = [[UIActivityViewController alloc] initWithActivityItems:#[title, url] applicationActivities:nil];
[shareActivity setCompletionWithItemsHandler:^(NSString *activityType, BOOL complete, NSArray *returnedItems, NSError *activityError) {
if (complete) { //Facebook share is always YES }];
Any suggestions?
iOS 8.3, Facebook App installed.

Related

Facebook sharing on iOS works on some devices, but not others

So I am doing just a basic facebook share inside my app, and it works fine on my devices (iPhone 6 OS 9.1 and iPad air 9.02) but doesn't work on my coworkers iPad Air 2 8.4.1 or iPhone 6 Plus 9.1. Anyone have any clue as to why this would be?
They are signed into facebook in the device settings. They also have the facebook app installed (I don't have the app, just signed in through settings)
Here's the screenshot of my device showing the message to share, the other devices just show the image and no text.
Here's the code I am using to share
- (void)shareTapped {
UIImage *image = self.pictureView.image;
NSString *postText = [NSString stringWithFormat:#"%#\n*%#\n%#\n%#, %# %#", self.event.shortDescription, self.event.eventCode, self.event.address, self.event.city, self.event.state, self.event.zip];
[[UtilitiesObject sharedInstance] shareMessage:postText image:image parentVC:self];
}
- (void)shareMessage:(NSString *)msg image:(UIImage *)image parentVC:(UIViewController *)parentVC {
NSArray *activityItems = #[msg];
if (image) {
activityItems = #[msg, image];
}
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
// Tailor the list of services displayed
activityController.excludedActivityTypes = #[UIActivityTypeAssignToContact,
//UIActivityTypePostToFacebook,
//UIActivityTypePostToTwitter,
//UIActivityTypeMail,
UIActivityTypeMessage,
UIActivityTypeSaveToCameraRoll,
UIActivityTypePrint,
UIActivityTypePostToWeibo,
UIActivityTypeCopyToPasteboard];
activityController.popoverPresentationController.sourceView = parentVC.view;
[activityController setCompletionWithItemsHandler:
^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
if ([activityType isEqualToString: UIActivityTypeMail]) {
NSLog(#"Mail");
}
if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
NSLog(#"Facebook");
}
if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
NSLog(#"Twitter");
}
}];
[parentVC presentViewController:activityController animated:YES completion:nil];
}
Facebook's platform policy does not allow apps to prefill the message field. In this case, because you don't have the Facebook app, it's using Apple's share sheet, which allows prefill but is against FB's platform policy. In your friends case, they're likely seeing the Facebook app's share extension, which correctly enforces the policy, and does not allow prefill.

Unlock a feature when the user shares the app via Facebook/Tweeter in iOS, Swift?

Is there such feature that can be un-locked when the user shares my app via Facebook or Tweeter?
Like this:
1) The user clicks on "Share" button within my app
2) My app is then posted(shared or advertised) on the wall of the user's facebook
3) Some feature gets unlocked within my app
You can use the Facebook sharing delegate method to know whether the user has successfully shared OR not.
Below is FB developer link which show various sharing methods,according to your requirements.
https://developers.facebook.com/docs/sharing/ios
Below are the methods in which you can know the sharing status
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results{
//UNLOCK THE APP FEATURE IN THIS METHOD
}
- (void)sharerDidCancel:(id<FBSDKSharing>)sharer{
}
- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error{
NSLog(#"%#",error);
}
See iOS Facebook Tutorial for an example of how its done on the facebook end.
On the app side you can setup a flag on NSUserDefaults or write to the app filesystem to check if the feature is unlocked.
This can be done relatively painlessly using UIActivityViewController and its completion handler.
NSString *message = #"my app is awesome.";
NSURL *link = [NSURL URLWithString:#"awesomeapp.com"];
UIActivityViewController *shareController = [[UIActivityViewController alloc] initWithActivityItems:#[message, link]
applicationActivities:nil];
//add whatever you don't want
shareController.excludedActivityTypes = #[UIActivityTypeMessage];
shareController.completionWithItemsHandler =
^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
//Share wasn't completed, don't unlock
if (!completed || activityError) {
return;
}
//Facebook or Twitter
if ([activityType isEqualToString:UIActivityTypePostToFacebook] ||
[activityType isEqualToString:UIActivityTypePostToTwitter]) {
//Unlock item logic
}
};
[self presentViewController:shareController animated:YES completion:^{
//Whatever
}];

UIActivityViewController completion handler still calls action even if user presses cancel

This is really annoying! Completed is always returned yes even if the user cancels the share option. e.g. I tap on the facebook share and dont share, just tap cancel. Is there a way to track this?
[activityVC setCompletionWithItemsHandler:
^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
if (completed)
{
[GoogleAnalytics trackShareEvents:activityType];
}else
{
[GoogleAnalytics trackEventWithCategory:#"Share" action:#"Failed/Canceled" eventLabel:activityType];
}
}];

App runs fine in iOS 8, but not in iOS 7

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.

Open my app from a FB app status (iOS)

I am trying to open my app via the Facebook app's status but my link shows up as text instead of a link. It does works with both SMS and Email but does not FB or Twitter status'.
I have a URL Scheme set up in my Info.plist.
My link is (appName is my URL Scheme name given)
Also I am calling FB, Twitter, & everything else with
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
[activityController setCompletionHandler:^(NSString *activityType, BOOL completed)
{
bubbleTable.typingBubble = NSBubbleTypingTypeNobody;
NSBubbleData *sayBubble = [NSBubbleData dataWithText:textField.text date:[NSDate dateWithTimeIntervalSinceNow:0] type:BubbleTypeMine];
[bubbleData addObject:sayBubble];
[bubbleTable reloadData];
textField.text = #"";
NSLog(#"%#, %d", activityType, completed);
}];
[self presentViewController:activityController animated:YES completion:nil];
Trying to keep everything native to iOS.
Have a look at https://developers.facebook.com/docs/applinks for your Use Case.

Resources