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

自訂 Nonce

限制登入可讓開發人員傳送 nonce,以用於驗證其伺服器上的身分驗證嘗試。如需使用 nonce 來驗證權杖的相關資訊,請參閱驗證限制登入 OIDC 權杖

限制

在有限的登入流程中,不支援應用程式切換(在用戶已登入 iOS 版 Facebook 應用程式的情況下,應用程式切換會對用戶顯示登入對話方塊)。

新的 API 元素

Facebook iOS 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)

登入嘗試要求的權限。預設為空集合。

requestedPermissionsSet<String>(ObjC)

登入嘗試要求的權限。預設為空集合。

trackingLoginTracking

登入追蹤偏好設定。預設為 .enabled

nonceString

用來建立配置的 nonce。如果沒有為原廠方法提供任何 nonce,則將使用不重複的 nonce。

如果不符合以下條件,嘗試建立配置將失敗:

  • Nonce 必須是不包含空格的非空字串。

  • 您不能要求超出追蹤範圍的權限。例如,如果追蹤為 .limited,要求 user_likes 將失敗。

  • 若要查看您可以要求的權限,請參閱權限小節。

實作限制登入

若要直接使用登入管理員類別在您的應用程式中實作限制登入,請升級到最新的 Facebook iOS 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;  
  }
}];

若要使用登入按鈕在您的應用程式中實作限制登入,請升級到最新的 Facebook iOS 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
}