0

处理涉及UIWebView. 基本问题:- Ui Webview的行为与safari完全一样吗?

我们最后的场景是使用 ui webview 显示网络应用程序。可以使某些页面在离线模式下工作。如用户上网->浏览网页。现在没有连接,safari 仍然可以从缓存中加载网页根据为该页面所做的设置。(清单文件)。

但是,这种情况在我正在使用的应用程序中不起作用UIWebView。我正在使用 ASIHttp 请求绕过代理并提供凭据。

如何使用UIWebView.Caching 技术处理这种离线场景?请提供代码示例,因为我发现很难找到一些代码示例。

使用的代码如下:-

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];

    }

使用缓存中的数据时,图像不显示。

4

2 回答 2

1

UIWebView与 Safari 不相似。Safari 是一个成熟的网络浏览器,而 webView 更像是准系统。特别是因为从 Objective-C 代码与 javascript 交互是一件非常痛苦的事情。

要执行诸如离线浏览之类的操作,您需要自己实现。要么在objectiveC层做。即首先使用objective-C 下载内容,将其存储在本地cashe 中,然后将代码扔到uiwebview 中。下次从缓存中离线加载时。

或者您可以使用 javascript 尝试此操作。uiwebview支持 HTML5,因此您可以尝试使用LocalStorage并查看效果如何。我的信念是它看起来很老套。在这里查看更多信息 -从本机 iPhone 应用程序访问 UIWebView 本地存储数据

于 2012-12-06T18:17:03.127 回答
0

WebView 和 Safari 比较的一些问答可能对您有所帮助:

Does UIWebView use the same JavaScript engine as Mobile Safari?

UIWebView 没有 Nitro Javascript 引擎,因此它执行 JS 比 Mobile Safari 慢。所以不一样。

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

不确定这个。可能不是。至少 UIWebView 在某些功能上比 Safari 更严格一些。示例:通过 JS 设置宽/高样式时,需要在数字后面加上 'px',而 Mobile Safari 则不需要。

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

如果您想在 App Store 上发布该应用程序,则必须对其进行包装。如果没有,你真的不必。

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

如果你把它包起来,是的。但它有一些限制......

于 2012-12-20T06:53:12.630 回答