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.
0 Comments