Unable to post an article with Facebook SDK on iOS with UIActivityViewController? - ios

Building an iOS app for reading articles. I’m using Facebook SDK to share the article link and image on Facebook.
This is the process:
1.After clicking on bar button item on top right, UIActivityViewController gets opened on the bottom of the screen which gives option to share via fb, google , linked in etc.
2.On clicking the Facebook button in UIActivityVIewController, default screen opens for sharing but ideally fb SDK code should get executed.
The following "if condition” does not executed.
[AVC setCompletionHandler:^(NSString *activityType, BOOL completed)
{
if([activityType isEqualToString: UIActivityTypePostToFacebook]){
FBLinkShareParams *params = [[FBLinkShareParams alloc] init];
3.But when clicked on the cancel button of the default screen, “if condition" gets executed and app is able to share the article as expected.
Here is the code.
- (IBAction)ysshareAction:(id)sender {
NSURL *Imageurl = [NSURL URLWithString:_DetailModal1[2]];
NSData *data = [NSData dataWithContentsOfURL:Imageurl];
UIImage *image = [[UIImage alloc] initWithData:data];
NSURL *linkURL = [NSURL URLWithString:_DetailModal1[4]];//article url
NSMutableAttributedString *stringText = [[NSMutableAttributedString alloc] initWithString:_DetailModal1[0]];//_DetailModal1[0] contain article title////
[stringText addAttribute:NSLinkAttributeName value:linkURL range:NSMakeRange(0, stringText.length)];
NSArray *itemArrany = #[stringText,image,];//title is displayed but not as hyperlink.
UIActivityViewController *AVC = [[UIActivityViewController alloc] initWithActivityItems:itemArrany applicationActivities:nil];
AVC.excludedActivityTypes=#[];
[AVC setCompletionHandler:^(NSString *activityType, BOOL completed)
{
if([activityType isEqualToString: UIActivityTypePostToFacebook]){
FBLinkShareParams *params = [[FBLinkShareParams alloc] init];
params.link = [NSURL URLWithString:#"https://www.youtube.com/watch?v=pa8lsBNG31c"];
// // If the Facebook app is installed and we can present the share dialog
if ([FBDialogs canPresentShareDialogWithParams:params]) {
// Present share dialog
[FBDialogs presentShareDialogWithLink:params.link
handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if(error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"Error publishing story: %#", error.description);
} else {
// Success
NSLog(#"result %#", results);
}
}];
// If the Facebook app is NOT installed and we can't present the share dialog
} else {
// FALLBACK: publish just a link using the Feed dialog
// Put together the dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"YourStory", #"name",
_DetailModal1[0], #"caption",
_DetailModal1[4], #"link",
_DetailModal1[2], #"picture",
nil];
// Show the feed dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"Error publishing story: %#", error.description);
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User canceled.
NSLog(#"User cancelled.");
} else {
// Handle the publish feed callback
// NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
// if (![urlParams valueForKey:#"post_id"]) {
// User canceled.
NSLog(#"User cancelled.");
// } else {
// User clicked the Share button
// NSString *result = [NSString stringWithFormat: #"Posted story, id: %#", [urlParams valueForKey:#"post_id"]];
//
}
}
}];
}
}
}];
[self presentViewController:AVC animated:YES completion:nil];
}
Any help is really appreciated.

Related

Facebook share shows blank image on iOS but works fine on desktop

I am using AddThis from the following page: http://www.douglasstratton.com/Beaches/Amnesia/flypage.tpl.html
When sharing from the desktop, the image shares fine, but when sharing from iOS, the image is blank on the facebook wall.
Has anyone else had this issue?
You can share with this code.Only import #import <FacebookSDK/FacebookSDK.h>:-
// Put together the dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
APP_NAME, #"name",
self.strSerialName , #"caption",
modelObject.strChapterDescription, #"description",
#"", #"link",
modelObject.strChapterImage, #"picture",
nil];
// Show the feed dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"Error publishing story: %#", error.description);
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User cancelled.
NSLog(#"User cancelled.");
} else {
// Handle the publish feed callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:#"post_id"]) {
// User cancelled.
NSLog(#"User cancelled.");
} else {
// User clicked the Share button
NSString *result = [NSString stringWithFormat: #"Posted story, id: %#", [urlParams valueForKey:#"post_id"]];
NSLog(#"result %#", result);
}
}
}
}];

I want to share an image that is within the application bundle on facebook without url using FBWebDialogs?

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: #"The mechanic has done a fine job improving my vehicle…", #"name", #"Click below to download this awesome app for FREE and challenge me", #"description",iTunesLink, #"link", img, #"picture", nil];
//step 2
// Invoke the dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil parameters:params handler:
^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// Error launching the dialog or publishing a story.
//nslog(#"Error publishing story.");
} else
{
if (result == FBWebDialogResultDialogNotCompleted)
{
// User clicked the "x" icon
//nslog(#"User canceled story publishing.");
}
else
{
// Handle the publish feed callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:#"post_id"])
{
// User clicked the Cancel button
//nslog(#"User canceled story publishing.");
} else
{
// User clicked the Share button
//NSString *msg = [NSString stringWithFormat: #"Posted story, id: %#", [urlParams valueForKey:#"post_id"]];
// Show the result in an alert
isSharedButtonTapped = YES;
self.btnFacebookShare.userInteractionEnabled = NO;
[[[UIAlertView alloc] initWithTitle:#"Success!" message:#"Successfully shared to Facebook" delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil] show];
}
}
}
}];
If I'm providing the picture as "URL" its posting fine. Else its throwing error. I want to post an image that is within the application bundle. Can some on tell me where I'm coding wrong?
`NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Sharing Tutorial", #"name",
#"Build great social apps and get more installs.", #"caption",
#"Allow your users to share stories on Facebook from your app using the iOS SDK.", #"description",
#"https://developers.facebook.com/docs/ios/share/", #"link",
#"http://i.imgur.com/g3Qc1HN.png", #"picture",
nil];`
[FBDialogs presentShareDialogWithPhotoParams:params
clientState:nil
handler:^(FBAppCall *call,
NSDictionary *results,
NSError *error) {
if (error) {
} else {
//share successdialog
}
}];
You can use something like this
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
FBPhotoParams *params = [[FBPhotoParams alloc] init];
// Note that params.photos can be an array of images. In this example
// we only use a single image, wrapped in an array.
params.photos = #[img];
[FBDialogs presentShareDialogWithPhotoParams:params
clientState:nil
handler:^(FBAppCall *call,
NSDictionary *results,
NSError *error) {
if (error) {
NSLog(#"Error: %#",
error.description);
} else {
NSLog(#"Success!");
}
}];
}
For more details you can check here.
However this will add the shared image to Photos which would not be the way you want it to be. It will create a folder with the name of the app and the image will be added under this.

FBWebDialogs does not show - no reaction

Please if any of you could help me with this Facebook SDK. I don't know what's going on, and the problem is that when button with facebook is clicked nothing shows up - FBWebDialog is dead but "Memory managment/consumption bar in Xcode when app is fired allocs additional memory (about 22 MB) after that action. I have followed official tutorial: https://developers.facebook.com/docs/ios/share and reainstalled facebook SDK, added things like AppID etc. Even that haven't helped me. What's wrong with that?
I'm using iphone simulator and ios 8 and Xcode 6.0. About 2 mounths ago everything worked fine. Pop-up window was showed in application until now.
Any help with this will be appreciated.
UPDATE:
MY CODE:
[FBLoginView class];
FBLinkShareParams *params = [[FBLinkShareParams alloc] init];
params.link = [NSURL URLWithString:#"https://developers.facebook.com/docs/ios/share/"];
// If the Facebook app is installed and we can present the share dialog
if ([FBDialogs canPresentShareDialogWithParams:params]) {
// Present the share dialog
[FBDialogs presentShareDialogWithLink:params.link
handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if(error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"Error publishing story: %#", error.description);
} else {
// Success
NSLog(#"result %#", results);
}
}];
} else {
// Present the feed dialog
// Put together the dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Sharing Tutorial", #"name",
#"Build great social apps and get more installs.", #"caption",
#"Allow your users to share stories on Facebook from your app using the iOS SDK.", #"description",
#"https://developers.facebook.com/docs/ios/share/", #"link",
#"http://i.imgur.com/g3Qc1HN.png", #"picture",
nil];
// Show the feed dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"Error publishing story: %#", error.description);
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User cancelled.
NSLog(#"User cancelled.");
} else {
// Handle the publish feed callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:#"post_id"]) {
// User cancelled.
NSLog(#"User cancelled.");
} else {
// User clicked the Share button
NSString *result = [NSString stringWithFormat: #"Posted story, id: %#", [urlParams valueForKey:#"post_id"]];
NSLog(#"result %#", result);
}
}
}
}];
}
Standard code from Facebook site but still no reaction.

Share app through facebook is not working

Hi in my Application i have option of sharing my application through Facebook. So I have added the Facebook framework to my project and I have added the bundle id into my Facebook and got the App id but the problem is I'm not Unable to share the App its showing like this.
An error occurred. Please try again later
In my Stimulator is showing like this i don't why it's showing like that. If my device have Facebook App its working fine but if it doesn't have the Facebook App its showing error like this. Please tell how to resolve this issue.
- (IBAction)share:(id)sender {
FBLinkShareParams *params = [[FBLinkShareParams alloc] init];
params.link = [NSURL URLWithString:#"url/"];
// If the Facebook app is installed and we can present the share dialog
if ([FBDialogs canPresentShareDialogWithParams:params]) {
// Present share dialog
[FBDialogs presentShareDialogWithLink:params.link
handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if(error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"Error publishing story: %#", error.description);
} else {
// Success
NSLog(#"result %#", results);
}
}];
// If the Facebook app is NOT installed and we can't present the share dialog
} else {
// FALLBACK: publish just a link using the Feed dialog
// Put together the dialog parameters
// Put together the dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"My appname", #"name",
#"test share.", #"caption",
#"sample.", #"description",
#"myrul", #"link",
nil];
// Show the feed dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"Error publishing story: %#", error.description);
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User canceled.
NSLog(#"User cancelled.");
} else {
// Handle the publish feed callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:#"post_id"]) {
// User canceled.
NSLog(#"User cancelled.");
} else {
// User clicked the Share button
NSString *result = [NSString stringWithFormat: #"Posted story, id: %#", [urlParams valueForKey:#"post_id"]];
NSLog(#"result %#", result);
}
}
}
}];
}
}
- (NSDictionary*)parseURLParams:(NSString *)query {
NSArray *pairs = [query componentsSeparatedByString:#"&"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *pair in pairs) {
NSArray *kv = [pair componentsSeparatedByString:#"="];
NSString *val =
[kv[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
params[kv[0]] = val;
}
return params;
}
I have used the above code for its not working please help me how to resolve this issue.
Thanks.
Use this code for Share App.
-(void)facebook
{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"Cancelled");
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Congratulations!" message:#"Successfully posted to facebook Wall." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[Flurry logEvent:#"Share screen - Successfully posted on Facebook"];
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler =myBlock;
NSString *str=[NSString stringWithFormat:#" I am using App for my iPhone. You too can download it from here"];
[controller setInitialText:str];
[self presentViewController:controller animated:YES completion:Nil];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry!"message:#"Please add Facebook account in settings menu" delegate:self cancelButtonTitle:Nil otherButtonTitles:#"Ok", nil];
alert.tag=100;
[alert show];
}
}
For use of SLComposeViewController,you have to add Social Framework. It fetch your facebook account from setting and you can share your app link through SLComposeViewController. and
if you want to add images or URL to in than :
[composeController addImage:[UIImage imageNamed:#"image.png"]];
[composeController addURL: [NSURL URLWithString:#"apple.com"]];

Mysterious magnifying glass in Facebook Feed Dialog

I installed Facebook iOS SDK via CocoaPods with Podfile
platform :ios, '6.0'
pod 'Facebook-iOS-SDK'
Screenshot is as follow:
A magnifying glass appears on the top right corner in Simulator, when I use the following code (in a button's IBAction) to attempt to share a message to Facebook (I follow the tutorial at Getting Started):
FBShareDialogParams *params = [[FBShareDialogParams alloc] init];
params.link = [NSURL URLWithString:#"http://www.google.com"];
params.name = #"Google";
params.caption = #"Google is Great";
params.picture = [NSURL URLWithString:#"http://i.imgur.com/g3Qc1HN.png"];
params.description = #"Google is a good search engine";
if([FBDialogs canPresentShareDialogWithParams:params]) {
// Present share dialog (requires Facebook App v6.0+ installed)
[FBDialogs presentShareDialogWithLink:params.link
name:params.name
caption:params.caption
description:params.description
picture:params.picture
clientState:nil
handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if(error) {
NSLog(#"%#", [NSString stringWithFormat:#"Error publishing story: %#", error.description]);
} else {
NSLog(#"result %#", results);
}
}];
} else {
// Present feed dialog
NSMutableDictionary *params1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Google", #"name",
#"Google is Great", #"caption",
#"Google is a good search engine", #"description",
#"http://www.google.com", #"link",
#"http://i.imgur.com/g3Qc1HN.png", #"picture",
nil];
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params1
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"%#",[NSString stringWithFormat:#"Error publishing story: %#", error.description]);
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User cancelled.
NSLog(#"User cancelled.");
} else {
// Handle the publish feed callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:#"post_id"]) {
// User cancelled.
NSLog(#"User cancelled.");
} else {
// User clicked the Share button
NSString *result = [NSString stringWithFormat: #"Posted story, id: %#", [urlParams valueForKey:#"post_id"]];
NSLog(#"result %#", result);
}
}
}
}];
}
And this at the end of the class, before #end:
// A function for parsing URL parameters returned by the Feed Dialog.
- (NSDictionary*)parseURLParams:(NSString *)query {
NSArray *pairs = [query componentsSeparatedByString:#"&"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *pair in pairs) {
NSArray *kv = [pair componentsSeparatedByString:#"="];
NSString *val =
[kv[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
params[kv[0]] = val;
}
return params;
}
Feed dialog is used because the Simulator does not have Facebook App v6.0+
The magnifying glass blocks the Share button completely. How can I remove it?
Note: The version installed is v3.13.0 (at the moment of writing, the official latest version is 3.13.1).
Note: iOS used are 6.0, 7.0, 7.1.
This issue has been resolved by Facebook in server side. The magnifier has been removed.
No need to update Facebook SDK.

Resources