Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Thelle93

macrumors newbie
Original poster
Oct 7, 2012
18
0
I have an UIWebView in my iOS app that loads different url's depending on a previous action. I wan't these pages to load as fast as possible. I found the class EGOCache (source) and I i got it working to store cacheData in library/Caches directory. But I'm not sure how to retrieve this cache to load it faster, I can't see the difference. Maybe use NSCache? What have I missed?
Code:
- (void)webViewDidStartLoad:(UIWebView *)webView {
        if (webView_1) {


        NSString *urlAddress = @"http://www.apple.com";
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 10.0];
        [webView1 loadRequest:request];

        NSData *data0 = [NSURLConnection sendSynchronousRequest:
                        [NSURLRequest requestWithURL:url]
                                             returningResponse:nil
                                                         error:nil];

        [[EGOCache globalCache] setData:data0 forKey:@"webCache"];
        }
    }
Thanks!
 
You get it back by

Code:
NSData *storedPage = [[EGOCache globalCache] dataForKey:@"webCache"];

The above will return nil if nothing has been saved in that key.

I'd recommend changing your key to the URL string of the page, setting a cache time ( see withTimeoutInterval: ) and checking your cache before you set up NSURLRequest.
 
update

You get it back by

Code:
NSData *storedPage = [[EGOCache globalCache] dataForKey:@"webCache"];

The above will return nil if nothing has been saved in that key.

I'd recommend changing your key to the URL string of the page, setting a cache time ( see withTimeoutInterval: ) and checking your cache before you set up NSURLRequest.

Thanks, I think I got it working now. but how do I set the cache time?

Code:
NSString *urlAddress = @"http://www.apple.com";
        NSURL *url = [NSURL URLWithString:urlAddress];

        NSString* cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString* file = [cachePath stringByAppendingPathComponent:@"/xxx.xxx/EGOCache/EGOCache.plist"];
        BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:file];
        
        if (fileExists)
        {
            NSData *data = [[EGOCache globalCache] dataForKey:urlAddress];
            data = [NSURLConnection sendSynchronousRequest:
                    [NSURLRequest requestWithURL:url]
                                         returningResponse:nil
                                                     error:nil];
            NSLog(@"loading cache");
            
        }else if(!fileExists){
            NSData  *data = [NSURLConnection sendSynchronousRequest:
                             [NSURLRequest requestWithURL:url]
                                                  returningResponse:nil
                                                              error:nil];
            [[EGOCache globalCache] setData:data forKey:urlAddress];
            NSLog(@"saved cache");
        }
        NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 10.0];

        [webView_1 loadRequest:request];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.