我正试图在我的应用程序中实现一个NSURLProtocol
,它将从myApp://...
我在一个新的SWIFT文件中创建了协议,并在AppDelegate
中实现:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
NSURLProtocol.registerClass(myURLProtocol)
return true
}
但是我一直得到这个错误。我不知道如何定义canInitWithRequest到我的webViews..
SubViewTest[6628:156910] * WebKit丢弃类中未捕获的异常webView: decidePolicyForNavigationAction:要求:框架:decisionListener:delegate: * -canInitWithRequest: only为抽象类定义。定义- [_TtC11SubViewTest13myURLProtocolcanInitWithRequest:] !
在你得到的例外中注意到,myURLProtocol
类应该实现canInitWithRequest
类函数/方法;确切地说,它应该覆盖基类的抽象方法。
下面是头文件中NSURLProtocol
的canInitWithRequest
方法的注释描述:
/*!
@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
之前检查请求对象,只是不要忘记添加此代码:)