我是iOS开发的新手。我想从我的iPhone应用程序连接到Facebook。我跟着FBGraph API看了我们如何在我们的应用程序中使用Facebook,如:
打印登录用户的信息:
FbGraphResponse *fb_graph_response = [fbGraph doGraphGet:@"me" withGetVars:nil];
NSLog(@"getMeButtonPressed: %@", fb_graph_response.htmlResponse);
或朋友列表:
FbGraphResponse *fb_graph_response = [fbGraph doGraphGet:@"me/friends" withGetVars:nil];
NSLog(@"getMeFriendsButtonPressed: %@", fb_graph_response.htmlResponse);
这是doGraphGet
中的FbGraph.m
方法:
- (FbGraphResponse *)doGraphGet:(NSString *)action withGetVars:(NSDictionary *)get_vars {
NSString *url_string = [NSString stringWithFormat:@"https://graph.facebook.com/%@?", action];
//tack on any get vars we have...
if ( (get_vars != nil) && ([get_vars count] > 0) ) {
NSEnumerator *enumerator = [get_vars keyEnumerator];
NSString *key;
NSString *value;
while ((key = (NSString *)[enumerator nextObject])) {
value = (NSString *)[get_vars objectForKey:key];
url_string = [NSString stringWithFormat:@"%@%@=%@&", url_string, key, value];
}//end while
}//end if
if (accessToken != nil) {
//now that any variables have been appended, let's attach the access token....
url_string = [NSString stringWithFormat:@"%@access_token=%@", url_string, self.accessToken];
}
首先我们需要登录Facebook,如下所示:
我猜它在FbGraph.m中使用此代码(使用UIWebView
):
self.redirectUri = @"http://www.facebook.com/connect/login_success.html";
- (void)authenticateUserWithCallbackObject:(id)anObject andSelector:(SEL)selector andExtendedPermissions:(NSString *)extended_permissions andSuperView:(UIView *)super_view {
self.callbackObject = anObject;
self.callbackSelector = selector;
NSString *url_string = [NSString stringWithFormat:@"https://graph.facebook.com/oauth/authorize?client_id=%@&redirect_uri=%@&scope=%@&type=user_agent&display=touch", facebookClientID, redirectUri, extended_permissions];
NSURL *url = [NSURL URLWithString:url_string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
CGRect webFrame = [super_view frame];
webFrame.origin.y = 0;
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
[aWebView setDelegate:self];
self.webView = aWebView;
[aWebView release];
[webView loadRequest:request];
[super_view addSubview:webView];
}
我的问题是关于可能性。我是否可以通过自己的机制从用户那里获取电子邮件和密码然后登录(例如其他方法,例如在控制台中打印认证失败或成功登录)?
答案 0 :(得分:0)
点击按钮,其事件是fbLogin,然后添加此代码进行登录
-(void)fbLogin
{
if(!self.fbGraph.accesstoken) // doesnot have access token. So needed to login
{
NSString *client_id = @"130902823636657"; //get your own client id from facebook
//alloc and initalize our FbGraph instance
self.fbGraph = [[FbGraph alloc] initWithFbClientID:client_id];
//begin the authentication process.....
[self.fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback:) andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins"];
}
else
{
// Add UIAlert as user is logged in already
//pop a message letting them know most of the info will be dumped in the log
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note" message:@"user is logged in already" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
现在,当用户被认证时,将调用以下方法。所以在你的.h文件中添加这个方法。
#pragma mark -
#pragma mark FbGraph Callback Function
/**
* This function is called by FbGraph after it's finished the authentication process
**/
- (void)fbGraphCallback:(id)sender
{
if ( (self.fbGraph.accessToken == nil) || ([self.fbGraph.accessToken length] == 0) )
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note" message:@"Try Again" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
//restart the authentication process.....
//[self.fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback:) andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins"];
}
else
{
//pop a message letting them know most of the info will be dumped in the log
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note" message:@"Logged In Successfully" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
NSLog(@"------------>CONGRATULATIONS<------------, You're logged into Facebook... Your oAuth token is: %@", self.fbGraph.accessToken);
}
}