简体   繁体   English

iOS网络应用作为原生应用

[英]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. 我主要是一名网络开发人员,我已经完成了最后一项任务,即为我们正在举办的会议推出一个iOS应用程序。

We have the web app for the conference built and it is hosted on our web server. 我们为会议构建了Web应用程序,它托管在我们的Web服务器上。 Following the tutorial here I've managed to make a very simple application which will display display the content in a UIWebView. 按照这里的教程我设法制作了一个非常简单的应用程序,它将在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. 我们的其他开发人员为android构建了一些东西,它将缓存页面并检查服务器上的.txt文件以查看内容是否已更改。 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. 我想在我的iOS应用程序中实现类似的功能。 Is there a simple way to go about doing this? 有一个简单的方法可以做到这一点吗?

This is what my ViewController.m looks like: 这就是我的ViewController.m样子:

#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

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. 如果我没有错,您需要执行的唯一操作是在远程.txt文件上执行编辑时刷新Web视图。 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). 在我的视图中您可以在第一次运行应用程序时下载文件,然后在set timer上下载该文件,该计时器将在一个间隔(例如1分钟或1秒)内检查远程文件属性。 Here is sample code that will trigger and return true if file is modified. 下面是示例代码,如果修改了文件,它将触发并返回true。

//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. 如果计时器返回YES刷新你的web-view.Hope我很清楚。

You've essentially created a very simple hybrid application, ie one that's part native code and part web content. 您基本上创建了一个非常简单的混合应用程序,即一部分是本机代码和部分Web内容。 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 . 最着名的是PhoneGap,其开源版本是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. 你可以使用Cordova来构建你的应用程序,或者你可以查看源代码并在你自己的代码中使用相同的技术。

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. 由于您的问题似乎与缓存内容有关,因此您可以查看HTML5的应用程序缓存,无论是否使用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. 看看如何在PhoneGap中缓存图像和html文件下载图像并在iPhone Phonegap应用程序上本地保存以获取更多信息。

You can also check periodically if .txt file on server is changed if so then reload the website using following code 您还可以定期检查服务器上的.txt文件是否已更改,如果是,则使用以下代码重新加载网站

[appView reload];

or 要么

you can put a refresh button on top of the webview in a corner and you can check it on demand 您可以在角落的webview上放置一个刷新按钮,您可以按需检查它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM