Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

IOS app - any service (I guess it's an API) - mysql (db server)

Hello, I want to build an app that connects to an existing mysql db running on my server. So, as i have researched, i have to use an api to make the connection between the app and the db, right? If so, could anyone tell me the best api to use? And, if possible, give me any link or so that i could read and learn how to make this connection using this api? if not, could anyone tell me how i could make this sql requests to my db from an ios app? Thank you!!!

1 Answer

Hey Fernando, My understanding is that you should write an interface (web service) for your database in a server side language (like PHP). This will receive some commands and then return data in a format like JSON. As apps can be decompiled, having database usernames and passwords in you iOS app is dangerous, so this also gives a level of separation.

I'm actually using a combination of PHP and MySQL for my web services. My app posts to a url which then interrogates the database and sends back a response in JSON which iOS then acts on.

I may be wrong, but my understanding is that this is the best way to do it. Some examples of how to go about this, or just some more reading on this in the links below:

http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service

Happy to answer any questions you may have.

Thanks for the awenser! Actually I've found this way to connect to a db using server-side PHP, but I don't actually know a lot about it and I thought having my db username and password at a PHP file in my server wasn't safe. Could anyone access this PHP file and would it have the db username and password in it? Or is there a way to make it safe? Thank you!!

Because PHP is server side code, whoever accesses it only sees what the PHP file returns to them. This means that 'view source' or the like will not show any PHP code which keeps your details safe.

What I have done in my iOS code to make it a little more secure is add a header to my post:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:@"http://yourWebService.php"];
[request setValue:@"insert a unique API key here" forHTTPHeaderField:@"myAwesomeApp"];

Then in PHP, you can check if this header exists. This will stop anyone random who has grabbed your PHP url and means it's either coming from your app or someone has gone to the effort of decompiling your app to get the key. It's not massively secure but does make it a little more of a pain for them.

$authorised = false;

foreach (getallheaders() as $name => $value) {
    if ($name == "myAwesomeApp") {
        if ($value == "87hq349fn98ehpvmq34gpn9w8k90dfg3")
            $authorised = true;
    }
}

// I also had to add this, to be able to get the headers...
if(!function_exists('getallnewheaders')) {
    function getallheaders() {
        foreach($_SERVER as $name => $value) {
            if(substr($name, 0, 5) == 'HTTP_') {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        return $headers;
    }
}

Sorry, realised this turned into a massive comment.. :)

Thank you!! Now I'll start building this ios-php-db system :D. If I have some kind of issue with it, I'll let you know :P. Thank you for your attention!!