好文章收藏夹

详细解析几个和网络请求有关的类(二十一) —— NSURLCre

2018-03-18  本文已影响12人  刀客传奇

版本记录

版本号 时间
V1.0 2018.03.18

前言

我们做APP发起网络请求,一般都是使用框架,这些框架的底层也都是苹果的API,接下来几篇就一起来看一下和网络有关的几个类。感兴趣的可以看上面几篇文章。
1. 详细解析几个和网络请求有关的类 (一) —— NSURLSession
2. 详细解析几个和网络请求有关的类(二) —— NSURLRequest和NSMutableURLRequest
3. 详细解析几个和网络请求有关的类(三) —— NSURLConnection
4. 详细解析几个和网络请求有关的类(四) —— NSURLSession和NSURLConnection的区别
5. 详细解析几个和网络请求有关的类(五) —— 关于NSURL加载系统(一)
6. 详细解析几个和网络请求有关的类(六) —— 使用NSURLSession(二)
7. 详细解析几个和网络请求有关的类(七) —— URL数据的编码和解码(三)
8. 详细解析几个和网络请求有关的类(八) —— 处理重定向和其他请求更改(四)
9. 详细解析几个和网络请求有关的类(九) —— 身份验证挑战和TLS链验证(五)
10. 详细解析几个和网络请求有关的类(十) —— 理解获取缓存(六)
11. 详细解析几个和网络请求有关的类(十一) —— Cookies和自定义协议(七)
12. 详细解析几个和网络请求有关的类(十二) —— URL Session的生命周期(八)
13. 详细解析几个和网络请求有关的类(十三) —— NSURLResponse(一)
14. 详细解析几个和网络请求有关的类(十四) —— NSHTTPCookie(一)
15. 详细解析几个和网络请求有关的类(十五) —— NSHTTPCookieStorage(一)
16. 详细解析几个和网络请求有关的类(十六) —— NSURLCache(一)
17. 详细解析几个和网络请求有关的类(十七) —— NSCachedURLResponse(一)
18. 详细解析几个和网络请求有关的类(十八) —— NSURLAuthenticationChallenge(一)
19. 详细解析几个和网络请求有关的类(十九) —— NSURLProtectionSpace(一)
20. 详细解析几个和网络请求有关的类(二十) —— NSURLCredential(一)

回顾

上一篇讲述了NSURLCredential这个类的详细信息以及一些注意要点,下面这篇我们就主要看一下NSURLCredentialStorage


Overview

一个管理credential存储的对象。

首先看一下该类的基本信息。


Topics

1. Getting the Credential Storage - 获取Credential Storage

2. Getting and Setting Default Credentials - 获取和设置Default Credentials

3. Adding and Removing Credentials - 添加和移除凭证

4. Retrieving Credentials - 检索凭据

5. Constants

6. Notifications


API

下面我们看一下该类的API文档

1. NSURLCredentialStorage本类

/*!
    @class NSURLCredentialStorage
    @discussion NSURLCredentialStorage implements a singleton object (shared instance) which manages the shared credentials cache. Note: Whereas in Mac OS X any application can access any credential with a persistence of NSURLCredentialPersistencePermanent provided the user gives permission, in iPhone OS an application can access only its own credentials.
*/
// NSURLCredentialStorage实现一个管理共享凭证缓存的单例对象(共享实例)。 
// 注意:在Mac OS X中,任何应用程序都可以通过持久化NSURLCredentialPersistencePermanent
// 访问任何凭据,只要用户授予权限即可,在iPhone OS中,应用程序只能访问自己的凭据。

@interface NSURLCredentialStorage : NSObject
{
    @private
    NSURLCredentialStorageInternal *_internal;
}

/*!
    @property sharedCredentialStorage
    @abstract Get the shared singleton authentication storage
    @result the shared authentication storage
*/
// 获取共享验证存储的单例

@property (class, readonly, strong) NSURLCredentialStorage *sharedCredentialStorage;

/*!
    @method credentialsForProtectionSpace:
    @abstract Get a dictionary mapping usernames to credentials for the specified protection space.
    @param space An NSURLProtectionSpace indicating the protection space for which to get credentials
    @result A dictionary where the keys are usernames and the values are the corresponding NSURLCredentials.
*/
// 获取将用户名映射到指定保护空间的凭证的字典

- (nullable NSDictionary<NSString *, NSURLCredential *> *)credentialsForProtectionSpace:(NSURLProtectionSpace *)space;

/*!
    @abstract Get a dictionary mapping NSURLProtectionSpaces to dictionaries which map usernames to NSURLCredentials
    @result an NSDictionary where the keys are NSURLProtectionSpaces
    and the values are dictionaries, in which the keys are usernames
    and the values are NSURLCredentials
*/
// 获取将NSURLProtectionSpaces映射到字典,该字典将用户名映射到NSURLCredentials。

@property (readonly, copy) NSDictionary<NSURLProtectionSpace *, NSDictionary<NSString *, NSURLCredential *> *> *allCredentials;

/*!
    @method setCredential:forProtectionSpace:
    @abstract Add a new credential to the set for the specified protection space or replace an existing one.
    @param credential The credential to set.
    @param space The protection space for which to add it. 
    @discussion Multiple credentials may be set for a given protection space, but each must have
    a distinct user. If a credential with the same user is already set for the protection space,
    the new one will replace it.
*/
// 为指定的保护空间添加一个新的凭证,或替换现有的凭证。
// 可以为给定的保护空间设置多个凭证,但每个凭证都必须具有不同的用户。 
// 如果已经为保护空间设置了具有相同用户的凭证,则新凭证将替换它

- (void)setCredential:(NSURLCredential *)credential forProtectionSpace:(NSURLProtectionSpace *)space;

/*!
    @method removeCredential:forProtectionSpace:
    @abstract Remove the credential from the set for the specified protection space.
    @param credential The credential to remove.
    @param space The protection space for which a credential should be removed
    @discussion The credential is removed from both persistent and temporary storage. A credential that
    has a persistence policy of NSURLCredentialPersistenceSynchronizable will fail.  
    See removeCredential:forProtectionSpace:options.
*/
// 从指定保护空间的集合中删除证书。该证书从永久和临时存储中删除。 
// 拥有NSURLCredentialPersistenceSynchronizable持久性策略的凭证将失败。 
// 请参阅removeCredential:forProtectionSpace:选项

- (void)removeCredential:(NSURLCredential *)credential forProtectionSpace:(NSURLProtectionSpace *)space;

/*!
 @method removeCredential:forProtectionSpace:options
 @abstract Remove the credential from the set for the specified protection space based on options.
 @param credential The credential to remove.
 @param space The protection space for which a credential should be removed
 @param options A dictionary containing options to consider when removing the credential.  This should
 be used when trying to delete a credential that has the NSURLCredentialPersistenceSynchronizable policy.
 Please note that when NSURLCredential objects that have a NSURLCredentialPersistenceSynchronizable policy
 are removed, the credential will be removed on all devices that contain this credential.
 @discussion The credential is removed from both persistent and temporary storage.
 */
// 根据选项从指定保护空间的集合中删除证书。
// @param options 包含删除凭证时要考虑的选项的字典。 尝试删除具有NSURLCredentialPersistenceSynchronizable
// 策略的凭证时应使用此选项。 请注意,当NSURLCredential对象具有NSURLCredentialPersistenceSynchronizable
// 策略时,将删除包含此凭据的所有设备上的凭证。

- (void)removeCredential:(NSURLCredential *)credential forProtectionSpace:(NSURLProtectionSpace *)space options:(nullable NSDictionary<NSString *, id> *)options API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));

/*!
    @method defaultCredentialForProtectionSpace:
    @abstract Get the default credential for the specified protection space.
    @param space The protection space for which to get the default credential.
*/
// 获取指定保护空间的默认凭证

- (nullable NSURLCredential *)defaultCredentialForProtectionSpace:(NSURLProtectionSpace *)space;

/*!
    @method setDefaultCredential:forProtectionSpace:
    @abstract Set the default credential for the specified protection space.
    @param credential The credential to set as default.
    @param space The protection space for which the credential should be set as default.
    @discussion If the credential is not yet in the set for the protection space, it will be added to it.
*/
// 为指定的保护空间设置默认凭证

- (void)setDefaultCredential:(NSURLCredential *)credential forProtectionSpace:(NSURLProtectionSpace *)space;

@end

2. NSURLCredentialStorage分类NSURLSessionTaskAdditions

@interface NSURLCredentialStorage (NSURLSessionTaskAdditions)

- (void)getCredentialsForProtectionSpace:(NSURLProtectionSpace *)protectionSpace task:(NSURLSessionTask *)task completionHandler:(void (^) (NSDictionary<NSString *, NSURLCredential *> * _Nullable credentials))completionHandler API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));
- (void)setCredential:(NSURLCredential *)credential forProtectionSpace:(NSURLProtectionSpace *)protectionSpace task:(NSURLSessionTask *)task API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));
- (void)removeCredential:(NSURLCredential *)credential forProtectionSpace:(NSURLProtectionSpace *)protectionSpace options:(nullable NSDictionary<NSString *, id> *)options task:(NSURLSessionTask *)task API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));
- (void)getDefaultCredentialForProtectionSpace:(NSURLProtectionSpace *)space task:(NSURLSessionTask *)task completionHandler:(void (^) (NSURLCredential * _Nullable credential))completionHandler API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));
- (void)setDefaultCredential:(NSURLCredential *)credential forProtectionSpace:(NSURLProtectionSpace *)protectionSpace task:(NSURLSessionTask *)task API_AVAILABLE(macos(10.10), ios(8.0), watchos(2.0), tvos(9.0));

@end

后记

本篇主要详细解析了类NSURLCredentialStorage,喜欢的给个赞和关注,谢谢~~~

上一篇下一篇

猜你喜欢

热点阅读