iPhone : Cannot launch facebook dialog from alert view - iphone

The facebook dialog is not launching from alert view handler.
But facebook dialog launches OK from button.
The code of launching facebook dialog is following
- (void) share_on_facebook
{
_facebook = [[Facebook alloc] initWithAppId:kAppId];
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSDictionary* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
#"Always Running",#"text",#"http://itsti.me/",#"href", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
#"a long run", #"name",
#"The Facebook Running app", #"caption",
#"it is fun", #"description",
#"http://itsti.me/", #"href", nil];
NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Share on Facebook", #"user_message_prompt",
actionLinksStr, #"action_links",
attachmentStr, #"attachment",
nil];
[_facebook dialog:#"feed" andParams:params andDelegate:self];
}
The handler of alert view is button click
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
[self share_on_facebook];
}
The facebook dialog is not launched but when I call share_on_facebook from ordinary button click handler the dialog opens successfully.
Please help to figure out the problem.

[self performSelectorOnMainThread:#selector(share_on_facebook)]; should do the trick

Related

iOS Post to Facebook publish screen doesn't show after authentication

Alrighty. I have a button (below) where I take some info from an app and I post it to the users FB feed.
It almost never works the first time. Them, it will usually work pretty well after that. What happens is, when I press the button it tells me that I already gave the app permission to use FB (which I would like to only have to see the first time, not every time). Then when I press OK from there it will either go back to the app, or it will go to show me the page where I can actually post to my wall (which is what I want).
Im trying different things to get this to work more consistently. Is there something here that would be causing this behavior?
-(IBAction)fbButton:(id)sender {
if (![facebook isSessionValid]) {
[facebook authorize:permissions];
[permissions release];
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
//Post to Wall
NSString *poopDollas = [[NSString alloc] initWithFormat:
#"sometext $%#!",dollas.text];
SBJSON *jsonWriter = [[SBJSON new] autorelease];
// The action links to be shown with the post in the feed
NSArray* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
#"Get Started",#"name",#"http://www.facebook.com/pages/xx/204640386498567",#"link", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
// Dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"DM", #"name",
poopDollas, #"caption",
#"sometext", #"description",
#"http://www.facebook.com/pages/xx/204640386498567", #"link",
#"http://farm8.staticflickr.com/7185/6999841878_e66a8e00fc_t.jpg", #"picture",
actionLinksStr, #"actions",
nil];
FBDialog *delegate = (FBDialog *)[UIApplication sharedApplication].delegate;
[facebook dialog:#"feed" andParams:params andDelegate:(id <FBDialogDelegate>)delegate];
}
I took out the
[facebook authorize:permissions];
[permissions release];
In that first IF statement and my problems seem to be solved..

sharing text on "facebook Page" in ios from our app

I have a Facebook page, and i want to share some text on that page just like my status as we share on our "me/feed" from our application.
Please someone suggest how i should go about it?
for iOS 6.0 you can do something like that:
NSString *text1 = [NSString stringWithFormat:#"%# %#", NSLocalizedString(#"Check out this nice'... ", #""), self.title];
NSString *text2 = [NSString stringWithFormat:#"%# %#", NSLocalizedString(#"in", #""), self.restaurant.title];
NSString *text3 = NSLocalizedString(#"Sent from FoodRock Menus App", #"");
NSURL *appURL = [NSURL URLWithString:#"http://testappstorelink.com"];
NSArray *activityItems = [NSArray arrayWithObjects:text1, text2, text3, appURL, photo, nil];
UIActivityViewController *actVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems
applicationActivities:nil];
actVC.excludedActivityTypes = [NSArray arrayWithObjects:UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard,
UIActivityTypePrint, UIActivityTypeSaveToCameraRoll, nil];
actVC.completionHandler = ^(NSString *activityType, BOOL completed){
DLog(#"ACTIVITYTYPE: %# FINISHED: %#", activityType, completed ? #"YES" : #"NO");
if (completed) {
NSString *socialString = #"";
if ([activityType containsSubstring:#"PostToTwitter"]) {
socialString = #"Twitter";
} else if ([activityType containsSubstring:#"PostToFacebook"]) {
socialString = #"Facebook";
} else if ([activityType containsSubstring:#"Message"]) {
socialString = #"Message";
} else if ([activityType containsSubstring:#"Mail"]) {
socialString = #"Mail";
}
[[FRNWAchievementsController controller] reportSocialShareEvent:socialString menuItem:self];
}
};
return actVC;
Here are some code snippets to help you out, but you should also have a look at the Facebook developer examples which are pretty good.
// To log in to Facebook with permission to post...
m_Facebook = [[Facebook alloc] initWithAppId:YOUR_FACEBOOK_APPID
andDelegate:self];
m_FacebookPermissions = [[NSArray
arrayWithObjects:/*#"read_stream",*/ #"publish_stream", nil] retain];
[m_Facebook authorize:m_FacebookPermissions];
And in some other method after the fbDidLogin callback tells you you're logged in:
// After successful login…
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
WHAT_YOU_WANT_TO_SAY, #"message",
nil];
[m_Facebook requestWithGraphPath:#"/me/feed"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];

Image Post on Facebook?

In my application. I successfully post a image on my Facebook wall (image type JPEG Type). It's work fine in Simulator and and Device also.
But the problem is that When I Capture Image from my iPhone or select an image from library which is captured by camera. It is not Posted on Facebook Wall.
on other hand when i choose any jpeg image (downloaded by internet) from my iPhone library. It post on wall.
In my opinion there is image format problem. Please Guide me
Thanks
My code is here.
-(IBAction)clickButtonClicked
{
// For link post
NSString *Message = [NSString stringWithFormat:#"Testing Iphone App : \"http://www.google.com\" "];
DataClass *obj=[DataClass getInstance];
UIImage *image=[[UIImage alloc]init ];
image= obj.imgFinalImage;
NSMutableDictionary *params1 = [[NSMutableDictionary alloc]init];
[params1 setObject:image forKey:#"source"];
[params1 setObject:Message forKey:#"message"];
NSString *post=#"/me/photos";
[[appDelegate facebook] requestWithGraphPath:post andParams:params1 andHttpMethod:#"POST" andDelegate:self];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message!" message:#"Invitation Send Sucessfully" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
My guess is that you pass the image in the wrong property:
NSMutableDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
sobj.imgFinalImage, #"picture",
Message, #"caption",
nil];
[[appDelegate facebook] requestWithMethodName:#"photos.upload"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
Then make sure you implement the delegate of Facbook request to check if the image is uploaded.
With the graph API you can't send a UIImage directly, you need to send the NSData representation of the UIImage:
NSMutableDictionary *params1 = [[NSMutableDictionary alloc]init];
NSData *imageDate = UIImagePNGRepresentation(obj.imgFinalImage);
[params1 imageDate forKey:#"source"];
[params1 setObject:Message forKey:#"message"];
NSString *post=#"/me/photos";
[[appDelegate facebook] requestWithGraphPath:post andParams:params1 andHttpMethod:#"POST" andDelegate:self];

Post Directly To the app's wall

i'm trying to post directly from my iOS App to the Facebook application page but with no luck.
i have read this
iOS post to the Facebook app wall
and this
Trying to post directly to an application's wall
but no luck with that.
help please
-(void)postToFacebookAppPage
{
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"##########...", #"app_id",
#"##########...",#"to",
//#"http://www.facebook.com/pages/AppPageURL", #"link",
//#"icon.png", #"picture",
//#"App Page Name", #"name",
//#"Post to App Facebook Page", #"caption",
#"Do Stuff Facebook page.", #"description",
nil];
AppDelegate *sharedAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[sharedAppDelegate.facebook dialog:#"feed" andParams:params andDelegate:self];
}
Where the numbers #### are for your page.
-(void)postToFacebookAppPage
{
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"##########...", #"app_id",
//#"http://www.facebook.com/pages/AppPageURL", #"link",
//#"icon.png", #"picture",
//#"App Page Name", #"name",
//#"Post to App Facebook Page", #"caption",
#"Do Stuff Facebook page.", #"description",
nil];
AppDelegate *sharedAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[sharedAppDelegate.facebook dialog:#"pageid/feed" andParams:params andDelegate:self];
}

how to login some other user on facebook in your app?

i am publishing some story on the user's facebook wall in my app. its working fine but i wanna know if some other user wanna login in my app with other facebook account how can he do that. i mean how can i logout an existing user and allow other user to login. my code in AppDelegate is:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
facebook = [[Facebook alloc] initWithAppId:#"app_id"];
NSArray* permissions = [[NSArray arrayWithObjects:
#"publish_stream", nil] retain];
[facebook authorize:permissions delegate:self];
return YES;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
and my code in DetailViewController is:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
[sTitle stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""], #"name",
[sSummary stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""], #"description",
[sLink stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""], #"href", nil];
NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"XXX", #"api_key",
#"Share on Facebook", #"user_message_prompt",
attachmentStr, #"attachment",
nil];
[appDelegate.facebook dialog:#"stream.publish" andParams:params andDelegate:self];
once app is installed it asks for username and password for facebook. and then it publishes stories to only that user. what about if i wanna change user in my app. where should i write
[facebook logout:self];
You'll need to provide the options for login/ logout. This post will help you how to do it.