我是 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];
}
我的问题是关于一种可能性。我可以自己有一个机制从用户那里获取电子邮件和密码然后登录(就像其他方法一样,例如在控制台中打印身份验证失败或成功登录)?