UIWebView slow loading using NSUrlProtocol Xamarin.Forms

I'm working on an iOS application using Xamarin.Forms.

This application is using UIWebView controller that shows a web application that is hosting on my server. Each time that I make a request I have to send a custom header in order to identify that this request comes to the mobile application and not from a browser, to do this I'm using an NSUrlProtocol object that overrides the method Request that inserts the custom header on each request.This is my code:

    public override NSUrlRequest Request {
        get {
            NSMutableDictionary headers = null;
            if (null == base.Request.Headers) {
                headers = new NSMutableDictionary ();
            } else {
                headers = new NSMutableDictionary (base.Request.Headers);
            }
            headers.Add(NSObject.FromObject(AppVariables.headerVariable), NSObject.FromObject (AppVariables.appVersion));
            NSMutableUrlRequest newRequest = (NSMutableUrlRequest)base.Request.MutableCopy ();
            newRequest.Headers = headers;
            return newRequest;
        }
    }

The problem that I have right now is that I noticed since I started using the NSUrlProtocol the loading time of the pages is increasing a lot. Right now the loading is taking 10 seconds, before this implementation the page took 3 seconds approximately.

Can anyone please point out some helpful direction to overcome this??


I don't see any reasons for the delay in response time when you're using custom headers. Like Andreas mentioned in the comments, I believe it has to do with your server code. I would recommend profiling your server code.

Do you see similar results when you send the requests (with custom headers) from Fiddler or cURL?


Just like @AndreasPaulsson and @prashant had mentioned, server might be the culprit. I would recommend testing the API with tools like Postman and check the response speed. I would also recommend you to check ModernHttpClient by Paul C Betts. In iOS the library uses NSUrlSession.

链接地址: http://www.djcxy.com/p/34804.html

上一篇: 使用NSURLSession自定义NSURLProtocol

下一篇: UIWebView使用NSUrlProtocol Xamarin.Forms缓慢加载