Collin
Collin

Reputation: 1867

iOS web app as native app

I'm primarily a web developer and I've been given the last minute task of throwing together an iOS application for a conference we're hosting.

We have the web app for the conference built and it is hosted on our web server. Following the tutorial here I've managed to make a very simple application which will display display the content in a UIWebView.

Our other developer has built something for android which will cache the pages and will check a .txt file on the server to see if the content has changed. If the content has changed, new content will be fetched. (conference schedule is very subject to change).

I would like to implement a similar functionality in my iOS application. Is there a simple way to go about doing this?

This is what my ViewController.m looks like:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize appView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *path = [NSURL URLWithString:[NSString stringWithFormat:@"http://ict.uiuc.edu/railroad/GLXS/app/index.html"]];
    NSURLRequest *request = [NSURLRequest requestWithURL:path];
    [appView loadRequest:request];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Upvotes: 3

Views: 318

Answers (3)

mhrrt
mhrrt

Reputation: 977

If i am not wrong the only thing u need to perform is to get web view refreshed when an edit is perform on remote .txt file. in my view You can download the file whenever your app is run for the first time and later on set timer that will check remote file attributes in an interval(say 1min or one sec). Here is sample code that will trigger and return true if file is modified.

//here is sample code that will return true if file is modified
//url:your remote file url. filePathe: local file that you downloade when you app is run for  first time
+ (BOOL)isServerURL:(NSString *)url NewerThanLocalFile:(NSString *)filePath {

if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {


    NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];

    NSDate *date = [attributes fileModificationDate];

    NSString *lastModifiedString = nil;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: url] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:2];
    [request setHTTPMethod:@"HEAD"];
    NSHTTPURLResponse *response;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error: NULL];
    if ([response respondsToSelector:@selector(allHeaderFields)]) {
        lastModifiedString = [[response allHeaderFields] objectForKey:@"Last-Modified"];
    }

    NSDate *lastModifiedServer = nil;
    @try {

        //set time format as required
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
        df.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
        df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
        lastModifiedServer = [df dateFromString:lastModifiedString];
    }
    @catch (NSException * e) {
        NSLog(@"Error parsing last modified date: %@ - %@", lastModifiedString, [e description]);
    }

    if ([date laterDate:lastModifiedServer] == lastModifiedServer)
    {
        return YES;
    } else {
        return NO;
    }

} else {
    return YES;
}
}

If timer returns YES refresh your web-view.Hope i am clear.

Upvotes: 3

Caleb
Caleb

Reputation: 124997

You've essentially created a very simple hybrid application, i.e. one that's part native code and part web content. There are a number of frameworks out there that make this kind of thing a lot easier. The best known is PhoneGap, the open source version of which is Apache Cordova. You can use Cordova as it comes to build your app, or you can look at the source and use the same techniques in your own code.

Since your question seems to relate to caching content, you might look into HTML5's application cache, which should work fine with or without PhoneGap.

Have a look at How to cache images and html files in PhoneGap and Download images and save locally on iPhone Phonegap app for more information.

Upvotes: 2

Venky
Venky

Reputation: 559

You can also check periodically if .txt file on server is changed if so then reload the website using following code

[appView reload];

or

you can put a refresh button on top of the webview in a corner and you can check it on demand

Upvotes: 1

Related Questions