Facebook Graph API for iphone passing access token
I am using Facebook Graph API like this example API
It is verifying login credentials in View did Appear and calling all the method in one view. I want to use these methods in other views but how to pass access token to other views(maintains login session). Here is code for login
- (void)viewDidAppear:(BOOL)animated {
//Facebook Application ID
NSString *client_id = @"142759389130183";
//alloc and initalize our FbGraph instance
self.fbGraph = [[FbGraph alloc] initWithFbClientID:client_id];
//begin the authentication process.....
//[fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback:)
// andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins"];
[fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback:) andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access" andSuperView:self.view];
}
- (void)fbGraphCallback:(id)sender {
if ( (fbGraph.accessToken == nil) || ([fbGraph.accessToken length] == 0) ) {
NSLog(@"You pressed the 'cancel' or 'Dont Allow' button, you are NOT logged into Fac开发者_高级运维ebook...I require you to be logged in & approve access before you can do anything useful....");
//restart the authentication process.....
// [fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback:)
// andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins"];
// StartPage *startPage = [[StartPage alloc]initWithNibName:@"StartPage" bundle:nil];
//[self.navigationController pushViewController:startPage animated:YES];
//[startPage release];
[self dismissModalViewControllerAnimated:YES];
} else {
//pop a message letting them know most of the info will be dumped in the log
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Welcome" message:@"You are logged into facebook" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
NSLog(@"------------>CONGRATULATIONS<------------, You're logged into Facebook... Your oAuth token is: %@", fbGraph.accessToken);
}
}
-(IBAction)postMeFeedButtonPressed:(id)sender {
postFeedCustom *detailViewController = [[postFeedCustom alloc] initWithNibName:@"postFeedCustom" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
-(void)postMeFeedCustomized{
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:4];
[variables setObject:postString forKey:@"message"];
//[variables setObject:@"http://bit.ly/bFTnqd" forKey:@"link"];
//[variables setObject:@"This is the bolded copy next to the image" forKey:@"name"];
//[variables setObject:@"This is the plain text copy next to the image. All work and no play makes Jack a dull boy." forKey:@"description"];
FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:@"me/feed" withPostVars:variables];
NSLog(@"postMeFeedButtonPressed: %@", fb_graph_response.htmlResponse);
//parse our json
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *facebook_response = [parser objectWithString:fb_graph_response.htmlResponse error:nil];
[parser release];
//let's save the 'id' Facebook gives us so we can delete it if the user presses the 'delete /me/feed button'
self.feedPostId = (NSString *)[facebook_response objectForKey:@"id"];
NSLog(@"feedPostId, %@", feedPostId);
NSLog(@"Now log into Facebook and look at your profile...");
}
in another class where you want user to post his custom message... you can do like this..
-(void) viewDidLoad{
[super viewDidLoad];
appDelegate=(oAuth2TestAppDelegate*)[[UIApplication sharedApplication]delegate];
}
-(IBAction)FeedPost:(id)sender{
postString=postField.text;
[appDelegate.viewController postMeFeedCustomized];
postField.text=@"";
[postField resignFirstResponder];
}
have you seen that?? in this method for the button I have called the postfeedcustomized
method and I am storing the textfield parameter in the postString
and this is that postStirng that I am posting in postfeedcustomized
method. see this instruction [variables setObject:postString forKey:@"message"];
here post string is the string ...which is in viewController 2 ...it carries the message entered by the user..now This is the code for posting message from another view...when button is pressed for posting feed it will go to another view controller where is a textbox..there user enters his message...and after he enters message the method postfeedcustomized
is being called from that viewController..I have done this for the status only...chage it according to u r need and post pic from another View..
I use Facebook API too. In my code I've a "FacebookLogger" who is a singleton with a Facebook object.
To store session I use NSUserDefaults (find docs here).
[[NSUserDefaults standardUserDefaults] setObject:m_Facebook.accessToken forKey:@"AccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:m_Facebook.expirationDate forKey:@"ExpirationDate"];
[[NSUserDefaults standardUserDefaults] synchronize];
Where m_Facebook is my Facebook object in my singleton. After that I can catch Access with :
m_Facebook.accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"AccessToken"];
m_Facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:@"ExpirationDate"];
精彩评论