0

Working on a application that involves UIWebView . Basic Question :- Does Ui Webview behave exactly as safari ?

Scenario at our end is displaying web apps using ui webview .Some Pages can be made to work in offline mode .As in , user goes online - > browses web page .Now there is no connection , safari can still load web page from its cache as per settings made for that page .( manifest file ) .

However this scenario does not work in my application which is using UIWebView . I am using ASIHttp request to bypass proxy and provide credentials .

How can this offline scenario be handled using UIWebView .Caching techniques to be used ? Please provide code sample , because i am finding it difficult to find some code samples on this .

The code used is as follows :-

ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:navigationURL];


    [request1 setDownloadCache:[ASIDownloadCache sharedCache]];

    [[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders:NO];

    [request1 setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];

    [request1 setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];



    [request1 setDownloadDestinationPath:
     [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest:request1]];


 // ************   Network Status check   

 NetworkStatus status = [self.retrive1.internetreachbility currentReachabilityStatus];


    if (status == ReachableViaWiFi || status == ReachableViaWWAN) {

        [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];        
    [request1 setShouldPresentProxyAuthenticationDialog:YES];
    [request1 setShouldPresentCredentialsBeforeChallenge:YES];
    [request1 setShouldPresentAuthenticationDialog:YES];
    [request1 setUsername:retrive1.username];
    [request1 setPassword:retrive1.password];

    [request1 setDelegate:self];

        [request1 setDidFinishSelector:@selector(webPageFetchSucceeded:)];

    [request1 startAsynchronous];

    }


    else

    {

        NSLog(@"app is offline");


        [request1 useDataFromCache];

        BOOL success = [request1 didUseCachedResponse];
        NSLog(@"------------>>>>>>> Success is %@\n", (success ? @"YES" : @"NO"));


        off_status.text = [NSString stringWithFormat:@"Success is %@", (success ? @"YES" : @"NO")];


// Not using this method .

        *NSData *responseData = [request1 responseData];

   [web loadData:responseData MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:nil];*



// Using this method 

        NSString *response = [NSString stringWithContentsOfFile:       
                              [request1 downloadDestinationPath] encoding:[request1 responseEncoding] error:nil];

    [web loadHTMLString:response baseURL:nil];

    }

Images are not getting displayed when using data from cache .

  • Does this address your question? Slightly different topic, but touches on similar issues. http://stackoverflow.com/questions/1348696/how-to-cache-content-in-uiwebview-for-faster-loading-later-on – Rob Dec 06 '12 at 18:14

2 Answers2

1

UIWebView is not similar to Safari. Safari is a full fledged web browser where as webView is more of barebones. Especially since interacting with javascript from Objective-C code is a real pain.

To do something like offline browsing you need to implement your own. either do it at objectiveC layer. i.e. first download content using objective-C, store it in local cashe then throw the code in uiwebview. next time when offline load from cache.

Or you can try this using javascript. uiwebview supports HTML5 so you can try using LocalStorage and see how it works out. My belief is it would look hacky. Check here for more - Accessing UIWebView local storage data from native iPhone app

Community
  • 1
  • 1
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • that could be because manybe the images are not getting cached & its trying to download the image when in offline. & since you are offline the download will fail... – Srikar Appalaraju Dec 10 '12 at 07:49
  • I was under the impression that when we use cache it will store all contents viewed online .So that when user goes offline he can browse through the same .Attaching code snippet in main thread. – Amit Nalawade Dec 10 '12 at 07:58
  • [web loadHTMLString:response baseURL:nil]; changing this to [web loadHTMLString:response baseURL:[request1 url]]; helped me get images as well in offline mode.only issue being some images are not getting displayed/dowloaded ..could cache storage capacity be a issue ? – Amit Nalawade Dec 10 '12 at 10:23
  • could be, i dont know what cache uiwebview uses. HTML5 in most browsers cache store supports of 5MB in size. dont know what cache configuration uiwebview uses. please update if you find any answer... – Srikar Appalaraju Dec 10 '12 at 11:10
  • [[ASIDownloadCache sharedCache]setDiskCapacity:512*1024] says unrecognized selector sent to instance ASIdownload cache .How can we increase cache size ? – Amit Nalawade Dec 11 '12 at 04:46
0

Some Questions and answers of WebView and Safari comparison may help you:

Does UIWebView use the same JavaScript engine as Mobile Safari?

UIWebView does not have the Nitro Javascript engine, so it executes JS slower than Mobile Safari. So it's not the same.

Also, does UIWebView support all HTML5 features like Mobile Safari does? I am specifically concerned about Web SQL and Web Workers

Not sure about this one. Probably not. At least UIWebView is a bit more strict than Safari on certain features. Example: when setting a width/height style through JS, you need to add 'px' behind the number, while Mobile Safari does not require that.

If I have an app which is written purely in HTML and JS, should I wrap it up in a UIWebView or should I have it open in Mobile Safari

If you want to release the app on the App Store, you will have to wrap it. If not, you don't really have to.

Are pure HTML and JS apps accepted on the Apple store?

If you wrap it, yes. But it has some limitations....

Adiee
  • 137
  • 1
  • 12