Amit Nalawade
Amit Nalawade

Reputation: 31

UIWebView Functionality

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 .

Upvotes: 0

Views: 841

Answers (2)

Adiee
Adiee

Reputation: 147

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....

Upvotes: 0

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73688

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

Upvotes: 1

Related Questions