views:

32

answers:

1

Pseudo code

pageUrl = "http://www.google.com"

if(pageUrl == never Downloaded)
  Download pageUrl
else
{
   Display Saved Version
   Mean while download pageUrl
   When done display new version

}

How can I do something like this in objective C for a UIWebview? Also what's the best way to save web pages for this scenario? PList, SQLite?

+1  A: 

Plist is the best way to solve your problem. iPhone/Objective-c can access very quickly to plist file as compare to SQLite Database.

Let me give you some sample code.

See - edit After some time.

Edit :

  • Steps for Creating project & connecting web-view
    • Create New Project -> View Based Application.
    • Give name "yourProjName" ( up to you what you give )
    • Open "yourProjNameViewController.xib"
    • Drag & drop UIWebView
    • Open "yourProjNameViewController.h" File
    • Place a variable IBOutlet UIWebView *wView;
    • Connect in interface builder
  • Steps for adding Property list file to your project

    • Expand Resources Group under your project tree
    • Right click on "Resources" -> Add -> New File
    • Select Template Category - osx -> Resource
    • Select Property List file
    • Give file name "LoadedURL.plist"
    • Change Root type to Array
    • Save "LoadedURL.plist" file
  • Now place following code to "yourProjNameViewController.m" file.


#import "yourProjNameViewController.h"
#define documentsDirectory_Statement NSString *documentsDirectory; \
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); \
documentsDirectory = [paths objectAtIndex:0];


@implementation WebViewLoadViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // your url to load
    NSString *strToLoad=@"http://www.mail.yahoo.com";


    // file management code
    // copy file to documents directory
    documentsDirectory_Statement;
    NSFileManager *fm=[NSFileManager defaultManager];
    if(![fm fileExistsAtPath:[documentsDirectory stringByAppendingPathComponent:@"LoadedURL.plist"]]){
        [fm copyItemAtPath:[[NSBundle mainBundle] pathForResource:@"LoadedURL" ofType:@"plist"] 
                    toPath:[documentsDirectory stringByAppendingPathComponent:@"LoadedURL.plist"]
                     error:nil];
    }

    // array from doc-dir file
    NSMutableArray *ar=[NSMutableArray arrayWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:@"LoadedURL.plist"]];

    // check weather file has url data or not.
    BOOL fileLocallyAvailable=NO;
    NSString *strLocalFileName=nil;
    NSUInteger indexOfObject=0;
    if([ar count]>0){
        for (NSDictionary *d in ar) {
            if([[d valueForKey:@"URL"] isEqualToString:strToLoad]){
                fileLocallyAvailable=YES;
                strLocalFileName=[d valueForKey:@"FileName"];
                break;
            }
            indexOfObject++;
        }
    }
    if(fileLocallyAvailable){
        NSDictionary *d=[ar objectAtIndex:indexOfObject];
        strLocalFileName=[d valueForKey:@"FileName"];
    } else {
        NSMutableDictionary *d=[NSMutableDictionary dictionary];
        [d setValue:strToLoad forKey:@"URL"];

        NSString *str=[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:strToLoad]];

        [str writeToFile:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%02i.htm",[ar count]]] 
              atomically:YES
                encoding:NSUTF8StringEncoding error:nil];

        strLocalFileName=[NSString stringWithFormat:@"%02i.htm",[ar count]];
        [d setValue:[NSString stringWithFormat:@"%02i.htm",[ar count]] forKey:@"FileName"];
        [ar addObject:d];

        [ar writeToFile:[documentsDirectory stringByAppendingPathComponent:@"LoadedURL.plist"]
             atomically:YES];
    }
    NSURL *u=[[NSURL alloc] initFileURLWithPath:[documentsDirectory stringByAppendingPathComponent:strLocalFileName]];
    NSURLRequest *re=[NSURLRequest requestWithURL:u];
    [wView loadRequest:re];
    [u release];
}

`

sugar
thanks you for the code sample
Yolanda