gpt4 book ai didi

swift - NSURLProtocol SWIFT 错误

转载 作者:行者123 更新时间:2023-11-30 10:22:57 26 4
gpt4 key购买 nike

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

我在新的 SWIFT 文件中创建了协议(protocol)并在 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 discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: * -canInitWithRequest: only defined for abstract class. Define -[_TtC11SubViewTest13myURLProtocol canInitWithRequest:]!

最佳答案

正如您所得到的异常中所述,myURLProtocol 类应该实现 canInitWithRequest 类函数/方法;准确地说,它应该重写基类的抽象方法。

以下是头文件中 NSURLProtocolcanInitWithRequest 方法的注释描述:

/*!
@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 之前检查请求对象,只是不要忘记添加此代码:)

关于swift - NSURLProtocol SWIFT 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25108172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com