5 Replies
      Latest reply on May 26, 2017 8:31 AM by eskimo
      BalanPrabhu Level 1 Level 1 (0 points)


        How to provide custom response for my network requests in WKWebView?


        I know it is possible in UIWebView using NSURLProtocol, but not with WKWebView. So is there any alternative in WKWebView to achieve this?

        • Re: NSURLProtocol in WKWebView
          eskimo Apple Staff Apple Staff (9,385 points)

          WKWebView does all of its networking out of process, which means it does not ‘see’ your NSURLProtocol subclass.

          What’s the goạl of your NSURLProtocol here?  The NSURLProtocol-under-UIWebView thing was always a bit of a hack, and in many cases WKWebView provides better techniques with real APIs.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: NSURLProtocol in WKWebView
              da_gagnon Level 1 Level 1 (0 points)

              Hi,

               

              We are trying to port our Cordova app to WKWebView. But the Cordova File plug-in use a custom scheme to work. With UIWebView, this plug-in was intercepting URLs with custom scheme using NSURLProtocol to load related files (local resources files). It seem that it is not possible to do that with WKWebView, and it is a blocker for us.


              There is another strategy to do that in WKWebView?

              Do we need to wait iOS 11 to have this kind of feature?

               

              Thanks!

                • Re: NSURLProtocol in WKWebView
                  eskimo Apple Staff Apple Staff (9,385 points)

                  There is another strategy to do that in WKWebView?

                  That depends on how you load the resources.  If you’re in control of the JavaScript code that loads the resources, you can take advantage of WKWebView nice JavaScript/native integration points.  However, if the resources are loaded by the web view itself, you really need an NSURLProtocol and that’s hard to avoid.

                  Do we need to wait iOS 11 to have this kind of feature?

                  You can certainly file an enhancement request for the features you need.  However, if you were in your shoes I’d discuss this with the Cordova folks; it seems like they should have a strategy for dealing with the reality of WKWebView.

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"

                    • Re: NSURLProtocol in WKWebView
                      ltr31 Level 1 Level 1 (0 points)

                      Hi eskimo

                       

                      We are using UIWebView + NSUrlProtocol to display content offline.

                      Images are preloaded in the app filesystem and served with URLs that are "catched" by NSUrlProtocol and served to the UIWebView as if the user is online.

                       

                      We cannot rely on web traditional APIs for that (thinking of HTML5 AppCache) because it is too limited in size for this purpose.

                       

                      Any idea to achieve this with WKWebView ?

                       

                      Thanks

                       

                      Lionel

                        • Re: NSURLProtocol in WKWebView
                          eskimo Apple Staff Apple Staff (9,385 points)

                          Any idea to achieve this with WKWebView ?

                          As things currently stand there’s no direct way to achieve this goal with WKWebView.  If you have control over the HTML content then you could customise it so that it works when loaded directly off the local file system.

                          Share and Enjoy

                          Quinn “The Eskimo!”
                          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                          let myEmail = "eskimo" + "1" + "@apple.com"

                          WWDC runs Mon, 5 Jun through to Fri, 9 Jun.  During that time all of DTS will be at the conference, helping folks out face-to-face. http://developer.apple.com/wwdc/