PhoneGap Static lib for iPhone-XCode | October 27th, 2009
I recently spent some time evaluating the iPhone branch of PhoneGap and have made some changes that I think will make things easier to develop.
One of the key issues that I wanted to address was that PhoneGap existed as a repository and anyone that made an application with it would invariably have to mix their code in. In a very few cases it is possible to make a complete application using PhoneGap out of the box, but what if you need to add, remove or change something? As soon as you start messing with the core PhoneGap code, you are looking at huge merge issues when updates are pushed to the PhoneGap repo. Most likely you will just ignore any new commits, and your code will be stuck in time from the moment you changed it, or you will have to pick through the updates, and decide what is worth re-implementing in your own codebase.
My changes work as follows: You do not create a PhoneGap app anymore! You create an app that inherits from PhoneGap. This means your PhoneGap code can be in a git repo somewhere (github) while your application code can be in it’s own repo. If there are updates to the PhoneGap codebase then ( as long as the API has not changed ) you simply update PhoneGap and build your app.
In order to make these changes, I had to simplify some of the PhoneGap code. I removed the dependency on XIB files so the UIWebView is now created through code instead of automagically by XCode boilerplate code behind the scenes. The PhoneGap core code is now a static library that you link to and inherit from so there are some things you will have to do to create a new phonegap application. Also keep in mind that this is all subject to change as we are currently looking at ways of making this dead-simple.
You can git the code here: http://github.com/phonegap/phonegap/tree/plugins/iphone/
The new PhoneGap 12 step program
- In XCode create a new WindowBased iPhone Application.
- Select the project and add a reference to the PhoneGapLib project. ( The one you downloaded from git )
- Add a reference to all headers in the PhoneGapLib project ( these are needed so the compiler can know what you will be linking against, and what the base PhoneGapDelegate protocol looks like )
- Delete the MainWindow.XIB interface builder file that XCode created.
- In main.h add your app delegate class name to the UIApplicationMain call.
ex.
int retVal = UIApplicationMain(argc, argv, nil,@"MyAppDelegate"); - Change the interface of your application delegate to inherit from PhoneGapDelegate.
ex.
@interface MyAppDelegate : PhoneGapDelegate { } - Select the build target in your project and add a dependency for PhoneGapLib.
- Add libPhoneGapLib.a to the ‘Link Binary with Library’ build step.
- Add the www folder to the ‘Copy Bundle Resources’ build step.
- Place phonegap.js in your www folder along with all your html/css/js application code.
( note: this is a little bit hack-y, and we are looking at ways of improving this step especially ) - Add project references to the following frameworks
- AddressBook.framework
- AddressBookUI.framework
- AudioToolbox.framework
- AVFoundation.framework
- CFNetwork.framework
- CoreGraphics.framework
- CoreLocation.framework
- Foundation.framework
- MediaPlayer.framework
- QuartzCode.framework
- SystemConfiguration.framework
- UIKit.framework
- Build and Go.
So What’s Next?
Recent PhoneGap discussions have led us to the conclusion that we need a plug-in architecture. It will be much easier to get people to contribute if they can focus on a key piece of functionality and not have to implement things throughout the codebase + it also gives a clearer separation of contributions and would allow for unit testing. This latest addition/change is a step towards a more plug-able architecture. Thanks also to Shazron for his help, support and suggestions along the way.
So get reviewin’ !!
October 27th, 2009 at 6:40 am
Cool work!
What’s next for PhoneGap includes Contacts conformance to the draft of the W3C Contacts API http://dev.w3.org/2009/dap/contacts/ , Plug-in architecture of course, Xcode template to create new PhoneGap projects, installer to install the PhoneGapLib above (with possible auto-updater). Note that I am only talking about the iPhone version currently…
October 27th, 2009 at 7:05 am
I like this idea a lot, and will be playing with it soon. Seems like a lot of the process could be made easier in the same way PhoneGap currently is, by downloading a premade XCode project.
October 28th, 2009 at 4:49 am
Sounds awesome! This is a great move for the project and the PhoneGap community.
Would it be correct to say that that non Obj-C developers using PhoneGap won’t have to really do anything differently?
IOW - If I want to write my entire app with HTML, CSS, and JavaScript, can I still just drop my files in the www directory and everything will continue to work as before?
Thanks,
j
October 28th, 2009 at 5:08 am
Yes, there is a sample iphone project that you can simply add your www contents + icon.png + default.png.
Other than that, you should just need to set your info.plist to reflect your Bundle-Identifier and Bundle-Name. Then you should be off to the races.
October 29th, 2009 at 11:48 am
[...] not affect your code. The main parts of the code should already be checked-in (see Jesse’s post for the url). This is a work in progress. See screencast [...]
November 9th, 2009 at 12:06 am
This is quite a step forward.
It would be helpful to know when this scheduled to go into a stable “official” version. Will it be days, weeks or months?
Did you also think about the other platforms, especially Android? I guess building a jar would do the same trick here.
Thanks!
November 9th, 2009 at 3:01 am
Anonymous, if that’s your real name:
Probably in the order of ‘weeks’ before anything is official, and yes the other platforms are being investigated to implement a similar mechanism.
Cheers!
December 6th, 2009 at 6:56 am
I am not an objective-c programmer, but I really liked Phonegap’s idea of “write once, run anywhere”. You have a great project, which will open the door of iPhone for people from different background, but it is really hard for people like me to get started without a step-by-step tutorial. Especially for people with no Mac OS experience.
July 22nd, 2010 at 5:10 am
Hi,
it’s perfect for my app, but i have this error :
error: cannot find interface declaration for ‘PhoneGapDelegate’, superclass of MyAppDelegate’
Do you any ideas??
thanks
July 22nd, 2010 at 7:59 am
It’s works form me, I’ve forgotten to add the the import !!
Now i have a new error, I have a black screen when the app start
do you have any suggestions??
Thanks
Medi
July 22nd, 2010 at 8:51 am
Is your HTML page black?
Have a look at the getting started guides and the mailing list at phonegap.com
July 22nd, 2010 at 11:52 pm
I don’t know if it’s the HTML…
The tuto said i should delete the mainwindow.xib file, but i have this error: Failed to load NSMainNibFile MainWindow.
I have already looked at the mailing list, there is no subject about this.
Thanks.
July 22nd, 2010 at 11:55 pm
i misunderstood your question : The HTML is a web page!
July 26th, 2010 at 6:24 am
I have to explain more specifically what i’m doing:
I’m trying to do an app that have a scan button. This button launch the camera view. A specific lib was given to me for this purpose.
The problem is when i create a phonegap project, i found it very hard to link the libs.
So the solution was to begin whit the app demo given with the lib and add the phonegap lib. It doesn’t work as i already told you ( i had a black screen…).
i have no ideas for the moment …so if you have suggestions
Thanks for your help
December 15th, 2010 at 4:15 am
I want to extend phonegap functionality through plugin.I create .h&.m file inplugin.I added js file in www folder but when i m call that js file through html it didnot call.only default phonegap.js file is called.pls tell me
March 7th, 2011 at 2:28 pm
Did you include your js file in the html page?
Other than this I would need to see code.
There are plenty of helpful folks on the PhoneGap mailing list at http://groups.google.com/group/phonegap
July 6th, 2011 at 4:50 pm
[...] not affect your code. The main parts of the code should already be checked-in (see Jesse’s post for the url). This is a work in progress. [Updated: Download a preview [...]