【问题标题】:NSURLProtocol SWIFT errorNSURLProtocol SWIFT 错误
【发布时间】:2014-08-03 19:14:44
【问题描述】:

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

我在一个新的 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: 委托:* -canInitWithRequest:仅 为抽象类定义。定义 -[_TtC11SubViewTest13myURLProtocol canInitWithRequest:]!

【问题讨论】:

  • 你想从其他应用程序打开你的应用程序吗?你在寻找 url 方案吗??

标签: swift nsurl nsurlprotocol


【解决方案1】:

正如您遇到的异常中所述,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 之前检查请求对象,只是不要忘记添加此代码:)

【讨论】:

  • @Tosen,你试过我在回答中建议的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-27
  • 2017-03-20
相关资源
最近更新 更多