Questions tagged [nsurlprotocol]

NSURLProtocol is an abstract class that provides the basic structure for performing protocol-specific loading of URL data.

NSURLProtocol is available in iOS 2.0 and later with iOS and available in OS X v10.2 with Safari 1.0 installed ,available in OS X v10.2.7 and later with OSX.

An NSURLProtocol object handles the loading of protocol-specific URL data. The NSURLProtocol class itself is an abstract class that provides the infrastructure for processing URLs with a specific URL scheme. You create subclasses for any custom protocols or URL schemes that your app supports.

Apps never need to instantiate an NSURLProtocol subclass directly. When a download starts, the system creates the appropriate protocol object to handle the corresponding URL request. All you have to do is define your protocol class and call the registerClass: class method during your app’s launch time so that the system is aware of your protocol.

Source : NSURLProtocol Class Reference

NOTE

You cannot use this class to define custom URL schemes and protocols in watchOS 2 and later.
142 questions
3
votes
2 answers

Intercept *all* requests from Android WebView

The title says it all. I would like to know about everything what the WebView is asking for. Some weird limitations like not knowing about XHR POST are not acceptable. In other words, something like iOS have in NSURLProtocol. That one is…
Pavel Zdenek
  • 7,146
  • 1
  • 23
  • 38
3
votes
2 answers

How to intercept networking calls (REST) in iOS using NSURLProtocol

I need to intercept the networking call that the client will make in the application while the call will successfully complete. I found out that a solution is implementing the NSURLProtocol abstract class and register to the application. This seemed…
George Taskos
  • 8,324
  • 18
  • 82
  • 147
3
votes
0 answers

NSPOSIXErrorDomain Code-54. "Operation couldn't be completed. Connection reset by peer"

Getting the above error when trying to redirect the entered URL through proxy server to a new request. NSMutableURLRequest *newRequest = [self.request mutableCopy]; newRequest.HTTPMethod = @"GET"; [newRequest setHTTPBody:nil]; [newRequest…
Leo
  • 31
  • 4
3
votes
1 answer

Modify headers in NSHTTPURLResponse

Is it possible to modify a header on a NSHTTPURLResponse without using the method initWithURL:statusCode:HTTPVersion:headerFields: to create a new customized NSHTTPURLResponse
2
votes
0 answers

How to use URLSessionWebSocketTask + URLProtocol properly

Http connection + URLProtocol works fine. But now I try to include websockets in this scheme and it hasn't been working. Here is websockets level: var urlSession = URLSession(configuration: myConfiguration, delegate: nil, delegateQueue: .main) …
2
votes
1 answer

How to load nil Data with a URLProtocol subclass?

I'm currently trying out a method of stubbing my network requests without needing to mock URLSession by using URLProtocol. So far it works pretty well and functions by calling self.client?.urlProtocol(_ URLProtocol, didLoad: Data). However I'm…
Declan McKenna
  • 4,321
  • 6
  • 54
  • 72
2
votes
1 answer

custom URLProtocol cannot work with AVPlayer

goal: network flow collection in my app. For collecting all network flow, i custom URLProtocol. it all work fine with normal GET, or POST request of Json. But it makes audio playing part not working. My Audio playing part is based on AVPlayer and…
Jimmy
  • 55
  • 4
2
votes
2 answers

How do you change the content-type of a custom NSURLProtocol?

I've written a custom subclass of NSURLProtocol, however it seems that whenever I use the protocol to load a request in UIWebView it assumes the data is content-type "text/html". Is there a way to specify that this content is actually something…
BadPirate
  • 25,802
  • 10
  • 92
  • 123
2
votes
0 answers

How to detect whether a link clicked should be opened in same UIWebview or external?

I have a webview which I am using as a content player. I want to detect whether a link clicked(like policy or t&c) should be opened in external webview or in same webview. As per UIWebView documentation, shouldStartLoadWithRequest delegate method…
Kishan Lal
  • 121
  • 7
2
votes
2 answers

When to use NSURLProtocol?

I am communicating over 'http://' and 'https://'. When and why to use NSURLProtocol? I read that it is used for new/custom schemes and for the above schemes we do not need to use NSURLProtocol.
Utsav Dusad
  • 2,139
  • 4
  • 31
  • 52
2
votes
3 answers

Provide HTTPVersion to NSHTTPURLResponse

My application uses custom NSURLProtocol subclass. I need to substitute NSHTTPURLResponse with my copy, modifying some of the headers fields. So, I create new NSHTTPURLResponse instance, like that: @implementation NSHTTPURLResponse (CocoaFix) -…
Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42
2
votes
1 answer

Custom NSURLProtocol subclasses are not available to background sessions

From NSURLSession.h... /* An optional array of Class objects which subclass NSURLProtocol. The Class will be sent +canInitWithRequest: when determining if an instance of the class can be used for a given URL scheme. You should not use…
72A12F4E
  • 1,744
  • 1
  • 14
  • 28
2
votes
1 answer

Custom scheme/protocol access from SSL?

Our iOS application uses a custom protocol (NSURLProtocol) to directly manage loading certain data needed by a UIWebview. This has worked fine until iOS 9 with XCode 7. Now, since the main site is loaded using HTTPS, the UIWebview throws an error…
bruth
  • 61
  • 1
  • 9
2
votes
1 answer

NSURLProtocol + UIWebView + certain domains = app UI frozen

We're building a browser for iOS. We decided to experiment with using a custom NSURLProtocol subclass in order to implement our own caching scheme and perform user-agent spoofing. It does both of those things quite well...the problem is, navigating…
Reid
  • 1,109
  • 1
  • 12
  • 27
2
votes
1 answer

Custom NSURLProtocol added to configuration.protocolClasses prevents working of default url protocols

I try to add a custom NSURLProtocol to a NSURLSession in order to serve test data for some calls, while providing the normal internet connectivity for all other calls. I need to do this by adding the NSURLProtocol to the session configuration's…
TheEye
  • 9,280
  • 2
  • 42
  • 58