@luckyJerry
2016-03-19T15:20:12.000000Z
字数 1561
阅读 516
iOS

这个类通常不会手动创建,除非配合使用NSURLProtocol。通常是由delegate回调的时候,在参数中提供。
有几个比较重要的属性
@property(readonly, copy) NSURLProtectionSpace *protectionSpace
提供一些Authentication的细节。比如Host,port,realm。最重要的是那种类型的Authentication。是需要Client提供Credential的Authentication,还是让Client进行TLS认证。
@property(readonly, copy) NSURLCredential *proposedCredential
返回一个默认的Credential(猜想是从CredentialStorage里拿的)
如果刚才已经提供过一个Credential但是没认证成功,那么会提供刚才那个认知失败的Credential。
如果这个值不是nil,那么可以调用Credential的hasPassword方法,如果返回YES,那么就原样使用。如果返回NO,就弹个框,让用户输密码。
@property(readonly, retain) id< NSURLAuthenticationChallengeSender > sender
这个sender在使用NSURLSession API的时候,用处不大。因为session的Delegate有个CompletionHandler,参数都是通过这个block传回的。在使用NSURLConnection的时候,就要用这个Sender来返回操作结果。
ProtectionSpace代表了Server上一块需要认证的区域,上面解释过了。
3种类型
@property(readonly) NSURLCredentialPersistence persistence
这个属性是指明创建的这个Credential是否要保存起来。
NSURLCredentialPersistenceNone,
不保存,用完就扔
NSURLCredentialPersistenceForSession,
保存到App退出
NSURLCredentialPersistencePermanent,
保存到KeyChain里去
NSURLCredentialPersistenceSynchronizable
保存到KeyChain里去,还用iCloud同步
一个单例,为某个ProtectionSpace设置Credential