Storing my ObJ-C class online. (amazon web services?) - iphone

Okay this is a fairly broad question. This is my first App and I'm not sure the best way to go about this. The app is on the IPHONE.
I have a 'Restaurant' class. The restaurant has many different attributes and opening times.
I currently store a restaurant in an instance of nsdata (it complies to NSCoding) LOcal storage is easy and I just use nsdata.
I have just built in an import/export function. I want these two methods to "post" a restaurant to the web and "get" a restaurant from the web. I know I can do this with NSUrlConnection and I have it up and working.
However I want to be cleverer about it. For instance what if I want to take my online list of restaurants that have been submitted and build a web interface that can also interact with the data?
This is what I am thinking so far:
Create a 'toString' method that will convert my class into a textual representation. Then store the string on a server. This will replace storing nsdata online. Does this sound appropriate?
However, I want to be able to query the amount of restaurants and have a bit more control over my online 'database'. Is XML the answer? I was about the start looking into Amazon Web Services and learn either Json or Rest.
Any bits of advice?
Thanks
Dan

I am not very familiar with iPhone development yet, but if there are reasonably easy way to consume web services I would recommend using that with typed parameters. This would make these services more "usable" from other systems too, they would not need to know how to serialize/pack objects to a string but only how to call a function: storeRestaurang(string name, int rating...) etc.
This is an article I will check out myself since I hope to get into iPhone development a bit more: http://icodeblog.com/2008/11/03/iphone-programming-tutorial-intro-to-soap-web-services/

Looks like you want to use a plist.
Any of the cocoa collection classes can be written out to a plist, which is a breed of xml file. The sweet part is if you use standard cocoa value classes, you can write the collection out, and read it back in later.
NSString, NSNumber, NSDate, NSData, NSAray, NSDictionary objects can be put into a collection and stored to a plist directly. Other classes will have to be serialized into NSData then written to disk as a plist. For truly custom data, an NSKeyedArchiver is probably what you want.
For simple strings and numbers data, pack it all into an NSDictionary and then write it to a plist. Now look around on your mac and you'll see just how popular plists are.
NSDictionary API - Storing Dictionaries
Property List Programming Guide
Archives and Serializations Programming Guide for Cocoa

Learn about REST, it's the easiest and cleanest way to provide a web-based API. Some frameworks, like Ruby on Rails, give you a REST interface right out of the box.
Then, you'll need to find a way to convert your objects on the iPhone into a REST-friendly format. XML and JSON are both options, but unfortunately I haven't seen any code for converting to/from XML or JSON that I can really recommend, since I haven't tried any just yet. But, there are some projects that might lead you in the right direction. One of the most promising-looking is this:
http://github.com/yfactorial/objectiveresource/tree/master

Related

iphone: Caching an NSMutableArray?

I'm currently writing a Twitter client for iPhone and need a way to cache the public timeline. The data fetched is stored as 'Status' objects in an NSMutableArray.
The NSMutableArray stores 20 objects at a time.
What is the best way to do this?
You will probably want to use Core Data. You'll need to create an entity for each tweet, and then store that in your database. There is quite a lot of documentation and sample code on Core Data out there; start at Apple's Samples page.
If you don't want to play with one of the frameworks already mentioned (SQLitePersistentObjects and CoreData), you could implement your own using the NSCoding protocol on your status objects. The array should already implement NSCoding for archiving/unarchiving.
If you want to start quickly try SQLitePersistentObjects.
If you never plan to go over 20 objects you could (slightly) violate some rules from the gods of apple, and use NSUserDefaults - likely the easiest one to implement, but as the name implies, made for things like user settings, so don't use for big piles of info.
Disclaimer: If you know how to use the others, use them, this one is kinda the lazy way if you don't want to learn something better but more difficult.

How to use local dictionary to my app?

In My application, how should i find meaning of the word, it should work both online and offline? Please help me out also is there any possiblilite to take local dictionary from iphone?
Sri
As far as I am currently aware, you aren't able to query the iPhone's built in dictionary.
Even if you could, I am not sure that it contains any definitions. Rather it looks for spelling correction on "close" words.
If you are wanting to replicate storing a dictionary inside your app, like the many dictionary apps currently out there, you would have to store the data in your iPhone app somehow.
Wikitionary provides free "dumps" of it's data which you could grab, and put into a form that would be usable in your app.
You would need to store the data in some form of plist, an SQLite database or CoreData.
For taking data from the internet, you would need to look at transferring the data using JSON, XML or PLIST.

Formats for communicating between backend and Objective-C/Cocoa

I'm developing an iPhone app that is connected to a backend server. It needs to communicate with it many times, through several requests. I'm sending HTTP messages, but I want to receive more complex responses that I can parse somehow. Supposedly, I can provide any type of format for responses from the server, so my question is: which one would be easier(maybe even faster) to use/parse for Objective-C/Cocoa, and transform in a dictionary of some kind?
I know it's a bit subjective but I still think it's a valid question, some programming languages just have more support for some formats and less for others.
From Cocoa 's perspective, the simplest format is a property list as Cocoa can parse this natively into a dictionary or an array.
You can use NSDictionary's +dictionaryWithContentsOfFile: and +dictionaryWithContentsOfUrl: to read a plist file into a dictionary.
If your plist data is not in a file, you can also convert an NSData object containing plist data to a dictionary with +[NSPropertyListSerialization dataFromPropertyList:format:errorDescription:] or convert an NSString to a dictionary with -[NSString propertyList].
PList is a good answer and very usable, but many server side people will be more comfortable producing JSON - TouchJSON is a very good JSON parser for the iPhone.
While there is a plist gem for ruby, JSON or (raw) XML are much more popular outside the Apple world. For instance most JavaScript libraries are set up to speak one or both of these.
So if you're exclusively talking to an iPhone, the plist is probably a good choice, but otherwise you should consider using JSON (or XML).

Client Server iPhone App

I need build a client server iphone app. Want to store a database on server and save it too on client's iphone. What strategy and what dbms' and tools i must use for it? AS my database can be enough heavy
This is EASY. If you control both parts of the system.
The magic word here is PLIST's.
IBM have a great example with an iPhone source project and a working google app engine deploy.
http://www.ibm.com/developerworks/web/library/wa-aj-iphone/
Here is some ultra basic code.
// SaveOnline.
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:#"one",#"two",nil];
NSURL *url = [NSURL URLWithString:#"http://www.hurl.ws/api/"];
ok = [myArray writeToURL:url atomically:NO];
if(ok) NSLog(#"saved worked");
You can also load this PLIST from the URL and load it back into your object pretty easily as well. The whole PLIST system is very cool. It is slighly verbose but I would not worry about that as it is ultra flexible and in the long run is going to save you hour and hours of debugging.
I also noticed there a lots of libraries on the server to convert PLISTs into native objects for PHP, Python and assume you can find libraries for Java or .Net.
Dont think about trying to do it in XML your self, its going to get messy ultra quick and yo are going to loose so much time trying to fix it when you dont need.
PLIST's are you friend so use them. John.
For saving to the server, you can try out Parse: http://www.parse.com, they seem to have an easy system for storing data (without you setting up any servers).
On top of that, you can go with persisting the data locally as plist files. Here's a pretty good rundown of the different types of storage options: http://doganberktas.com/2010/10/16/data-storage-alternatives-on-ios-in-a-nutshell/
If you're going for simple, you should just go with storing data in a plist, preferably using the NSCoding protocol, which will allow you to easily store arbitrary objects.
If your database objects are complex you will probably want to use core data and model the objects your own way in the iphone. Of course you will have to sort of translate them when you are going from your service database to storing in core data but that should not be too difficult. If you getting and XML or Json response from your service you should be able to easily parse them and construct your objects over on the iphone and just simply use core data to store them.
The database that you are using on the server side doesn't matter, it's just a black box to the iPhone app. The app can communicate with this server via HTTP using XML, or PLISTs or JSON as John and Daniel recommend.
As far as what's easiest, just go with what server side language you already know. It's probably easiest to run it on something akin to Google app engine, I'd imagine.
bpapa you note "the app can communicate with this server via HTTP using XML", I assume from that you mean web services?
I ask because I'm trying to demystify getting data from the iPhone to a data store such as provided by Google App Engine, and back.
Thanks // :)
P.S.) More to the point of this thread, Google has a great solution for scaleable data store creation without large capital investment. I've been looking at it, Amazon Web Services, and Windows Azure. It seems that, unless you have a heavy reliance on or strong ability in .net, Google's solution makes sense.

Is there an easy way to work with web services for iPhone dev?

We want to take XML data and convert it to an NSDictionary object, but we don't want to manually iterate over the XML. Is there an easy way to do this? How are you doing web services for your iPhone app?
If you have control over the XML output you could try creating a property list which you can then read into a dictionary using -dictionaryWithContentsOfURL: (though the better asynchronous way would be to get the data using an NSURLConnection and then converting the data using the -propertyList method on NSString). You can find more about property lists here: http://developer.apple.com/documentation/Cocoa/Conceptual/PropertyLists/Introduction/chapter_1_section_1.html
Of course the best solution is to use a RESTful client and use a combination of NSURLConnection to get/send the data and the TouchXML classes (http://code.google.com/p/touchcode/wiki/TouchXML) to parse the data, though this would require more work to put the data into a dictionary. Of course if these are going to be the main data objects in your system you really want to be using either a custom class or SQLite to store the data as it provides you much more reliability in testing your app than a dictionary.
If you can control the server output, try using plists. Otherwise you're stuck with parsing XML (or JSON if the server can do that), but there are frameworks you can use. See the answer to this question.
Also, here is a good overview of how to do RESTful clients on the iphone:
https://developer.apple.com/webapps/articles/creatingrestfulclients.html
You can return the data in JSON format. There are many open-source JSON parsers available for the iPhone (TouchJSON being one).
There's another class available called NSPropertyListSerialization which gets you a dictionary from data.
You can do something like this with the data you receive
NSDictionary* propertyList;
NSPropertyListFormat format;
NSString *errorStr;
propertyList = [NSPropertyListSerialization
propertyListFromData:receivedData
mutabilityOption: NSPropertyListImmutable
format: &format
errorDescription: &errorStr];
Sorry, don't know what tags are used here for formatting code!