简体   繁体   English

如何在不使用Web应用程序的情况下完全使用JavaScript编写iPhone应用程序?

[英]How can I write an iPhone app entirely in JavaScript without making it just a web app?

I don't want to take the time to learn Obj-C. 我不想花时间学习Obj-C。 I've spent 7+ years doing web application programming. 我花了7年多的时间做网络应用程序编程。 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? 难道不应该有办法使用WebView,只需在javascript中编写整个应用程序,从项目资源中提取文件?

I found the answer after searching around. 搜索后我找到了答案。 Here's what I have done: 这就是我所做的:

  1. Create a new project in XCode. 在XCode中创建一个新项目。 I think I used the view-based app. 我想我使用了基于视图的应用程序。

  2. Drag a WebView object onto your interface and resize. 将WebView对象拖到界面上并调整大小。

  3. Inside of your WebViewController.m (or similarly named file, depending on the name of your view), in the viewDidLoad method: 在viewDidLoad方法中,在WebViewController.m(或类似命名的文件,取决于视图的名称)内部:

     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; NSString * filePath = [[NSBundle mainBundle] pathForResource:@“index”ofType:@“html”];  \nNSData *htmlData = [NSData dataWithContentsOfFile:filePath]; NSData * htmlData = [NSData dataWithContentsOfFile:filePath];  \nif (htmlData) { if(htmlData){  \n  NSBundle *bundle = [NSBundle mainBundle]; NSBundle * bundle = [NSBundle mainBundle]; \n  NSString *path = [bundle bundlePath]; NSString * path = [bundle bundlePath];\n  NSString *fullPath = [NSBundle pathForResource:@"index" ofType:@"html" inDirectory:path]; NSString * fullPath = [NSBundle pathForResource:@“index”ofType:@“html”inDirectory:path];\n  [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fullPath]]]; [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fullPath]]];\n} } 
  4. Now any files you have added as resources to the project are available for use in your web app. 现在,您作为项目资源添加的任何文件都可以在您的Web应用程序中使用。 I've got an index.html file including javascript and css and image files with no problems. 我有一个index.html文件,包括javascript和css以及没有问题的图像文件。 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. 到目前为止,我发现的唯一限制是我无法创建新文件夹,因此所有文件都会混淆资源文件夹。

  5. Trick: make sure you've added the file as a resource in XCode or the file won't be available. 技巧:确保您已将文件作为资源添加到XCode中,否则该文件将不可用。 I've been adding an empty file in XCode, then dragging my file on top in the finder. 我一直在XCode中添加一个空文件,然后在finder中将我的文件拖到顶部。 That's been working for me. 这一直在为我工作。

Note: I realize that Obj-C must not be that hard to learn. 注意:我意识到Obj-C一定不难学。 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. 但是因为我已经在JS中存在这个应用程序并且我知道它在Safari中有效,所以对我来说这是一个更快的开发周期。 Some day I'm sure I'll have to break down and learn Obj-C. 有一天,我确信我必须打破并学习Obj-C。

A few other resources I found helpful: 我发现其他一些有用的资源:

Calling Obj-C from javascript: calling objective-c from javascript 从javascript调用Obj-C:从javascript 调用objective-c

Calling javascript from Obj-C: iphone app development for web hackers 从Obj-C调用javascript: 针对网络黑客的iphone应用开发

Reading files from application bundle: uiwebview 从应用程序包中读取文件: uiwebview

查看http://www.phonegap.com上的 PhoneGap,他们声称它允许您将JavaScript,HTML和CSS嵌入到原生iPhone应用程序中。

For those doing this on iPhone 2.1 (maybe 2.0), you do NOT need to create any special services for local data storage. 对于那些在iPhone 2.1(可能是2.0)上执行此操作的人,您不需要为本地数据存储创建任何特殊服务。 MobileSafari appears to support the HTML5/WHATWG SQL database API. MobileSafari似乎支持HTML5 / WHATWG SQL数据库API。 This is the same API supported by recent versions of desktop Safari and Firefox. 这与最新版本的桌面Safari和Firefox支持的API相同。

If you're using a toolkit like Dojo or ExtJS that offers a storage abstraction, your code should work on just about any modern browser, including MobileSafari. 如果您使用的是Dojo或ExtJS等提供存储抽象的工具包,那么您的代码应该适用于任何现代浏览器,包括MobileSafari。

To test, open http://robertsanders.name/dev/stackoverflow/html5.html on your iPhone. 要测试,请在iPhone上打开http://robertsanders.name/dev/stackoverflow/html5.html

If you open that page then look on the filesystem of a Jailbroken iPhone, you should see a database somewhere in /private/var/mobile/Library/WebKit/Databases/. 如果您打开该页面然后查看越狱iPhone的文件系统,您应该在/ private / var / mobile / Library / WebKit / Databases /中的某个位置看到一个数据库。 There's even a directory of web-opened DBs there. 甚至还有一个Web打开的DB目录。

root# sqlite3 /private/var/mobile/Library/WebKit/Databases/Databases.db SQLite version 3.5.9 Enter ".help" for instructions root#sqlite3 /private/var/mobile/Library/WebKit/Databases/Databases.db SQLite版本3.5.9输入“.help”作为说明

sqlite> .databases seq name file sqlite> .databases seq名称文件


0 main /private/var/mobile/Library/WebKit/Databases/Databases.db 0 main /private/var/mobile/Library/WebKit/Databases/Databases.db

sqlite> .tables sqlite> .tables

Databases Origins 数据库起源

sqlite> select * from Databases; sqlite> select * from Databases;

1|http_robertsanders.name_0|NoteTest|Database|API example|20000|0000000000000001.db 1 | http_robertsanders.name_0 | NoteTest |数据库| API示例| 20000 | 0000000000000001.db

sqlite> select * from Origins; sqlite> select * from Origins;

http_robertsanders.name_0|5242880 http_robertsanders.name_0 | 5242880

You can create an application without knowing any obj-C. 您可以在不知道任何obj-C的情况下创建应用程序。 The QuickConnectiPhone framework allows you to do this. QuickConnectiPhone框架允许您执行此操作。 Check out http://tetontech.wordpress.com for how to use it as well as other ways of doing what you have asked. 查看http://tetontech.wordpress.com了解如何使用它以及其他方式来做你所要求的。

You should have the native wrapper written in Objective C. This wrapper could contain really few lines of code (like, 10) necessary to create a WebView and navigate it to the given address in the internet (where your application resides). 您应该使用Objective C编写的本机包装器。这个包装器可以包含创建WebView并将其导航到Internet中的给定地址(应用程序所在的位置)所需的几行代码(如10)。 But in this case your application should be a full-featured web application (I mean, use not only the JavaScript, but also some HTML for markup). 但在这种情况下,您的应用程序应该是一个功能齐全的Web应用程序(我的意思是,不仅使用JavaScript,还使用一些HTML进行标记)。

I ran into this same problem. 我遇到了同样的问题。 I already have a game written entirely in Javascript. 我已经有一个完全用Javascript编写的游戏。 I would love to make an iPhone friendly version, but Obj-C is an overkill. 我很想制作iPhone友好版本,但是Obj-C是一种矫枉过正的版本。 What I ended up doing was using the WebView to point to a special url of the iphone app. 我最终做的是使用WebView指向iphone应用程序的特殊URL。 After thinking about it, I suppose I could just move those files to the app directory and run them locally. 在考虑之后,我想我可以将这些文件移动到app目录并在本地运行它们。

There not way to do this with the current apple API's. 使用当前的Apple API无法做到这一点。 Your closest bet is to write a simple native iPhone app that embeds the webkit browser. 您最接近的赌注是编写一个嵌入webkit浏览器的简单原生iPhone应用程序。 That will let you browse your xhtml/js application locally. 这将让您在本地浏览您的xhtml / js应用程序。

If you want to store data, you'll need to take it a step further and include a light weight http server that servers up your app and provides calls to store and retrieve data. 如果你想存储数据,你需要更进一步,包括一个轻量级的http服务器,它可以为你的应用程序提供服务,并提供存储和检索数据的调用。 Probably not an ideal solution for you, but possibly less work than a full Obj-C app. 可能不是一个理想的解决方案,但可能比完整的Obj-C应用程序更少的工作。

As a side note, Obj-C is fairly easy to learn. 作为旁注,Obj-C相当容易学习。 There are tons of examples in the SDK. SDK中有大量示例。 The community is strong and will answer well put questions without hesitation. 社区很强大,会毫不犹豫地回答好问题。

I have been using phonegap for a while and it seems to have the best results for me. 我一直在使用phonegap一段时间,它似乎对我有最好的结果。 I will post my experience in a week or so with a link to my app as well. 我会在一周左右的时间内发布我的经验,并附上我的应用程序的链接。

Titanium Mobile也是一个选项 - 它允许您编写可以转换为Objective-C的JavaScript。

At least 2 others mentioned phonegap, but I thought I'd post this too and mention that Apple has approved the phonegap framework. 至少有2个人提到了phonegap,但我想我也会发布这个帖子,并提到Apple已经批准了phonegap框架。 So, now you won't get your app rejected by Apple just because you're using phonegap. 所以,现在你不会因为你正在使用phonegap而被Apple拒绝了你的应用程序。

Blog post about phonegap and Apple - http://blogs.nitobi.com/jesse/2009/11/20/phonegapp-store-approval/ 关于phonegap和Apple的博客文章 - http://blogs.nitobi.com/jesse/2009/11/20/phonegapp-store-approval/

Phone Gap Home 电话差距首页

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

相关问题 如何解决移动中的跳动问题 <LI> 流jQTouch移动Web应用(iPhone JavaScript) - how can I fix the jumpiness problem on a moving <LI> stream jQTouch mobile web app (iPhone JavaScript) 如何编写一个 javascript web 应用程序在移动设备 safari 上查看时不会旋转? - How can I write a javascript web app that doesn't rotate when viewed on mobile safari? 如何在iPhone Web应用程序上通过键盘捕获事件? - How can I catch an event from the keyboard on an iPhone web app? 如何在iPhone应用程序中缓存Javascript和JSON数据? - How can I cache Javascript and JSON data in my iPhone app? JavaScript可以完全用于制作SQL查询应用程序吗? - Can JavaScript be used entirely to make a SQL querying app? 如何制作无需JavaScript即可运行的网络应用 - How to make a web app that works without javascript 如何使用JavaScript将QuickTime影片嵌入到iPhone Web应用程序中? - How do I embed a quicktime movie into an iphone web app using javascript? iPhone应用程序:无需Web API即可获取数据 - iPhone app: getting data without a web API 我怎么能用javascript(没有jquery)写这个 - how can i write this with javascript (without jquery) 使用Javascript在iPhone网络应用中播放声音? - Play sound in iPhone web app with Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM