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

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.

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..

iphone: Facebook logout feature is not work in my app [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
iphone: facebook logout feature does not working
i am implementing FB integration through FBConnect
It is not logout from Fb when i pressed logout button
It only show previous Login..
My code is as follows:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (![[delegate facebook] isSessionValid]) {
} else {
[self showLoggedIn];
}
}
- (IBAction)postMessageToWall:(id)sender {
currentAPICall = kDialogFeedUser;
SBJSON *jsonWriter = [[SBJSON new] autorelease];
NSArray* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
#"TaxSmart Hello.",#"name",#"http://www.google.com",#"link", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
UIImage *img=[UIImage imageNamed:#"tick.png"];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Piyush here.", #"name",
#"This is Great App.", #"caption",
#"FuNNNN.", #"description",
#"http://www.iTaxsmart.com/", #"link",
img.CIImage, #"picture",
actionLinksStr, #"actions",
nil];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[delegate facebook] dialog:#"feed"
andParams:params
andDelegate:self];
}
when you click on logout button call bellow method its work but just check your userdefault key..
- (void)fbDidLogout
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[facebook invalidateSession];
[defaults removeObjectForKey:#"FBAccessTokenKey"];
[defaults removeObjectForKey:#"FBExpirationDateKey"];
[defaults synchronize];
}
hope,this help you.....
:)

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];
}

why fbdidlogin didn't call?

- (id)init {
if (self == [super init]) {
facebook = [[Facebook alloc] initWithAppId:kAppId];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"FBAccessTokenKey"]
&& [defaults objectForKey:#"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
if (![facebook isSessionValid]) {
permissions = [[NSArray arrayWithObjects:
#"read_stream", #"user_birthday",
#"publish_stream", nil] retain];
[facebook authorize:permissions delegate:self];
}
[self login];
}
return self;
}
- (void)login {
if (![_session isConnected]) {
[self postToWall];
}
if (![facebook isSessionValid]) {
[facebook authorize:permissions delegate:self];
}
}
- (void)fbdidLogin {
[[NSUserDefaults standardUserDefaults] setObject:self.facebook.accessToken forKey:#"FBAccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:self.facebook.expirationDate forKey:#"FBExpirationDate"];
// User has logged in to Facebook, now get their userId from Facebook
[facebook requestWithGraphPath:#"me" andDelegate:self];
}
-(void)postToWall
{
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:
#"Your Happiness!", #"name",
#"asda", #"caption",
#"asdf", #"description",
nil];
NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"Share on Facebook", #"user_message_prompt",
actionLinksStr, #"action_links",
attachmentStr, #"attachment",
nil];
[facebook dialog:#"stream.publish" andParams:params andDelegate:self];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [facebook handleOpenURL:url];
}
This is my methods for calling facebook dialog boxes.But every time when i click to score for publishing;The permission box came first.I need to give permission after that the publish dialog box comes.I want to give permission ones and i want it to save it;after saving it i don't wanna see the permission box ever again.How can i do it?What is wrong with my code?
edit:
My access token and expiration date is null i guess thats because of this.
A few things to check, you call the method login handling method "fbdidLogin" instead of fbDidLogin. This is case sensitive.
Make sure in your view controller header file that you added FBSessionDelegate as one of the protocols you support. That could be a reason fbDidLogin is not called. You could always add an NSLog to verify that the fbDidLogin method is reached.
You need to implement Facebook's Single-Sign-On feature.
http://developers.facebook.com/docs/guides/mobile/#ios
This link has the step-by-step guide for implementing SSO, if you follow it carefully your App will request for permission once, then it will never request permission again (until the user remove your App)
Note that, you will need to go to www.facebook.com and use the "Developer Tools" to create a new Facebook App, you will be the admin of the app, and you will have your AppID, you will need this too.

iPhone : Cannot launch facebook dialog from alert view

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