0

我正在尝试NSURLProtocol在我的应用程序中实现一个以获取开头的 URLmyApp://...

我在一个新的 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 丢弃了 webView:decidePolicyForNavigationAction:request:frame:decisionListener:delegate: * -canInitWithRequest: 仅为抽象类定义的未捕获异常。定义-[_TtC11SubViewTest13myURLProtocol canInitWithRequest:]!

4

1 回答 1

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,只是不要忘记添加此代码:)

于 2014-08-03T19:46:50.663 回答