IPhone Native App using PhoneGap and PHP
I want to develop an iPhone nati开发者_运维知识库ve app, that sync with a remote DB.Is it possible to develop this application using PhoneGap.If I use PhoneGap, how do I get data from an external DB? Can I use PHP on PhoneGap?
PhoneGap allows you to run JavaScript and HTML applications on the iPhone. all your PHP code will have to reside in a remote server.
To get data from the server you have to use the XMLHttpRequest
object or something more user friendly like jquery's $.get()
or $.post()
functions.
I just compiled a PhoneGap app with PHP using Ajax to get content.
First, load jQuery library at index.html head. At function onBodyLoad(), put the Ajax call for the PHP file:
$('#content').load('http://www.example.com/test.php');
at the HTML session, put the div id="content" where do you want to show content.
PHP:
for($i=1; $i<=10; $i++) {
echo '<p>I\'m a PHP Loop! Value: ' . $i . ' of 10.</p>';
}
HTML will print:
<p>I'm a PHP Loop! Value: 1 of 10.</p>
<p>I'm a PHP Loop! Value: 2 of 10.</p>
<p>I'm a PHP Loop! Value: 3 of 10.</p>
<p>I'm a PHP Loop! Value: 4 of 10.</p>
<p>I'm a PHP Loop! Value: 5 of 10.</p>
<p>I'm a PHP Loop! Value: 6 of 10.</p>
<p>I'm a PHP Loop! Value: 7 of 10.</p>
<p>I'm a PHP Loop! Value: 8 of 10.</p>
<p>I'm a PHP Loop! Value: 9 of 10.</p>
<p>I'm a PHP Loop! Value: 10 of 10.</p>
You could also use
$.get('test.php?name', function(data) {
$('#content').html(data);
});
And your test.php could have something like:
if (isset($_GET['name'])) {
echo "Asked for name!";
}
With this, you can go on and make some nice stuff. I have one doubt on the subject: can I host external PHP files and deploy the app on app store? There's any restrictions about that?
wm_eddie is correct.
Also, I wanted to comment on your "I want to build a native iPhone app" quote. Applications written for PhoneGap are not native apps. While they can be made to look, and for the most part, act like native apps, a better analogy is to think of them as pre-packaged, locally installed, web apps that can, using the PhoneGap api's, get access to a limited subset of native functionality. Notably, this functionality consists of sensors (accelerometer, gps, etc) and camera operation among others.
This may have no bearing on your usage, but just wanted to clarify the point... ;)
(PhoneGap is still a very nice tool, and I'm using it in some of my projects. However, if you need a fully native app, except for the JS which must be interpreted at run time, but is pre-compiled using mostly 1-1 language dependent symbolism, then I'd recommend Titanium which I'm leveraging as well. Both nice tools, but they deliver distinctly different products with different benefits and disadvantages. It's a matter of determining the requirements of your end-product and then choosing the platform that makes the most sense.)
精彩评论