首页 > 学院 > 开发设计 > 正文

URLProtocol

2019-11-09 14:59:38
字体:
来源:转载
供稿:网友

NSURLPRotocol主要用于处理特定协议的数据加载。它本身是一个抽象类,通过继承它我们可以自定义任何网络协议来返回给app数据,还可以拦截非法请求。

Symbols

1 Creating Protocol Objects

// 初始化public init(request: URLRequest, cachedResponse: CachedURLResponse?, client: URLProtocolClient?)

2 Registering and Unregistering Protocol Classes

// 注册实现的子类open class func registerClass(_ protocolClass: Swift.AnyClass) -> Bool// 移除注册的实现子类open class func unregisterClass(_ protocolClass: Swift.AnyClass)

3 Determining If a Subclass Can Handle a Request

// 是否拦截该请求,并处理open class func canInit(with request: URLRequest) -> Bool

4 Getting and Setting Request Properties

// 通过属性key获取值open class func property(forKey key: String, in request: URLRequest) -> Any?// 动态添加属性可以和对应的值 open class func setProperty(_ value: Any, forKey key: String, in request: NSMutableURLRequest)// 移除属性key和对应的值open class func removeProperty(forKey key: String, in request: NSMutableURLRequest)

5 Providing a Canonical Version of a Request

// 将拦截的请求转换为另一个请求处理open class func canonicalRequest(for request: URLRequest) -> URLRequest

6 Determining If Requests Are Cache Equivalent

// 验证两个请求是否使用同样的缓存open class func requestIsCacheEquivalent(_ a: URLRequest, to b: URLRequest) -> Bool

7 Starting and Stopping Downloads

// 开始加载数据open func startLoading()// 加载数据结束open func stopLoading()

8 Getting Protocol Attributes

// 数据加载器open var client: URLProtocolClient? { get }// 发出的请求open var request: URLRequest { get }// 缓存数据@NSCopying open var cachedResponse: CachedURLResponse? { get }

9 Initializers

// 初始化@available(iOS 8.0, *)public convenience init(task: URLsessionTask, cachedResponse: CachedURLResponse?, client: URLProtocolClient?)

10 Instance Properties

// 会话任务@available(iOS 8.0, *)@NSCopying open var task: URLSessionTask? { get }

11 Type Methods

// 是否拦截处理会话任务@available(iOS 8.0, *)open class func canInit(with task: URLSessionTask) -> Bool

 


Appendix

Sample Code

Swift

URLProtocol

Revision History

时间 描述
2016-02-10 博文完成

CSDN:http://blog.csdn.net/y550918116j

GitHub:https://github.com/937447974


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表