dongyi1215 2014-10-09 19:08
浏览 91
已采纳

将php json文件导入swift

I have a website that uses MySQL database. I'm trying to build an iOS app for it, so I wanted to use Swift to import information from the database and insert new information to it. I read that for the reading part I should use a PHP file to create a Json file, read that and extract the data. But I can't find the way to do it with swift.

Is there a way to connect iOS apps (written in Swift) to MySQL?

  • 写回答

1条回答 默认 最新

  • dsbqfrr098575666 2014-10-09 19:17
    关注

    In the client-server architecture, the client is completely separate from the server, and they just exchange data thanks to a common "language" (in your case, certain fields encoded in JSON).

    Your client is your iPhone app, written in Swift (but that's irrelevant).
    You now need to build a server, which is entirely separate from the app. You don't need Swift for that. You will need a server (for example a cheap cloud VPS on Amazon EC2, Rackspace Cloud Servers, Microsoft Azure...) and you will have to create another application that runs on that server.

    If it's just to pull data from a MySQL database, you can easily make that in PHP. Or you could use Node.js (which uses JavaScript: it's among the "trendiest" technologies at the moment), Ruby, Python, etc.
    For an example that uses PHP, you can check: https://stackoverflow.com/a/22367600/192024
    To read the JSON data in your iOS app, then, you can use the builtin libraries: https://developer.apple.com/library/ios/DOCUMENTATION/Foundation/Reference/NSJSONSerialization_Class/index.html (it's available in Swift too)

    I don't know what your app is doing, but if you want to ignore all the things with the backend (the server) you can always consider something like Parse Core and let somebody else take care of the backend.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?