0

Now I can create a web service for updating pass successfully. Next, I want to create an app by using xcode in order to set at the back of pass. I use many locations of companies for locations in pass. The app I will create is the detail of those locations, so how can I use data in company table to show in table view of the app ? I used to use sqlite for storing data for app, but now I already have database from web service, so what should I do to get those data ? How can I connect MYSQL and xcode ?

malinchhan
  • 767
  • 2
  • 8
  • 28
  • In Xcode, you could create a Core Data entity for your locations which you then use as the datasource for your `UITableView`. You can create a method in your web service to return a JSON dictionary of locations which your App can call with an `NSURLRequest` to populate and refresh the Core Data entity. That way, you will have a persistent store on the device for when the network is not available. You can update the Core Data entity with an asynchronous `NSURLRequest` to your web service when the `UITableView` loads, or you could add a button for the user to trigger an update manually. – PassKit May 14 '13 at 11:19
  • Is it the same way if I use sqlite ? – malinchhan May 15 '13 at 02:01
  • CoreData will automatically use SQLite, but if you already have SQLite code that you are comfortable with then yes, you can just use that. The principle is the same - your web service should contain the master data which you then connect to from your App, replicate in a local data store and then drive your UITableView from that store. – PassKit May 15 '13 at 06:23
  • Can you give some examples of NSURLRequest ? – malinchhan May 15 '13 at 06:53
  • See the accepted answer to this question. http://stackoverflow.com/questions/4456966/how-to-send-json-data-in-the-http-request-using-nsurlrequest – PassKit May 15 '13 at 07:02
  • Thank you ! but I not really understand from it ! – malinchhan May 15 '13 at 07:37
  • It's really not something that you can explain easily. How about [this tutorial](http://nscookbook.com/2012/12/ios-programming-recipe-4-using-nsurlconnection/) that explains how to retrieve JSON from an `NSURLConnection` – PassKit May 15 '13 at 07:44
  • I understand much better than before , thank u !! Now I will make an app that use sqlite first before connecting to server ! – malinchhan May 15 '13 at 07:56
  • How is it work for NSURLRequest ? Do I have use update query or select query from that link ? – malinchhan May 17 '13 at 03:14
  • You create a method for your web services with it's own url e.g. `https://your_service_url.kh/getLocations` which when called creates a JSON dictionary of your locations. You then can use the `NSURL` methods in your app to read the JSON from this url and transfer it to your SQLite table in your app. – PassKit May 17 '13 at 03:58
  • I use this url to get locations from server: #define LOCATION_URL @"http://192.168.1.202/passesWebserver/getAllLocations.php" – malinchhan May 18 '13 at 03:13
  • in getAllLocations.php, what should I return ? I just use query select like this : – malinchhan May 18 '13 at 03:14
  • In xcode, I get json dictionary by : NSMutableDictionary *companyDictList = [WebServiceUtils getDictionaryFromUrlString:urlString]; – malinchhan May 18 '13 at 03:15
  • and I don't understand this error: 2013-05-18 10:17:16.637 DigiPromo[865:c07] -JSONValue failed. Error trace is: ( "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Unrecognised leading character\" UserInfo=0x71e7cc0 {NSLocalizedDescription=Unrecognised leading character}" ) 2013-05-18 10:17:16.638 DigiPromo[865:c07] number of company from servers: 0 – malinchhan May 18 '13 at 03:18
  • I think your URL is missing the leading 'http://' or 'https://' so your app does not know where to look for your file. Also, for your PHP page, make sure that you are only outputting JSON (i.e. no leading whitespace or other characters), and it may help to set a content header of `application/json` – PassKit May 18 '13 at 04:42
  • in getAllLocation.php, I return json like this : while($row=mysql_fetch_array($query1)){ $companyData[] = array("companyID" => $row['companyID'], "companyName" => $row['companyName'], "discount" => $row['discount'], "longitude" => $row['longitude'], "latitude" => $row['latitude'], "link" => $row['link']); } $data = json_encode($companyData,0); echo $data; – malinchhan May 19 '13 at 08:25
  • is it right to send json like this ? – malinchhan May 19 '13 at 08:26
  • Your code looks OK apart from the second argument in your json_encode call. You should use a [PHP JSON constant](http://www.php.net/manual/en/json.constants.php) or just json_encode($companyData); You should also exit after echoing the JSON to avoid writing anything that would invalidate your JSON. – PassKit May 19 '13 at 11:01
  • it works and show this message: 2013-05-20 11:53:15.595 DigiPromo[11353:4003] ADDRESPONSE - ADDING TO MEMORY ONLY: http://192.168.1.202/passesWebserver/getAllLocations.php – malinchhan May 20 '13 at 04:58
  • is it the temporary memory ? – malinchhan May 20 '13 at 04:59
  • Where is this log message appearing? What is generating it? – PassKit May 20 '13 at 07:40

0 Answers0