0

When I use NSURLProtocol to catch the requests from WKWebView, the body is lost. Any way to fix this issue?

Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
Jerrold
  • 47
  • 1
  • 3

1 Answers1

0

NSURLRequest objects never have body data in protocols. You have to instead get the body stream, open it, and read the data in a streaming fashion.

For best performance, you should also resend that data to the write end of a new bound stream pair and provide the read end as the body stream in the new request. (Use the CFStreamCreateBoundPair function in the Core Foundation API and use a bridged cast it to cast the CFStreamRef object to an NSStream object.)

dgatwood
  • 10,129
  • 1
  • 28
  • 49
  • Where should I do this ? NSURLProtocol delegate method ? Which one please – Jerrold Jan 03 '18 at 03:30
  • Several methods. Minimally: store a reference to the stream in `initWithRequest:cachedResponse:client:`, create the stream pair and assign a custom stream delegate class instance to provide the data for it in `startLoading`, and stop any in-progress loading in stopLoading. – dgatwood Jan 03 '18 at 14:10
  • Wait a sec. I just reread the question. WKWebView is out-of-process, and doesn't support NSURLProtocol at all, so you won't get any call there, with a body or not. Was this question supposed to be about UIWebView? *confused* – dgatwood Dec 12 '19 at 05:40
  • NO, this question only about WKWebview, UIWebView works perfectly – Jerrold Jun 30 '23 at 02:36