使用嵌入式WebView登录对话框登录Facebook [英] Login facebook right in app with Embedded WebView Login Dialog

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

问题描述

我读了facebook文档,找到一些登录Facebook的方式。如何使用FB sdk 3.5.x的嵌入式WebView登录对话框登录



在这里链接facebook文档

解决方案

可能有用给你和几个Facebook的代理方法也显示在下面

   - (BOOL)应用程序:(UIApplication *)应用程序openURL :( NSURL *)url 
sourceApplication :( NSString *)sourceApplication注释:(id)注释
{
return [facebook handleOpenURL:url];
}

- (void)applicationDidBecomeActive:(UIApplication *)应用程序
{
//尽管SDK在进行API调用时尝试刷新其访问令牌,
//这是在应用程序变为活动状态时刷新访问令牌的好习惯。
//这使得很少使api调用的应用程序有更高的机会获得非过期的
//访问令牌。
[[self facebook] extendAccessTokenIfNeeded];
}

- (BOOL)应用程序:(UIApplication *)应用程序handleOpenURL :( NSURL *)url
{
NSLog(@url->%@ ,URL);
return [self.facebook handleOpenURL:url];
}
- (BOOL)isFBLoggedIn
{
return [facebook isSessionValid];
}

- (void)login
{
if(![facebook isSessionValid])
{
[facebook authorize:[[ NSArray alloc] initWithObjects:@offline_access,nil]];

} else
{
[facebook logout];
[facebook authorize:[[NSArray alloc] initWithObjects:@offline_access,nil]];
}
}
- (void)fetchFBFriends
{
[facebook requestWithGraphPath:@me / friendsandDelegate:self];
}
- (void)fetchFbDetails
{
[facebook requestWithGraphPath:@meandDelegate:self];
}
- (void)storeAuthData:(NSString *)accessToken expiresAt :( NSDate *)expiresAt
{
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:accessToken forKey:@FBAccessTokenKey];
[defaults setObject:expiresAt forKey:@FBExpirationDateKey];
[默认同步];
}

NSString * strRes = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@https://graph.facebook.com/me?fields=name,username,email ,gender,birthday,hometown,picture& access_token =%@,facebook.accessToken]] usedEncoding:& encoding error:& error];

NSMutableDictionary * dirFbDetails = [[NSMutableDictionary alloc] initWithDictionary:[strRes JSONValue]];

- (void)fbDidExtendToken:(NSString *)accessToken expiresAt :( NSDate *)expiresAt
{
NSLog(@token extended);
[self storeAuthData:accessToken expiresAt:expiresAt];
}
- (void)doFaceBookLogin
{
[self.facebook authorize:[NSArray arrayWithObjects:@friends_birthday,@user_birthday,@email,@ publish_stream,零]];

}
#pragma mark - FBRequestDelegate方法

- (void)请求:(FBRequest *)请求didReceiveResponse :( NSURLResponse *)响应
{

}
- (void)请求:( FBRequest *)请求didLoad:(id)result
{
}

- ( void)请求:( FBRequest *)请求didFailWithError:(NSError *)错误
{


NSLog(@Err message:%@,[[error userInfo] objectForKey :@ ERROR_MSG]);
NSLog(@错误代码:%d,[错误代码]);
}


I read facebook docs and find some ways to login facebook. How to login with Embedded WebView Login Dialog with FB sdk 3.5.x

Link facebook docs here

解决方案

It may be usefull to you and few delegate methods of facebook are also shown below

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [facebook handleOpenURL:url];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Although the SDK attempts to refresh its access tokens when it makes API calls,
    // it's a good practice to refresh the access token also when the app becomes active.
    // This gives apps that seldom make api calls a higher chance of having a non expired
    // access token.
    [[self facebook] extendAccessTokenIfNeeded];
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    NSLog(@"url->%@",url);
    return [self.facebook handleOpenURL:url];
}
- (BOOL)isFBLoggedIn
{
    return [facebook isSessionValid];
}

- (void)login
{
    if (![facebook isSessionValid])
    {
        [facebook authorize:[[NSArray alloc] initWithObjects:@"offline_access", nil]];

    } else
    {
        [facebook logout];
        [facebook authorize:[[NSArray alloc] initWithObjects:@"offline_access", nil]];
    }
}
- (void)fetchFBFriends
{
    [facebook requestWithGraphPath:@"me/friends" andDelegate:self];
}
-(void)fetchFbDetails
{
    [facebook requestWithGraphPath:@"me" andDelegate:self];
}
- (void)storeAuthData:(NSString *)accessToken expiresAt:(NSDate *)expiresAt
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:accessToken forKey:@"FBAccessTokenKey"];
    [defaults setObject:expiresAt forKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}

    NSString *strRes = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/me?fields=name,username,email,gender,birthday,hometown,picture&access_token=%@",facebook.accessToken]] usedEncoding:&encoding error:&error];

    NSMutableDictionary *dirFbDetails = [[NSMutableDictionary alloc] initWithDictionary:[strRes JSONValue]];

    -(void)fbDidExtendToken:(NSString *)accessToken expiresAt:(NSDate *)expiresAt
    {
        NSLog(@"token extended");
        [self storeAuthData:accessToken expiresAt:expiresAt];
    }
    -(void)doFaceBookLogin
    {
        [self.facebook authorize:[NSArray arrayWithObjects:@"friends_birthday",@"user_birthday",@"email",@"publish_stream",nil]];

    }
    #pragma mark - FBRequestDelegate Methods

    - (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response
    {

    }
    - (void)request:(FBRequest *)request didLoad:(id)result
    {
    }

    - (void)request:(FBRequest *)request didFailWithError:(NSError *)error
    {


        NSLog(@"Err message: %@", [[error userInfo] objectForKey:@"error_msg"]);
        NSLog(@"Err code: %d", [error code]);
    }

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

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