IOS——在项目中获取FaceBook账号内容(从申请APPID开始)

 下面开启官方文档翻译模式:

https://developers.facebook.com/docs/ios/getting-started

介绍了如何使用FaceBook SDK的设置以及IOS 9 的适配

(1) 下载Facebook IOS-SDK

(2) 创建一个新的FaceBook 项目,步骤如下:

    2.1) 首先要在官网中登记并获取FaceBook AppID

    2.2) 访问网址:https://developers.facebook.com/apps (尼玛!!就找这个网址找了好久!!)

              按照上面指示,该登陆的登陆,该注册的注册

(3) 需要设置的参数有:

    3.1) 选择setting-> 点击 Add Platform -> 选择IOS

    3.2) 在显示的页面中添加Bundle ID

    3.3) Enable Sigle Sign On

保存设置

(4) 在你的项目中添加SDK

    第一步已经下载了SDK的压缩包,解压,将FBSDKCoreKit.framework导入你的项目中,要注意,引入第三方framework的时候,记得将原来的framework拷贝到你的项目中,否则在import的时候会报错,告知找不到该文件

(5) 按照下面的参数在info.plist文件中添加设置




(6) 在AppDelegate.m中添加下面代码:

<pre name="code" class="objc">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [FBLoginView class];
    return [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    
    NSLog(@"openURL");
    BOOL wasHandled=[FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
    [[FBSDKApplicationDelegate sharedInstance] application:application
                                                   openURL:url
                                         sourceApplication:sourceApplication
                                                annotation:annotation
     ];
    NSString *testtocken=[FBSDKAccessToken currentAccessToken].tokenString;
    NSLog(@"test token :%@",testtocken);
    
    return wasHandled;
    
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [FBSDKAppEvents activateApp];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"getFacebookData" object:nil];
}



(7) 在ViewController中添加FBLoginViewDelegate,并在Storyboard中添加FBLoginView,且添加到.h文件中

(8)ViewController.m文件中添加下面代码:

    //Init LoginView
    loginView.readPermissions=@[@"public_profile",@"email",@"user_friends"];
    loginView.delegate=self;

并实现delegate方法:

#pragma FBLoginView delegate
// This method will be called when the user information has been fetched
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user {
    NSLog(@"self.profilePictureView.profileID = :%@",user.id);
    NSLog(@"nameLabel.text :%@",user.name);
    
}

// Logged-in user experience
- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView {
    NSLog(@"self.statusLabel.text = @Youre logged in as");
}

// Logged-out user experience
- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView {
    /*self.profilePictureView.profileID = nil;
    self.nameLabel.text = @"";
    self.statusLabel.text= @"You're not logged in!";*/
    NSLog(@"Already logout");
}


// Handle possible errors that can occur during login
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error {
    NSString *alertMessage, *alertTitle;
    
    // If the user should perform an action outside of you app to recover,
    // the SDK will provide a message for the user, you just need to surface it.
    // This conveniently handles cases like Facebook password change or unverified Facebook accounts.
    if ([FBErrorUtility shouldNotifyUserForError:error]) {
        alertTitle = @"Facebook error";
        alertMessage = [FBErrorUtility userMessageForError:error];
        
        // This code will handle session closures that happen outside of the app
        // You can take a look at our error handling guide to know more about it
        // https://developers.facebook.com/docs/ios/errors
    } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) {
        alertTitle = @"Session Error";
        alertMessage = @"Your current session is no longer valid. Please log in again.";
        
        // If the user has cancelled a login, we will do nothing.
        // You can also choose to show the user a message if cancelling login will result in
        // the user not being able to complete a task they had initiated in your app
        // (like accessing FB-stored information or posting to Facebook)
    } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
        NSLog(@"user cancelled login");
        
        // For simplicity, this sample handles other errors with a generic message
        // You can checkout our error handling guide for more detailed information
        // https://developers.facebook.com/docs/ios/errors
    } else {
        alertTitle  = @"Something went wrong";
        alertMessage = @"Please try again later.";
        NSLog(@"Unexpected error:%@", error);
    }
    
    if (alertMessage) {
        [[[UIAlertView alloc] initWithTitle:alertTitle
                                    message:alertMessage
                                   delegate:nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil] show];
    }
}

 

重要事情说三遍!!重要事情说三遍!!重要事情说三遍!!

运行的时候可能会报错,这个时候在Build Settings中搜索bitcode,值改为NO即可

如果还不行,那么在AppDelegate中的didFinishLaunchingWithOptions中添加[FBLoginView class]即可!

上面的方法使用了Facebook定义的FBLoginView,可以获取用户的名字,还有生日和地点

获取图片方法:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=normal",user.id]];NSData  *data = [NSData dataWithContentsOfURL:url];_imageView.image = [UIImage imageWithData:data]; 

获得AccessToken的方法是:参见AppDelegate方法的 openURL~~

如何获取email呢?

还可参考博客:http://blog.csdn.net/cctvzxxz1/article/details/43191077


                
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值