我如何完全用JavaScript编写iPhone应用程序而又不只是将其变成Web应用程序? [英] How can I write an iPhone app entirely in JavaScript without making it just a web app?

查看:97
本文介绍了我如何完全用JavaScript编写iPhone应用程序而又不只是将其变成Web应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想花时间学习Obj-C.我花了7年以上的时间进行Web应用程序编程.应该没有办法使用WebView并只使用javascript编写整个应用程序,直接从项目资源中提取文件吗?

I don't want to take the time to learn Obj-C. I've spent 7+ years doing web application programming. Shouldn't there be a way to use the WebView and just write the whole app in javascript, pulling the files right from the resources of the project?

推荐答案

我在四处搜寻后找到了答案.这是我所做的:

I found the answer after searching around. Here's what I have done:

  1. 在XCode中创建一个新项目.我想我使用了基于视图的应用程序.

  1. Create a new project in XCode. I think I used the view-based app.

将WebView对象拖放到您的界面上并调整其大小.

Drag a WebView object onto your interface and resize.

在WebViewController.m(或类似名称的文件,取决于您的视图的名称)内部,在viewDidLoad方法中:

Inside of your WebViewController.m (or similarly named file, depending on the name of your view), in the viewDidLoad method:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];  
NSData *htmlData = [NSData dataWithContentsOfFile:filePath];  
if (htmlData) {  
  NSBundle *bundle = [NSBundle mainBundle]; 
  NSString *path = [bundle bundlePath];
  NSString *fullPath = [NSBundle pathForResource:@"index" ofType:@"html" inDirectory:path];
  [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fullPath]]];
}

  • 现在,您添加为项目资源的所有文件都可以在您的Web应用程序中使用.我有一个index.html文件,其中包括javascript,css和图像文件,都没有问题.到目前为止,我发现的唯一限制是我无法创建新文件夹,因此所有文件都使资源文件夹变得混乱.

  • Now any files you have added as resources to the project are available for use in your web app. I've got an index.html file including javascript and css and image files with no problems. The only limitation I've found so far is that I can't create new folders so all the files clutter up the resources folder.

    技巧:确保已在XCode中将文件添加为资源,否则该文件将不可用.我一直在XCode中添加一个空文件,然后将我的文件拖到查找器的顶部.那一直在为我工作.

    Trick: make sure you've added the file as a resource in XCode or the file won't be available. I've been adding an empty file in XCode, then dragging my file on top in the finder. That's been working for me.

    注意:我意识到Obj-C一定不那么难学.但是,由于我已经在JS中安装了该应用程序,并且知道它可以在Safari中运行,因此对我来说这是一个更快的开发周期.有一天,我确定我必须分解并学习Obj-C.

    Note: I realize that Obj-C must not be that hard to learn. But since I already have this app existing in JS and I know it works in Safari this is a much faster dev cycle for me. Some day I'm sure I'll have to break down and learn Obj-C.

    我发现其他一些有用的资源:

    A few other resources I found helpful:

    从javascript调用Obj-C:从javascript调用Objective-c

    Calling Obj-C from javascript: calling objective-c from javascript

    从Obj-C调用javascript: iphone针对网络黑客的应用开发

    Calling javascript from Obj-C: iphone app development for web hackers

    从应用程序包中读取文件: uiwebview

    Reading files from application bundle: uiwebview

    这篇关于我如何完全用JavaScript编写iPhone应用程序而又不只是将其变成Web应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆