Using a custom NSURLProtocol with UIWebView and POST requests

后端 未结 2 1169
梦如初夏
梦如初夏 2021-01-04 23:44

In my iOS app, I\'m using a UIWebView and a custom protocol (with my own NSURLProtocol implementation). I\'ve been fairly careful about making sure that whenever I load a

2条回答
  •  渐次进展
    2021-01-05 00:43

    Unfortunately it looks like that http: and https: scheme requests are handled slightly differently than other (including custom) schemes by Foundation Framework. Obviously HTTPBody and HTTPBodyStream calls on relevant NSURLRequest returns always nil for former ones. This is decided already prior call of [NSURLProtocol canInitWithRequest] therefore custom NSURLProtocol implementation has no way of influencing that (it is too late).

    It seems that different NSURLRequest class is used for http: and https: than 'a default one'. Default GnuStep implementation of this class returns always nil from HTTPBody and HTTPBodyStream calls. Therefore particular implementations (e.g. one under PhoneGap, likely part of Foundation Framework) choose NSURLRequest-type of class based on scheme prior consulting that with NSURLProtocol. For custom schemes, you get NSURLRequest that returns nil for both HTTPBody and HTTPBodyStream which effectively disables use of POST method (and other methods with body) in custom URI scheme handler.

    Maybe there is a way how to influence decision of which NSURLRequest class is actually used but it is currently unknown to me.

    As a workaround, you can still use http: or https: scheme and decide in [NSURLProtocol canInitWithRequest] based on other criteria (e.g. host name).

提交回复
热议问题