我正在使用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);
}
}
发布于 2011-05-09 20:42:18
-(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
方法,并将文本字段参数存储在postString
中,这就是我在postfeedcustomized
方法中发布的postStirng。请参阅此说明[variables setObject:postString forKey:@"message"];
这里的post字符串是字符串...which在viewController 2中,...it携带由user..now输入的消息这是用于发布消息的代码从另一个view...when按钮被按下用于发布馈送它将转到另一个视图控制器,其中是textbox..there用户在输入消息后输入他的message...and方法postfeedcustomized
正从该视图控制器被调用。..我已经根据您的需要和从另一个视图的post图片对状态控制器执行了此操作。
发布于 2011-05-09 19:18:40
我也使用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对象。在此之后,我可以使用以下命令捕获访问:
m_Facebook.accessToken = [[NSUserDefaults standardUserDefaults] stringForKey:@"AccessToken"];
m_Facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:@"ExpirationDate"];
https://stackoverflow.com/questions/5935846
复制相似问题