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)

為登入嘗試所要求的權限。預設為空白組合。

requestedPermissions: Set<String>(ObjC)

為登入嘗試所要求的權限。預設為空白組合。

tracking: LoginTracking

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

nonce: String

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

如果不符合以下條件,便無法嘗試建立配置:

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

  • 您無法要求超出追蹤範圍的權限。例如,如果追蹤為 .limited,則要求 user_likes 無效。

  • 請查看權限部分,了解您可以要求哪些權限。

執行限制登入

如要直接使用 Login Manager 類別在應用程式中執行限制登入,請升級至最新的 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
}