我正在尝试在我的应用中实施一个NSURLProtocol
来获取以myApp://...
开头的网址
我在新的SWIFT文件中创建了协议,并在AppDelegate
:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
NSURLProtocol.registerClass(myURLProtocol)
return true
}
但我一直收到这个错误。我无法弄清楚如何将canInitWithRequest定义为我的webViews ..
2014-08-03 21:10:27.632 SubViewTest [6628:156910] * WebKit被丢弃 一个未被捕获的例外 web视图:decidePolicyForNavigationAction:请求:帧:decisionListener: 委托:* --canInitWithRequest:仅限 为抽象类定义。定义 - [_ TtC11SubViewTest13myURLProtocol canInitWithRequest:!]
答案 0 :(得分:1)
正如您所获得的例外中所述,myURLProtocol
类应该实现canInitWithRequest
类函数/方法;确切地说,它应该覆盖基类的抽象方法。
以下是来自头文件的canInitWithRequest
NSURLProtocol
方法的注释说明:
/*!
@method canInitWithRequest:
@abstract This method determines whether this protocol can handle
the given request.
@discussion A concrete subclass should inspect the given request and
determine whether or not the implementation can perform a load with
that request. This is an abstract method. Sublasses must provide an
implementation. The implementation in this class calls
NSRequestConcreteImplementation.
@param request A request to inspect.
@result YES if the protocol can handle the given request, NO if not.
*/
所以,答案是:在你的myURLProtocol
类下面添加代码:
class func canInitWithRequest(request: NSURLRequest!) -> Bool {
return true;
}
备注:您可能需要在返回true
或false
之前检查请求对象,只是不要忘记添加此代码:)