objective-c - UIWebView 功能

标签 objective-c ios caching uiwebview

正在开发涉及 UIWebView 的应用程序。 基本问题:- Ui Webview 的行为是否与 safari 完全相同?

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

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

如何使用 UIWebView 来处理这种离线场景。要使用的缓存技术? 请提供代码示例,因为我发现很难找到一些关于此的代码示例。

使用的代码如下:-

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

    }

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

最佳答案

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

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

或者您可以使用 javascript 尝试此操作。 uiwebview 支持 HTML5,因此您可以尝试使用 LocalStorage 并查看其效果。我相信它看起来会很老套。检查此处了解更多 - Accessing UIWebView local storage data from native iPhone app

关于objective-c - UIWebView 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13749851/

相关文章:

ios - "id"类型如何在不强制转换的情况下理解方法的接收者?

ios - 未调用 GTLServiceYouTube executeQuery 回调

iphone - UIImagePickerController 语言未更改

c - 优化嵌套数组值的代码

java - 使用数组实现缓存 - ArrayIndexOutOfBoundsException (Java)

php - 如何获取 youtube 视频的 mp4/mov/m4v 视频链接?

iOS:在 UIActivityViewController 中禁用 WhatsApp?

ios - ios 可以同时使用本地通知和推送通知吗?

c# - AppFabric 缓存服务器和 Web 应用程序在同一台物理机器上

ios - 在多个目标之间导入不同的桥接头