iOS開發之Accounts框架詳解

iOS開發之Accounts框架詳解

    Accounts框架是iOS原生提供的一套帳戶管理框架,其支持Facebook,新浪微博,騰訊微博,Twitter和領英帳戶管理的功能。須要注意,在iOS 11及以上系統中,將此功能已經刪除,所以Accounts.framework實際上已經沒有太大的意義,其只在iOS 11以前的系統上可用。數據庫

1、Accounts框架概覽

從上圖能夠看出,Accounts框架中最重要的3個類是ACAccountCredential類、ACAccount類和ACAccountStore類。後面咱們着重介紹這3個類。安全

      首先先來看ACAccountType類,這個類用來定義帳戶類型,以下:app

@interface ACAccountType : NSObject
//類型描述
@property (readonly, nonatomic) NSString *accountTypeDescription;
//標識符
@property (readonly, nonatomic) NSString *identifier;
//此類帳戶是否已經受權
@property (readonly, nonatomic) BOOL     accessGranted;
@end

須要注意,這個類中的屬性都是隻讀的,這也就是說,開發者不可以直接使用這個類進行對象的構建,須要藉助ACAccountStore類來建立ACAccountType實例,後面會介紹。框架

      ACErrorCode定義了錯誤碼的意義,以下:async

typedef enum ACErrorCode {
    ACErrorUnknown = 1,//未知錯誤
    ACErrorAccountMissingRequiredProperty,  // 缺乏必選屬性錯誤
    ACErrorAccountAuthenticationFailed,     // 受權失敗
    ACErrorAccountTypeInvalid,              // 受權無效
    ACErrorAccountAlreadyExists,            // 帳戶已經存在
    ACErrorAccountNotFound,                 // 帳戶未找到
    ACErrorPermissionDenied,                // 沒有權限
    ACErrorAccessInfoInvalid,               // 信息失效
    ACErrorClientPermissionDenied,          // 客戶端沒有權限
    ACErrorAccessDeniedByProtectionPolicy,  // 沒法取得證書
    ACErrorCredentialNotFound,              // 證書未找到
    ACErrorFetchCredentialFailed,           // 請求證書失敗
    ACErrorStoreCredentialFailed,           // 存儲證書失敗
    ACErrorRemoveCredentialFailed,          // 刪除證書失敗
    ACErrorUpdatingNonexistentAccount,      // 更新失敗
    ACErrorInvalidClientBundleID,           // 無效的BundleID
    ACErrorDeniedByPlugin,                  // 權限被阻止
    ACErrorCoreDataSaveFailed,              // 數據庫存儲失敗
    ACErrorFailedSerializingAccountInfo,    //序列化數據失敗
    ACErrorInvalidCommand,                  //無效的命令
    ACErrorMissingTransportMessageID,       //缺乏安全信息
    ACErrorCredentialItemNotFound,          // 證書缺乏字段
    ACErrorCredentialItemNotExpired,        // 證書字段失效
} ACErrorCode;

2、進行帳戶操做

    在iOS 11如下的系統中,在設置中能夠找到帳戶管理選項,以下圖:ide

點擊相應的社交平臺,經過帳號密碼能夠進行登陸。這裏一旦設置登陸,那麼在第三方應用程序中即可以經過Accounts框架來獲取受權信息。ui

    首先,要使用Accounts框架,須要導入相應頭文件,以下:atom

#import <Accounts/Accounts.h>

但應用程序首次使用用戶社交平臺的帳戶時,須要獲取用戶的受權,示例代碼以下:spa

//建立操做對象
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
//經過操做對象 構建社交平臺類型示例  這裏採用的新浪微博
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierSinaWeibo];
//進行用戶受權請求
[accountStore requestAccessToAccountsWithType: accountType options:nil completion:^(BOOL granted, NSError *error) {
   if (error) {
        NSLog(@"error = %@", [error localizedDescription]);
   }      
   dispatch_async(dispatch_get_main_queue(), ^{
      if(granted){
         NSLog(@"受權經過了");
    });
}];

獲取用戶受權界面以下圖所示:code

一旦用戶受權經過,可使用以下方法獲取到用戶的社交平臺帳戶信息:

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierSinaWeibo];
NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];

Accounts框架支持的社交平臺類型ID以下:

NSString * const ACAccountTypeIdentifierTwitter;//Twitter
NSString * const ACAccountTypeIdentifierFacebook;//Facebook
NSString * const ACAccountTypeIdentifierSinaWeibo;//新浪微博
NSString * const ACAccountTypeIdentifierTencentWeibo;//騰訊微博
NSString * const ACAccountTypeIdentifierLinkedIn;//領英

在調用requestAccessToAccountsWithType方法時,能夠傳入一個字典參數,某些社交平臺的受權須要配置一些額外參數,例如:

//facebook相關
NSString * const ACFacebookAppIdKey;//設置appid
NSString * const ACFacebookPermissionsKey;//設置權限key
NSString * const ACFacebookAudienceKey; //權限key
NSString * const ACFacebookAudienceEveryone;//公開權限
NSString * const ACFacebookAudienceFriends;//好友權限
NSString * const ACFacebookAudienceOnlyMe;//私人權限
//領英相關
NSString * const ACLinkedInAppIdKey;//領英appid
NSString * const ACLinkedInPermissionsKey; //設置權限key
//騰訊微博
NSString *const ACTencentWeiboAppIdKey; //設置appid

3、用戶信息相關類解析

ACAccount類解析以下:

@interface ACAccount : NSObject
- (instancetype)initWithAccountType:(ACAccountType *)type;//構造方法
@property (readonly, weak, NS_NONATOMIC_IOSONLY) NSString      *identifier;//標識符
@property (strong, NS_NONATOMIC_IOSONLY)   ACAccountType       *accountType;//帳戶類型
@property (copy, NS_NONATOMIC_IOSONLY)     NSString            *accountDescription;//帳戶描述
@property (copy, NS_NONATOMIC_IOSONLY)     NSString            *username;//用戶名
@property (readonly, NS_NONATOMIC_IOSONLY)  NSString           *userFullName;//完整名稱
@property (strong, NS_NONATOMIC_IOSONLY)   ACAccountCredential *credential;//受權憑證
@end

ACAccountCredential類解析以下:

@interface ACAccountCredential : NSObject
//構造方法
- (instancetype)initWithOAuthToken:(NSString *)token tokenSecret:(NSString *)secret;
- (instancetype)initWithOAuth2Token:(NSString *)token 
                       refreshToken:(NSString *)refreshToken
                         expiryDate:(NSDate *)expiryDate;
//token
@property (copy, nonatomic) NSString *oauthToken;
@end

4、ACAccountStore類解析

@interface ACAccountStore : NSObject
//全部帳戶列表
@property (readonly, weak, NS_NONATOMIC_IOSONLY) NSArray *accounts;
//根據id獲取帳戶
- (ACAccount *)accountWithIdentifier:(NSString *)identifier;
//根據id獲取帳戶類型對象
- (ACAccountType *)accountTypeWithAccountTypeIdentifier:(NSString *)typeIdentifier;
//獲取指定類型的全部帳戶
- (NSArray *)accountsWithAccountType:(ACAccountType *)accountType;
//進行帳戶存儲
- (void)saveAccount:(ACAccount *)account withCompletionHandler:(ACAccountStoreSaveCompletionHandler)completionHandler;
//進行帳戶使用權限申請
- (void)requestAccessToAccountsWithType:(ACAccountType *)accountType
                  withCompletionHandler:(ACAccountStoreRequestAccessCompletionHandler)handler NS_DEPRECATED(NA, NA, 5_0, 6_0);
- (void)requestAccessToAccountsWithType:(ACAccountType *)accountType
                                options:(NSDictionary *)options
                             completion:(ACAccountStoreRequestAccessCompletionHandler)completion;
//若是帳戶權限已通過期 調用此方法進行刷新
- (void)renewCredentialsForAccount:(ACAccount *)account completion:(ACAccountStoreCredentialRenewalHandler)completionHandler;
//刪除帳戶
- (void)removeAccount:(ACAccount *)account withCompletionHandler:(ACAccountStoreRemoveCompletionHandler)completionHandler;
@end

千里之遙始於足下,冰凍三尺非一日之寒

——共勉

相關文章
相關標籤/搜索