jwknz
jwknz

Reputation: 6822

Getting data from MYSql into my iOS app with JSON

Ok I found a few questions on how to get data from a MYSql database into an iOS app, but I am just asking a few best practices here. I know these can all be separate questions, but I am hoping that they can be answered in a way that they relate to each other.

Am I correct to understand that to be able to get data into an iOS app - I need to first generate a JSON file, have that stored on a server and than have the app download this file??

If the previous answer is NO then does that mean, I can pull in data on the fly?

Lastly I have seen PHP examples to create JSON files, but iOS is in Objective-c. Does this mean I need to load a UIWebView to be able to load the PHP page that generates the file?

What I have:

I have a MYSql database - it is set up through PHPMyAdmin, so I am not familiar enough with the creation process of the database yet. I will look into that.

I can also export the JSON file from PHPMyAdmin, but that is no good to me in a iOS app.

I also have the parsing from a JSON file into an iOS app sorted, but I want to be able to do this on the fly instead of creating potentially hunderds of files.

I hope someone can help me here:-)

I am not necessarily asking for code, but would be mad to ignore it:-)

Upvotes: 2

Views: 4079

Answers (3)

James Sumners
James Sumners

Reputation: 14777

The problem is that there are not any iOS libraries for directly connecting to a MySQL server; and you really wouldn't want to do that, anyway. So, you need an intermediary server capable of sending data in a format your iOS application can understand. Note, this does not mean the data has to be JSON formatted. But it is very easy to use JSON as the format for your data. Most languages have native support for generating JSON from its native object format(s).

Once you have a server capable of sending data in your preferred format, you need to write some way for your iOS application to retrieve it. You do not have to use a UIWebView for this. As mentioned, the NSURLConnection framework is very easy to use to make such a request. However, there are a lot of other factors to consider when making network requests and others have already done most of the work for you. I like using the AFNetworking framework in conjunction with JSONKit. AFNetworking makes asynchronous calls to remote web services very easy, and JSONKit is nicer than NSJSONSerialization in my opinion.

Upvotes: 6

Pedro Vieira
Pedro Vieira

Reputation: 3370

What I do to retrieve data from MySQL to my iOS app is:

  1. Create a PHP file on your server and prepare it for GET methods (you're going to send data from the iOS app)
  2. Send a request from your iOS app to your php file, like: "www.yourdomain.com/data.php?name=..."
  3. Process the information on your php file and echo the json output.
  4. When connectionDidFinishLoading: convert the NSData to an Array using NSJSONSerialization.
  5. Do whatever you like with the output information

That's just do way I do. I'm not familiar with other approaches.

Upvotes: 2

Kitsune
Kitsune

Reputation: 9341

PHP (and any other server side language) can take the data from the MySQL database and output it to any client as JSON, on the fly. No need to save the JSON to disk beforehand. Of course, from the client's point of view, there really is no fundamental difference (except the data will always be the latest representation of what's in the database).

You also don't have to use a UIWebView. There's a number of ways to make an HTTP request using Objective-C, but you'll likely want to look at something along the lines of NSURLConnection's sendSynchronousRequest:returningResponse:error: method (I prefer using synch methods inside an async block, but that's not the only way). You can find many tutorials on how to do similar things, as well as higher level libraries to simplify the process.

Upvotes: 1

Related Questions