7

我的应用程序已经在 iOS 5 和 iOS 6 上运行。现在,我想实现“使用 Facebook 登录”的功能。

我的应用程序已经有登录身份验证,所以 facebook 登录将是额外的。我知道 iOS 6 为 facebook 提供了新框架,但我有两个问题:

  1. 如何实现这个 facebook 功能,以便它可以在 iOS 5 和 iOS 6 设备上运行?2.我如何保持与facebook登录的会话?
4

4 回答 4

8

1) 按照以下步骤获取 facebook 登录说明:https ://developers.facebook.com/docs/howtos/login-with-facebook-using-ios-sdk/

2)一旦您使用 facebook sdk 3.x 实现上述内容,这也适用于 iOS 5 和 iOS 6。

于 2012-11-07T18:50:33.627 回答
3

1.要支持在ios 5 和ios 6 中登录,可以使用FBLoginView 类。这是使用 Facebook sdk 登录 facebook 的最简单方法。

    FBLoginView * pFBLoginViewObj = [[FBLoginView alloc] init];
    [pFBLoginViewObj setFrame:self.view.frame];
    pFBLoginViewObj.delegate = self;//optional
    [self.view addSubview:pFBLoginViewObj];

如果需要,实现委托方法。

2.要维护会话,您必须在应用程序 dalagate 文件中进行更改 ....like

-(BOOL) application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    if (url != nil)
    {

        return [[FBSession activeSession] handleOpenURL:url];
    }

    return NO;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{

    [[FBSession activeSession] handleDidBecomeActive];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
    [[FBSession activeSession] close];
}
于 2013-04-10T05:15:03.737 回答
2

首先我们必须在我们的项目中添加这个框架,

在此处输入图像描述

之后,您可以在 appDelegate 中导入此方法

 #import <FacebookSDK/FacebookSDK.h>
 #import <FBSDKCoreKit/FBSDKCoreKit.h>

之后 didFinishLaunchingWithOptions 我们必须添加这个方法

[[FBSDKApplicationDelegate sharedInstance] application:application
                         didFinishLaunchingWithOptions:launchOptions];

你可以调用回调方法,

#pragma mark - Callback methods
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation    {
return [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}

之后,我们必须在您的 facebooklogin 操作方法中编写,

FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
        [loginManager logInWithReadPermissions:@[@"public_profile", @"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
            //TODO: process error or result.
            if (error) {
                NSLog(@"Process error");
                [self facebookLoginFailed:YES];
            } else if (result.isCancelled) {
                [self facebookLoginFailed:NO];
            } else {
                if ([FBSDKAccessToken currentAccessToken]) {
                    NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
                    NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
                    [parameters setValue:@"id,name,email,first_name,last_name,picture.type(large)" forKey:@"fields"];
                    [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:parameters]
                     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                         if (!error) {
                             NSString *fbPhotoUrl = [[[result objectForKey:@"picture"]objectForKey:@"data"]objectForKey:@"url"];
                             lDefaults = [NSUserDefaults standardUserDefaults];
                             [lDefaults setObject:@"fbPhotoUrl" forKey:@"fbPhotoUrl"];
                             [lDefaults synchronize];
                             NSLog(@"%@",fbPhotoUrl);
                         }
                     }];
                }
                else {
                    [self facebookLoginFailed:YES];
                }
            }
        }]; 

如果 facebook 登录失败,我们可以写这个警报,

- (void)facebookLoginFailed:(BOOL)isFBResponce{
if(isFBResponce){
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"pop_attention", nil) message:NSLocalizedString(@"request_error", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"pop_ok", nil) otherButtonTitles: nil];
    [alert show];
}
else{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"pop_attention", nil) message:NSLocalizedString(@"loginfb_cancelled", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"pop_ok", nil) otherButtonTitles: nil];
    [alert show];
}
}

之后转到目标-->信息-->网址类型-->UrlSchmes(添加您的 facebook id)。

还有更多我们必须在 www.developers.facebook.com 上做的事情去 ypur app-->在 AppReview-->Make YourApp 公开?是。

之后我们需要在 plist 中添加 tis 代码

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fb1801381186770425</string>
        </array>
    </dict>
</array>
<key>FacebookAppID</key>
<string>AddYourID</string>
<key>FacebookDisplayName</key>
<string>YourAppName</string>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>
    <string>instagram</string>
</array>
<key>NSLocationWhenInUseUsageDescription</key>
<string>To access your current location</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>To share </string> 

在Objective-C中就是这样。

Swift版本中

在 appdelegate 方法中,我们需要导入这个框架

import FacebookCore
import FacebookLogin
import FBSDKLoginKit

之后在 didFinishLaunchingWithOptions 中写入

FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

并调用回调方法

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url as URL!, sourceApplication: sourceApplication, annotation: annotation)
}

之后在您的视图控制器中导入相同的框架,然后在您的 facebook 登录操作中编写这些代码行

var dict : [String : AnyObject]!

let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager.logIn(withReadPermissions: ["email"], from: self) { (result, error) in
        if (error == nil){
            let fbloginresult : FBSDKLoginManagerLoginResult = result!
            if fbloginresult.grantedPermissions != nil {
                if(fbloginresult.grantedPermissions.contains("email")) {
                    self.getFBUserData()
                }
            }
        }
    }

之后我得到用户数据并存储在字典中

func getFBUserData(){
    if((FBSDKAccessToken.current()) != nil){
        FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
            if (error == nil){
                self.dict = result as! [String : AnyObject]
                print(result!)
                print(self.dict)
            }
        })
    }
} 
于 2017-02-07T11:58:50.710 回答
0

https://developers.facebook.com/features/whats-new-ios-sdk-3.1/

看看这个适用于 iOS5 和 iOS6 的 Facebook sdk 工作。

于 2012-10-05T07:00:43.990 回答