iOS 版限制登录

借助限制登录,开发者可以表明当前的登录在追踪用户方面会受到限制。

预期情况

在限制登录模式中成功登录后,系统将填充一个全局 AuthenticationToken 实例,该实例会提供关于登录尝试的信息,可用于验证客户端服务器上的身份验证。此外,我们将填充一个共享的个人主页实例,该实例会包含用户的应用范围编号、用户名称和头像等基本信息。

权限

您可以申请的可用权限如下所示:

  • public_profile
  • email
  • gaming_profile
  • gaming_user_picture
  • user_age_range
  • user_birthday
  • user_friends
  • user_gender
  • user_hometown
  • user_link
  • user_location
  • user_messenger_contact

自定义随机数

限制登录允许开发者传递随机数,以用于验证其服务器上的身份验证尝试。有关使用随机数验证口令的信息,请参阅验证限制登录 OIDC 口令

限制

应用切换可将登录对话框展示给 iOS 版 Facebook 应用中的用户(前提是他们已经登录),但应用切换不支持限制登录流程。

全新 API 元素

iOS 版 Facebook SDK 提供了全新的 FBSDKLoginTracking 枚举。可能的值为 enabledlimited。对于限制登录,请使用 limited

enum LoginTracking {
    case enabled
    case limited
}
typedef NS_ENUM(NSUInteger, FBSDKLoginTracking)
{
  FBSDKLoginTrackingEnabled,
  FBSDKLoginTrackingLimited,
} NS_SWIFT_NAME(LoginTracking);

此外,限制登录会使用 FBSDKLoginConfiguration 来修改登录尝试的默认行为。可以使用默认属性、显式属性(仅限 Swift)或以下几种初始化程序之一创建此配置:

init?(
    permissions: Set
initWithPermissions:(NSArray\<NSString *> *)permissions                                                          
           tracking:(FBSDKLoginTracking)tracking
              nonce:(NSString *)nonce

属性

属性描述

requestedPermissions: Set<Permissions> (Swift)

为登录尝试请求的权限。默认为空集。

requestedPermissions: Set<String> (ObjC)

为登录尝试请求的权限。默认为空集。

tracking: LoginTracking

登录追踪首选项。默认为 .enabled

nonce: String

创建配置所用的随机数。如果未为工厂方法提供任何值,则将使用唯一的随机数。

如果未满足以下条件,则创建配置尝试会失败:

  • 随机数必须为不包含空格的非空字符串。

  • 您无法请求超出追踪范围的权限。例如,如果将追踪的值设为 .limited,则对 user_likes 的请求将无效。

  • 如需了解您可以申请的权限,请参阅权限版块。

实现限制登录

要直接使用登录管理器类在您的应用中实现限制登录,请升级到最新的 iOS 版 Facebook SDK,并使用以下代码:

let loginManager = LoginManager()

// Ensure the configuration object is valid
guard let configuration = LoginConfiguration(
	permissions:["email", "user_friends", "user_birthday", "user_age_range", "user_gender", "user_location", "user_hometown", "user_link"],  
	tracking: .limited,
	nonce: "123"
) 
else {
    return
}

loginManager.logIn(configuration: configuration) { result in
    switch result {
    case .cancelled, .failed:
        // Handle error
        break
    case .success:
        // getting user ID
        let userID = Profile.current?.userID

        // getting pre-populated email
        let email = Profile.current?.email

        // getting pre-populated friends list
        let friendIDs = Profile.current?.friendIDs

        // getting pre-populated user birthday
        let birthday = Profile.current?.birthday

        // getting pre-populated age range
        let ageRange = Profile.current?.ageRange

        // getting user gender
        let gender = Profile.current?.gender
  
        // getting user location
        let location = Profile.current?.location
  
        // getting user hometown
        let hometown = Profile.current?.hometown
  
        // getting user profile URL
        let profileURL = Profile.current?.linkURL
  
        // getting id token string
        let tokenString = AuthenticationToken.current?.tokenString
    }
}
FBSDKLoginManager *loginManager = [FBSDKLoginManager new];
FBSDKLoginConfiguration *configuration =
  [[FBSDKLoginConfiguration alloc] initWithPermissions:@[@"email", @"user_friends", @"user_birthday", @"user_age_range", @"user_hometown", @"user_location", @"user_gender", @"user_link"]
                                              tracking:FBSDKLoginTrackingLimited
                                                 nonce:@"123"];
[loginManager logInFromViewController:self
                        configuration:configuration
                           completion:^(FBSDKLoginManagerLoginResult * result, NSError *error) {
  if (!error && !result.isCancelled) {
    // Login successful

    // getting user ID
    NSString *userID =
      FBSDKProfile.currentProfile.userID;

    // getting id token string
    NSString *idTokenString =
      FBSDKAuthenticationToken.currentAuthenticationToken.tokenString;

    // fetching pre-populated email
    NSString *email = FBSDKProfile.currentProfile.email;
  
    // getting pre-populated friends list
    NSArray<FBSDKUserIdentifier *> *friendIDs = FBSDKProfile.currentProfile.friendIDs;

    // getting pre-populated user birthday
    NSDate *birthday = FBSDKProfile.currentProfile.birthday;

    // getting pre-populated age range
    FBSDKUserAgeRange *ageRange = FBSDKProfile.currentProfile.ageRange;  
  
    // getting pre-populated age range
    FBSDKLocation *hometown = FBSDKProfile.currentProfile.hometown;  
  
    // getting pre-populated age range
    FBSDKLocation *location = FBSDKProfile.currentProfile.location;  
  
    // getting pre-populated age range
    NSString *gender = FBSDKProfile.currentProfile.gender;  
  
    // getting pre-populated age range
    NSURL *userLink = FBSDKProfile.currentProfile.linkURL;  
  }
}];

要使用“登录”按钮在您的应用中实现限制登录,请升级至最新的 iOS 版 Facebook SDK,并使用以下代码:

override func viewDidLoad() {
    super.viewDidLoad()

    setupLoginButton()
}
  
func setupLoginButton() {
    loginButton.delegate = self
    
    loginButton.permissions = ["email"]
    loginButton.loginTracking = .limited
    loginButton.nonce = "123" as NSString
}

func loginButton(
    _ loginButton: FBLoginButton,
    didCompleteWith potentialResult: LoginManagerLoginResult?,
    error potentialError: Error?
) {
    if let error = potentialError {
        // Handle Error
    }

    guard let result = potentialResult else {
        // Handle missing result  
    }
        
    guard !result.isCancelled else {
        // Handle cancellation
    }

    // Handle successful login
  
    let userID = Profile.current?.userID
    let email = Profile.current?.email
    let tokenString = AuthenticationToken.current?.tokenString
}
- (void)viewDidLoad
{
  [super viewDidLoad];

  [self setupLoginButton];
}
  
- (void)setupLoginButton
{
  self.loginButton.delegate = self;  
  self.loginButton.permissions = @[@"email"]
  self.loginButton.loginTracking = FBSDKLoginTrackingLimited
  self.loginButton.nonce = @"123"
}

- (void)    loginButton:(FBSDKLoginButton *)loginButton
  didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result
                  error:(NSError *)error
{
  if (error) {
    // Handle error
  }

  if (result && result.isCancelled) {
    // Handle cancellation
  }

  // Handle success
}