Change JSON values from iphone app (possible API needed?) - ios

I have an app with tutorials. I also have a tab where users can suggest tutorials. Right now, the app loads a table view with an JSON array taken from my website. Every tutorial object in the array looks like this:
{"string" : "Tutorial Name", "value":1, "devices":["deviceid1","deviceid2"]}
The string is the name of the tutorial they want to see. The value is how many people have voted for it. The devices is an array of strings where I would save the device ID of each user who votes for that tutorial. This is to make sure they do not vote more than once.
Now, what I want the app to do is to change the value key to add one and to add its deviceID to the array.
How do I do that in xcode? I am assuming I might have problems with 2 users trying to change the values at the same time. Should I maybe create a small API for this? Possible create a file that would get the values for me and change them as well?
If so, how do I go about doing this?
Thanks!

If you are making changes to the JSON from your app, you are only changing the values locally. This means that the changes won't be updated on the devices of other users who are accessing the app. I think the best way to do this would be to create a small API.
1) You will need to setup a MySQL database that has keys for "string" and "value".
2) Create a PHP script that takes in a string and then adds one to the value field of the database row. This script will be called when a user "votes" for a tutorial.
3) Create a PHP script that queries the data from the database and converts it to JSON format. When loading the app, this script would be called and your app would parse the JSON data and display it into a tableview with the updated values.
Hope this sets you off in the right direction!

Related

Saving data in non-document based application

I have an app similar structured to the new Notes app. A sidebar contains information about different files (date, title, excerpt) and the detail view shows the whole content.
To store data in my app, I use NSKeyedArchiver. Each file is stored seperatly to make iCloud Sync easier.
So here is my question: Is there any good way of storing and loading this data in the sidebar?
The problem is that when the app starts, it iterates through every available file to load the information needed. However, the whole object graph loads for each file even if I only need the date and the title. Is there anything I can do to optimize this?
Should I use a seperatly index file where all dates and titles are stored?
Am I using the wrong storage concept?
Any advice would be appreciated!
Sounds like what you need to do is store the key information (date and title) separately from the rest of the data.
When you start the app, you can load the key information and display it, then retrieve the rest of the data only when required, by linking the title to the content.
You could use CoreData for this - but probably overkill.
Realm will work very well for this. There's a good tutorial available here http://www.raywenderlich.com/112544/realm-tutorial-getting-started

What is the best way to externalize the static items used in iOS app?

I am working on app where I have country object and I will be having around 200 instances of object. Each object will have few properties like flagName, capitalCity etc.. I started working on prototype using only 10 countries and I stored these objects inside the viewDidLoad method as an arrays. I know its not a good practice to "hard code" everything in the source code. In a real app, I would like to externalize these static items and put them in a file or database or somewhere else. What is the best way to store such information ? I am thinking of property list but if the data-model changes then I will have to re-submit the app. Any help would be greatly appreciated. Thanks
Try to load a JSON file from your web site. Create a simple JSON model, hide the file on your web site and have the app load it from there. If you want to change it, you just have to change the JSON file on your server. This way you won't have to re-submit the app.
Cache the content of the file in your app so it can work off line, if needed.
Just a suggestion.

Creating you own database with Parse for an iOS app in Swift

I need to know if I can use Parse for what i need. Let me give you some context. I am new to coding for iOS in Swift. I am creating an app, and a big part of it is displaying info to the user depending on what country they select.
So at the start of the app, they will select a country. I have used Parse a little bit through an online course, so I know how to signup and create users etc.
What i want to know if i can do?
Can i use Parse to create my own database? So have a Class called country_data, and then in that class, have all the countries with info about them. As an example of what i mean, see the table below:
Then, in the app, i can check that if the user selects 'Spain' for example, I can display all the Info for Spain.
Everything i've done in Parse so far had been about sending info to Parse that Ive created from the code in Xcode. But i need to know if i can go into my app in Parse and create this database myself.
Is this possible and is it the right way to do what i'm looking for. Any suggestions welcome, and thanks! Please ask questions if you need me to explain more.
Nick
Good news OP,
(1) yes Parse is absolutely perfect for this.
Parse is now at http://back4app.com
Here...
It literally took me less time to do that, than it took to answer your question!
It's almost unbelievable to understand how much time "BAAS" services save, in the new "BAAS" era.
(2) 196 countries. You should be OK :) Anything up to a few million countries will work fine. After that Parse will get a little slow on searches.
(3) "But I need to know if i can go into my app in Parse and create this database myself."
Yes, using the internet and a computer,
just go to back4app.com, log in, and click on your project. Click on "Core". (Parse software also offers features like Analytics, etc. For the database system, just click on "Core".)
Notice the button "Add Class", click that and type in "Country". Add a column called "name" which is a "String".
Make another new class "City". Again make a "String" column "name", and also make a column that is a "Pointer" to "Country".
You're completely done.
It is far quicker than me typing it here, heh

using a array list in ios to create a translation app

I am taking development courses for ios and I was wondering if I wanted to create a translation app would i use a array list to do so? As example code is:-
var dictionary = [“talofa”: “hello’, “faafetai”: “thank you”]
print(dictionary[“talofa”])
it shows up in the logs as “hello” but there has to be an easier way to do translations otherwise I would be fitting a whole language in a array list?
I also read online that people have been using third party services like google to make a translation app but my language is not on google (Hawaiian) what do I do?
First of all, what you are using in your example, in swift/objc it is called a dictionary.
Secondly, for such a huge amount of data, I recommend you use some sort of persistent storage. You can use plain text to store the dictionary (like creating a .plist file), but being iOS I would recommend setting up coredata.
CoreData will allow you to store the information on the device, and access it through a data Model.
Here you can find an example on storing in a file.
Here you can find an example on storing in CoreData.
I personally recommend using coredata for such a large quantity of data. Plist files are more suitable for storing low information quantities (like saving some credentials, some settings, etc).
You need to use DB for this. You can update it from your server when user will have connection, so you don't need to re-submit your app when you will update your vocabulary.
You can use CoreData as #Alex Bartiş told you or you can try another one which becomes popular: Realm

Ideas to store an TableView for different Users on Parse

I have an application where the user needs to sign in first. Now in my application there is an table view which can be filled with different events for example: a user adds an Title for a new event and an yellow icon. Now the Title and the yellow icon appears in the tableview.
Now i want the app to sync with Parse so that this user can see it's Title and it's yellow icon on every device he's logged in.
I'm sorry that i need to ask, i hope you can help me :/
Actually there is already a lot of content regarding your question and the Parse SDK is very well documented.
For example have a look at this: Save App Data to Parse Backend
You should do the following steps:
Create an App on Parse
Create User Table
Create AppData Table
Reference the User with the AppData entry via column type : "Pointer"
Whenever a entry was added to your tableview or whenever the app is closed save the current data to your parse backend
Whenever the tableview is shown or whenever your application starts retrieve the information from your Parse backend and show them in your tableview accordingly
I hope the link I gave you can give you some insight, however nobody knows whether you are coding in Swift or Objective-C because you added both tags.
If you need more information on how to design your backend in order to retrieve the right entries for the specific user I recommend reading this:
find-all-objects-belonging-to-a-user-with-objectid
or reading about relational queries in the Parse SDK.

Resources