iosnsurlsessionurl-schemensurlprotocolnsurlsessionconfiguration

Register NSURLProtocol (URLProtocol) class for use with external app?


I want to use a custom NSURLProtocol class to serve static data for testing and I have done this implementing the protocol in the application itself and registering it to the NSURLSessionConfiguration.protocolClasses. I want to move the protocol class to another app and register it for other applications to use. Is this possible?

Apple Documentation for registerClass: states:

Attempts to register a subclass of NSURLProtocol, making it visible to the URL loading system.

In the new app that I want to use as the protocol, I register the protocol but the external app doesn't call into it.

// Swift 3
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    URLProtocol.registerClass(MyCustomURLProtocolClass)
}

I want an external app to be able to use my protocol class with NSURLSession and call it with a url such as foo://externalApplication.com.

Thanks for the help!


Solution

  • NSURLProtocol registration is inherently per-process. It is not possible to register a protocol in one process for use it in another, because that would be a security hole, allowing any app to trivially manipulate any other app's traffic.

    Assuming this is just for testing purposes, and assuming you are in control of both apps, you might consider either A. writing your server code in an app extension or B. using openURL to establish a communication channel between the apps like this:

    For details, and for more inter-application communication tips and techniques, I would suggest reading this article: http://www.goldmansachs.com/what-we-do/engineering/see-our-work/app-to-app-comm.html