Posts Tagged ‘iphone’

RailsConf – Day 1 – Mobile App Development with iPhone and Rails

June 7th, 2010 by Brian | No Comments | Filed in work

Mike Clark http://en.oreilly.com/rails2010/public/schedule/detail/14136

This is going to be mostly an iPhone application development session, not Rails.
Demo app is performs CRUD operations on a Rails 3 site from an iPhone app

http://github.com/clarkware/iphone-rails-tutorial

Basic Railsy JSON controller methods

Standard iPhone callback methods: applicationWillTerminate, didFinishLaunchingWithOptions

in .h file – create instance variables in the @interface and add:
@property(nonatomic, retain) NSMutableArray *goals;

in .m use: @synthesize goals (initializes instance variable), also release in dealloc()

viewDidLoad – called when the view is loaded
reference “goals” with “self.goals” to access variable through property

“Do not fear the square brackets”

cellForRowAtIndexPath – don’t delete what’s there (that’s what makes scrolling through large list fast), just add your own logic to the end of the method, before “return cell”;

commitEditingStyle – delete from our array – allows deletion in app. Restart app to get data back (since it’s in memory)

StringWithContentsOfURL – fetch URL synchronously (blocks main UI thread)
json-framework is a strict JSON parser and generator
return NSDictionary or NSArray of NSDictionaries

refreshButton – “action:@selector(refresh)” — is like using “send” in ruby. target:self” means that self.refresh will be called on click

networkActivityIndicatorVisible – network spinner

NSURLRequest, NSURLConnection, sendSynchronousRequest

Shift – Apple – R brings up debug console in Xcode

After break – Asynchronous Networking, Authentication, Error handling ….

Async Networking – done with the Delegate pattern – http://en.wikipedia.org/wiki/Delegation_pattern

Use an OperationQueue to background work – NSOperationQueue, NSInvocationOperation – don’t use NSThread
Always update the UI on the main thread – performSelectorMainThread

HTTPRiot is a simple REST library that makes it easy to interact with RESTful resources (inspired by httparty) – responses converted to NSDictionary and NSArray objects.

http://allseeing-i.com/ASIHTTPRequest/ – flexible, low-level library with tons of features – File uploads, post form-encoded data, basic authentication, progress indictors, etc. Not REST specific, doesn’t handle json/xml by default, but very useful for low-level stuff

ObjectiveResource is an Objective-C port of ActiveResource – Automatically serializes/deserializes objects (to/from JSON or XML) – Assumes Rails RESTful conventions – adds methods to NSObject

didFinishLaunchWithOptions – only gets called once – when app starts up

ObjectiveResource needs the following to give json a default root object:
ActiveRecord::Base.include_root_in_json = true

Tags: ,