Tuesday, March 15, 2011

Is that possible to cache UIWebView in iPhone?

I was using NYTimes iPhone application, I become bit curious when I notice that it cache UIwebview even though I didn't open that article.Does anyone have idea how to do that?

How NYTimes iPhone application doing offline reading?

Any help greatly appreciated.

Thanks,

Amit

From stackoverflow
  • You can use UIWebView to load html files stored locally on the iPhone.

    NYTimes app is probably caching html and images in local storage.

    Search google for "UIWebView local" and you get several useful hits.

    I tried it out and it works great:


    First, create a "view based application" and add a UIWebView to the NIB.

    Second, add this code to your UIViewController code:

    - (void)viewDidLoad 
    {
    
        NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
        NSURL *url = [NSURL fileURLWithPath:path isDirectory:NO];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
        [webView loadRequest:request];
    
    
        [super viewDidLoad];
    }
    

    Third, add a file called "index.html" to your "Resources" folder in xcode and it will be displayed.

    bentford : The code works, but I'm not sure I described the app creation clearly. Let me know if it helps!

0 comments:

Post a Comment