如何知道我的应用程序是否通过FacebookSDK授权?

时间:2015-08-07 09:22:48

标签: ios objective-c facebook-graph-api uiview

您好我已经在我的应用中集成了我的Facebook登录信息,一切正常。但问题是我有一个注册表格,当用户第一次用Facebook授权应用程序时必须显示该表格(至此它运作良好)。但是,当我再次启动应用程序并使用Facebook登录时,表单会显示但我不希望显示该表单,而是应该直接显示我的主页视图。如何实现这一点?

1 个答案:

答案 0 :(得分:0)

试试这个:set your app info.plist file which is available at supporting folder

-(void)Fb_Login
{
    if (!FBSession.activeSession.isOpen)
    {
        // if the session is closed, then we open it here, and establish a handler for state changes

        [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState state, NSError *error)
         {
             if (error)
             {
                 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                 [alertView show];
             }
             else if(session.isOpen)
             {
                 [self Fb_Login];
             }

         }];
        return;
    }



    [FBRequestConnection startWithGraphPath:@"me" parameters:[NSDictionary dictionaryWithObject:@"picture,id,birthday,email,name,gender" forKey:@"fields"] HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
     {
         if (!error)
         {
             if ([result isKindOfClass:[NSDictionary class]])
             {
                 //NSDictionary *dictionary;
                 if([result objectForKey:@"data"])
                     dictionary = (NSDictionary *)[(NSArray *)[result objectForKey:@"data"] objectAtIndex:0];
                 else
                     dictionary = (NSDictionary *)result;

                 app.fb_image_url = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?width=200&height=200",[dictionary objectForKey:@"id"]];
                 [app.fb_image_url retain];

                 Url_Image_social = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?width=200&height=200",[dictionary objectForKey:@"id"]]];
                 [Url_Image_social retain];



                 user_email = [dictionary objectForKey:@"email"];
                 [dictionary retain];

                 app.accessTokan = [[[FBSession activeSession] accessTokenData] accessToken];

                 NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
                 [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/me?access_token=%@",app.accessTokan]]];
                 [request setHTTPMethod:@"GET"];
                 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
                 NSError *error;
                 NSURLResponse *response;
                 NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
                 NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

                 fb_dict = [str JSONValue];
                 [fb_dict retain];
                 [str release];

                 if([user_email isEqualToString:@"null"] || [user_email isEqual:[NSNull null]] || !user_email)
                 {
                     NSString *strName = [NSString stringWithFormat:@"%@%@",[fb_dict objectForKey:@"first_name"],[fb_dict objectForKey:@"last_name"]];

                     user_email = [NSString stringWithFormat:@"%@@facebook.com",strName];
                     [user_email retain];
                 }
                 else
                 {     
                   //direct where ever you want 
                 }


             }
         }
     }];

}