使用FBGraph登录到Facebook [英] Login to Facebook using FBGraph

查看:415
本文介绍了使用FBGraph登录到Facebook的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS开发中是新的。我想从我的iPhone应用程序连接到Facebook。我遵循 FBGraph API 查看我们如何使用Facebook我们的应用程序喜欢:



打印登录用户的信息:

  FbGraphResponse * fb_graph_response = [fbGraph doGraphGet:@mewithGetVars:nil]; 
NSLog(@getMeButtonPressed:%@,fb_graph_response.htmlResponse);

或朋友列表:

  FbGraphResponse * fb_graph_response = [fbGraph doGraphGet:@我/朋友withGetVars:nil]; 
NSLog(@getMeFriendsButtonPressed:%@,fb_graph_response.htmlResponse);

这是 doGraphGet c $ c> FbGraph.m :

   - (FbGraphResponse *)doGraphGet:(NSString *) actionGetVars:(NSDictionary *)get_vars {

NSString * url_string = [NSString stringWithFormat:@https://graph.facebook.com/%@?,action];

//粘贴任何get vars我们有...
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){
//现在任何变量都已经附加,我们来附加访问令牌....
url_string = [NSString stringWithFormat:@%@ access_token =%@,url_string,self.accessToken];
}

首先我们需要登录到Facebook,如图所示:




$ b $我猜这个代码在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];
}

我的问题是一个可能性。我可以自己拥有一个机制,从用户那里获取电子邮件和密码,然后登录(像其他方法,例如在控制台中打印出来的Authenication failed或successfull Login)?

解决方案

点击按钮,其事件是说fbLogin然后添加此代码进行登录

   - (void)fbLogin 
{
if(!self.fbGraph.accesstoken)//没有访问令牌。所以需要登录
{
NSString * client_id = @130902823636657; //从Facebook获取您自己的客户端ID

//分配和初始化我们的FbGraph实例
self.fbGraph = [[FbGraph alloc] initWithFbClientID:client_id];

//开始认证过程.....
[self.fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback :) andExtendedPermissions:@user_photos,user_videos,publish_stream,offline_access, user_checkins,friends_checkins];
}
else
{
//添加UIAlert,因为用户已经登录了
//弹出一个消息让他们知道大部分信息将被转储在log
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@注意消息:@用户已经登录委托:nil cancelButtonTitle:@OkotherButtonTitles:nil];
[alert show];
[alert release];
}
}

现在,当用户被自动化时,此方法被称为。所以在.h文件中添加这个方法。

  #pragma mark  -  
#pragma mark FbGraph回调函数
/ **
* FbGraph在完成身份验证过程后调用此函数
** /
- (void)fbGraphCallback:(id)sender
{

if((self.fbGraph.accessToken == nil)||([self.fbGraph.accessToken length] == 0))
{

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@注意消息:@再试一次委托:nil cancelButtonTitle:@OkotherButtonTitles:nil];
[alert show];
[alert release];

//重新启动身份验证过程.....
//[self.fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback :) andExtendedPermissions:@user_photos,user_videos,publish_stream, offline_access,user_checkins,friends_checkins];

}
else
{
//弹出消息让他们知道大部分信息将被转储到日志中
UIAlertView * alert = [ [UIAlertView alloc] initWithTitle:@注意消息:@成功登录委托:nil cancelButtonTitle:@OkotherButtonTitles:nil];
[alert show];
[alert release];
NSLog(@------------> CONGRATULATIONS< ------------,您已登录Facebook ...您的oAuth令牌是:%@,self.fbGraph.accessToken);
}
}


I am new in iOS development. I want to connect to Facebook from my iPhone app. I followed FBGraph API to see how we can use Facebook in our App like:

Prints the information of user which is logged in:

FbGraphResponse *fb_graph_response = [fbGraph doGraphGet:@"me" withGetVars:nil];
NSLog(@"getMeButtonPressed:  %@", fb_graph_response.htmlResponse);

or the list of friends:

FbGraphResponse *fb_graph_response = [fbGraph doGraphGet:@"me/friends" withGetVars:nil];
NSLog(@"getMeFriendsButtonPressed:  %@", fb_graph_response.htmlResponse);

And this is doGraphGet method in 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];
    }

First we need to Login to Facebook as shown:

I guess that it uses this code in FbGraph.m (using 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];
}

My question is about a possibility. Can I have a mechanism on my own that get the email and password from the user and then login (like the other methods for instance print in the console that Authenication failed or successfull Login)?

解决方案

On click of button and its event is say fbLogin then add this code for login

-(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];
   }
}

Now when user is authebticated this method below is called. So add this method in your .h file.

#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);
 }
}

这篇关于使用FBGraph登录到Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆