0

I am trying to implement an NSURLProtocol in my app that will fetch URLs starting with myApp://...

I created the Protocol in a new SWIFT file and implemented in the AppDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
    NSURLProtocol.registerClass(myURLProtocol)
    return true
} 

But i keep getting this error. I can't figure out how to define the canInitWithRequest to my 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:]!

Keenle
  • 12,010
  • 3
  • 37
  • 46
Tosen
  • 76
  • 1
  • 5

1 Answers1

1

As noted in the exception which you've got, myURLProtocol class should implement canInitWithRequest class function/method; to be precise it should override abstract method of base class.

Here is the annotation-description of canInitWithRequest method for NSURLProtocol from header file:

/*!
@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.
*/

So, the answer is: add code bellow to your myURLProtocol class:

class func canInitWithRequest(request: NSURLRequest!) -> Bool {
    return true;
}

Remarks: you might need to examine request object before returning true or false, just do not forget to add this code :)

Keenle
  • 12,010
  • 3
  • 37
  • 46