我正在使用Facebook Graph API,如此示例API
验证View中的登录凭据是否出现并在一个视图中调用所有方法。我想在其他视图中使用这些方法但是如何将访问令牌传递给其他视图(维护登录会话)。这是登录代码
- (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 Facebook...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);
}
}
答案 0 :(得分:0)
我也使用Facebook API。在我的代码中,我有一个“FacebookLogger”,他是一个带有Facebook对象的单身人士。
要存储会话,我使用NSUserDefaults (find docs here)。
[[NSUserDefaults standardUserDefaults] setObject:m_Facebook.accessToken forKey:@"AccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:m_Facebook.expirationDate forKey:@"ExpirationDate"];
[[NSUserDefaults standardUserDefaults] synchronize];
m_Facebook在我的单身人士中是我的Facebook对象。 之后,我可以通过以下方式访问Access:
m_Facebook.accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"AccessToken"];
m_Facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:@"ExpirationDate"];
答案 1 :(得分:0)
-(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...");
}
在另一个您希望用户发布自定义消息的类中... 你可以这样做..
-(void) viewDidLoad{
[super viewDidLoad];
appDelegate=(oAuth2TestAppDelegate*)[[UIApplication sharedApplication]delegate];
}
-(IBAction)FeedPost:(id)sender{
postString=postField.text;
[appDelegate.viewController postMeFeedCustomized];
postField.text=@"";
[postField resignFirstResponder];
}
你看到了吗?在这个按钮的方法中,我调用了postfeedcustomized
方法,我将textfield参数存储在postString
中,这就是我在postfeedcustomized
方法中发布的postStirng。请参阅此说明[variables setObject:postString forKey:@"message"];
这里post string是字符串...在viewController 2中...它带有用户输入的消息..现在这是从另一个视图发布消息的代码...当按下按钮发布时feed它将转到另一个视图控制器,其中是一个文本框..用户输入他的消息...并且在他输入消息之后,正在从该viewController调用方法postfeedcustomized
。我已经仅为状态执行此操作...根据你的需要改变它并从另一个视图发布图片..