Sponsor

iPhone Native Application Connect to (CRUD) Remote Database Server | How to connect to a Remote Database from native Iphone | How to connect an iOS app to an existing database

1. Is it possible to create a native iphone application that connects to a remote database server? — Yes. Its possible. As far as I know, there are no limits as to what you can connect to and communicate with using the iPhone OS native SDK.

2. If #1 can be done, is it possible with objective-c and cocoa? — Yes.

3. If #2 is possible, what classes are needed? — Yes. The class you want to use is NSURL. Look for the “URL Loading System” documentation that you’ll find in your Xcode documentation set. In the NSURL Class Reference documentation, it will also list sample code that uses the class that you can study. There should be lots of examples using MacOS applications as well so search for Cocoa samples too (not just Cocoa Touch for the iPhone).

5. On the remote server, what database can be used? — Any database can be used.

Apps which utilize the data of popular sites such as Facebook will generally use an API provided by the site's developers. By making calls against this API, you can ask for specific pieces of data from the website without having to actually connect to any database at all. Any database connections are handled by the web service on behalf of the API caller, which then returns the requested data in a meaningful format (such as JSON or XML). Larger sites usually make APIs when they want to allow other developers to use their data in a secure, future-proof, and consistent way.

For example, imagine an iOS app utilizing an API designed by the developers of randomwebsite.com which enables access to the profiles of  users. With this simple hypothetical API in mind, let me show you how you may interact with this API in your application.

1.     A user presses a button to access MHill's profile in the app.

2.     The app makes a HTTP request to. You'd probably use NSURLConnection to do this in Cocoa.

3.     When randomwebsite receives this request, the API handler will detect that you're after the profile of someone who's named "MHill". Randomwebsite will retrieve this data from their database, and return it as the response to the request.

4.     The app will receive this data, parse it, and display it.


Now, in the case of a small app connecting to an arbitrary MySQL database, you'll probably want to do so using one of the popular Objective-C MySQL libraries. From a brief search, I've found an Objective-C wrapper around MySQL's C API which may be of interest to you if aren't comfortable with using C and would like a higher level of abstraction. If not, then you can just download the C library. which is possibly the more flexible option.

Using a MySQL library in any language isn't much different to connecting to a MySQL database in something like PHP.

You'll need to

1.     Connect to the database, specifying the hostname, username, and password (if necessary).

2.     Query the database.

3.     Store the results of the database.

4.     Close the connection to the database.


Post a Comment

0 Comments