七 5, 2010

Posted by shine in Cocoa / Object-C, 技术研究 | 2 comments

将网页打包成iOS应用程

-(void)setupWebView {

webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];

[self.view addSubview:webView];

NSString *path = [[NSBundle mainBundle] pathForResource:@”index” ofType:@”html”];

NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];

NSString *htmlString = [[NSString alloc] initWithData:

[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];

// to make html content transparent to its parent view -

// 1) set the webview’s backgroundColor property to [UIColor clearColor]

// 2) use the content in the html: <body style=”background-color: transparent”>

// 3) opaque property set to NO

//

webView.opaque = NO;

webView.backgroundColor = [UIColor clearColor];

NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];

[webView loadHTMLString:htmlString baseURL:baseURL];

[htmlString release];

}

这个周末测试了下在iOS中使用html开发应用程序并打包成app~没有什么太大的难点。主要就是基于webview就行了。

有几个关键地方值得注意:

1.如果你的html不止一个,并且需要相互链接,请一定记得设置baseURL。

2.如果要从html中调用本地资源,请在设置baseURL的同时将所有文件的路径设置为同目录。因为打包成app,资源文件不会分为一个个的文件夹。都散落在一个目录中。

基于这种方式的应用程序或许不能获得底层api的支持(没试过,应该也有办法)但是html语言简单明了,门槛较低。开发小应用的时候不妨考虑下这种方式。


                                                                                                                  分享本文: | 更多
  1. 可以转载嘛?
    大帅!

    XD