NSURLProtocol SWIFT错误

时间:2014-08-03 19:14:44

标签: swift nsurl nsurlprotocol

我正在尝试在我的应用中实施一个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:!]

1 个答案:

答案 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;
}

备注:您可能需要在返回truefalse之前检查请求对象,只是不要忘记添加此代码:)