cs193p - lecture 9 · cs193p - lecture 9 iphone application development data in your iphone app...

166
CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino 1 Tuesday, February 2, 2010

Upload: others

Post on 12-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

CS193P - Lecture 9iPhone Application Development

Data in Your iPhone AppChris Marcellino

1Tuesday, February 2, 2010

Page 2: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Today’s Topics• Data in Your iPhone App

■ Saving & loading local data■ Accessing remote data over the Internet

2Tuesday, February 2, 2010

Page 3: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Today’s Topics• Property Lists, NSUserDefaults and Settings• iPhone’s File System• Archiving Objects• The Joy of SQLite• JSON• Apple Push Notification Service

3Tuesday, February 2, 2010

Page 4: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Property Lists

4Tuesday, February 2, 2010

Page 5: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Property Lists• Convenient way to store a small amount of data

■ Arrays, dictionaries, strings, numbers, dates, raw data■ Human-readable XML or binary format

• NSUserDefaults class uses property lists under the hood

5Tuesday, February 2, 2010

Page 6: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

When Not to Use Property Lists• More than a few hundred KB of data

■ Loading a property list is all-or-nothing

• Complex object graphs• Custom object types• Multiple writers (e.g. not ACID)

6Tuesday, February 2, 2010

Page 7: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Reading & Writing Property Lists• NSArray and NSDictionary convenience methods• Operate recursively// Writing- (BOOL)writeToFile:(NSString *)aPath atomically:(BOOL)flag;- (BOOL)writeToURL:(NSURL *)aURL atomically:(BOOL)flag;

// Reading- (id)initWithContentsOfFile:(NSString *)aPath;- (id)initWithContentsOfURL:(NSURL *)aURL;

7Tuesday, February 2, 2010

Page 8: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Writing an Array to Disk

8Tuesday, February 2, 2010

Page 9: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Writing an Array to DiskNSArray *array = [NSArray arrayWithObjects:@“Foo”,

8Tuesday, February 2, 2010

Page 10: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Writing an Array to DiskNSArray *array = [NSArray arrayWithObjects:@“Foo”, [NSNumber numberWithBool:YES],

8Tuesday, February 2, 2010

Page 11: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Writing an Array to DiskNSArray *array = [NSArray arrayWithObjects:@“Foo”, [NSNumber numberWithBool:YES], [NSDate dateWithTimeIntervalSinceNow:60],

8Tuesday, February 2, 2010

Page 12: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Writing an Array to DiskNSArray *array = [NSArray arrayWithObjects:@“Foo”, [NSNumber numberWithBool:YES], [NSDate dateWithTimeIntervalSinceNow:60], nil];

8Tuesday, February 2, 2010

Page 13: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Writing an Array to DiskNSArray *array = [NSArray arrayWithObjects:@“Foo”, [NSNumber numberWithBool:YES], [NSDate dateWithTimeIntervalSinceNow:60], nil];[array writeToFile:@“MyArray.plist” atomically:YES];

8Tuesday, February 2, 2010

Page 14: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Writing an Array to DiskNSArray *array = [NSArray arrayWithObjects:@“Foo”, [NSNumber numberWithBool:YES], [NSDate dateWithTimeIntervalSinceNow:60], nil];[array writeToFile:@“MyArray.plist” atomically:YES];

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><array> <string>Foo</string> <true/> <date>2010-02-02T09:26:18Z</date>

</array></plist>

8Tuesday, February 2, 2010

Page 15: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Writing a Dictionary to Disk

9Tuesday, February 2, 2010

Page 16: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Writing a Dictionary to DiskNSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:

9Tuesday, February 2, 2010

Page 17: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Writing a Dictionary to DiskNSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @“Bob”, @“Name”,

9Tuesday, February 2, 2010

Page 18: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Writing a Dictionary to DiskNSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @“Bob”, @“Name”, [NSNumber numberWithInt:9], @“Lecture”,

9Tuesday, February 2, 2010

Page 19: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Writing a Dictionary to DiskNSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @“Bob”, @“Name”, [NSNumber numberWithInt:9], @“Lecture”, nil];

9Tuesday, February 2, 2010

Page 20: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Writing a Dictionary to DiskNSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @“Bob”, @“Name”, [NSNumber numberWithInt:9], @“Lecture”, nil];[dict writeToFile:@“MyDict.plist” atomically:YES];

9Tuesday, February 2, 2010

Page 21: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Writing a Dictionary to DiskNSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @“Bob”, @“Name”, [NSNumber numberWithInt:9], @“Lecture”, nil];[dict writeToFile:@“MyDict.plist” atomically:YES];

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict> <key>Name</key> <string>Bob</string> <key>Lecture</key> <integer>10</integer>

</dict></plist>

9Tuesday, February 2, 2010

Page 22: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

NSPropertyListSerialization• Allows finer-grained control

■ File format■ More descriptive errors■ Mutability

10Tuesday, February 2, 2010

Page 23: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

NSPropertyListSerialization• Allows finer-grained control

■ File format■ More descriptive errors■ Mutability

// Property list to NSData+ (NSData *)dataFromPropertyList:(id)plist format:(NSPropertyListFormat)format errorDescription:(NSString **)errorString;

// NSData to property list+ (id)propertyListFromData:(NSData *)data mutabilityOption:(NSPropertyListMutabilityOptions)opt format:(NSPropertyListFormat *)format errorDescription:(NSString **)errorString;

10Tuesday, February 2, 2010

Page 24: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

More on Property Lists• “Property List Programming Guide for Cocoa”

http://developer.apple.com/documentation/Cocoa/Conceptual/PropertyLists/

11Tuesday, February 2, 2010

Page 25: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

iPhone’s File System

12Tuesday, February 2, 2010

Page 26: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Keeping Applications Separate

Image (cc) by davidsilver on Flickr

13Tuesday, February 2, 2010

Page 27: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Why Keep Applications Separate?• Security• Privacy• Cleanup after deleting an app

14Tuesday, February 2, 2010

Page 28: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Home Directory Layout

15Tuesday, February 2, 2010

Page 29: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Home Directory Layout• Each app has its own set of directories

15Tuesday, February 2, 2010

Page 30: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Home Directory Layout• Each app has its own set of directories• <Application Home>

15Tuesday, February 2, 2010

Page 31: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Home Directory Layout• Each app has its own set of directories• <Application Home>

■ MyApp.app

15Tuesday, February 2, 2010

Page 32: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Home Directory Layout• Each app has its own set of directories• <Application Home>

■ MyApp.app■ MyApp

15Tuesday, February 2, 2010

Page 33: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Home Directory Layout• Each app has its own set of directories• <Application Home>

■ MyApp.app■ MyApp■ MainWindow.nib

15Tuesday, February 2, 2010

Page 34: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Home Directory Layout• Each app has its own set of directories• <Application Home>

■ MyApp.app■ MyApp■ MainWindow.nib■ SomeImage.png

15Tuesday, February 2, 2010

Page 35: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Home Directory Layout• Each app has its own set of directories• <Application Home>

■ MyApp.app■ MyApp■ MainWindow.nib■ SomeImage.png

■ Documents

15Tuesday, February 2, 2010

Page 36: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Home Directory Layout• Each app has its own set of directories• <Application Home>

■ MyApp.app■ MyApp■ MainWindow.nib■ SomeImage.png

■ Documents■ Library

15Tuesday, February 2, 2010

Page 37: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Home Directory Layout• Each app has its own set of directories• <Application Home>

■ MyApp.app■ MyApp■ MainWindow.nib■ SomeImage.png

■ Documents■ Library

■ Caches

15Tuesday, February 2, 2010

Page 38: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Home Directory Layout• Each app has its own set of directories• <Application Home>

■ MyApp.app■ MyApp■ MainWindow.nib■ SomeImage.png

■ Documents■ Library

■ Caches■ Preferences

15Tuesday, February 2, 2010

Page 39: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Home Directory Layout• Each app has its own set of directories• <Application Home>

■ MyApp.app■ MyApp■ MainWindow.nib■ SomeImage.png

■ Documents■ Library

■ Caches■ Preferences

• Applications only read and write within their home directory

15Tuesday, February 2, 2010

Page 40: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Home Directory Layout• Each app has its own set of directories• <Application Home>

■ MyApp.app■ MyApp■ MainWindow.nib■ SomeImage.png

■ Documents■ Library

■ Caches■ Preferences

• Applications only read and write within their home directory• Backed up by iTunes during sync (mostly)

15Tuesday, February 2, 2010

Page 41: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

File Paths in Your Application

16Tuesday, February 2, 2010

Page 42: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

File Paths in Your Application// Basic directoriesNSString *homePath = NSHomeDirectory();NSString *tmpPath = NSTemporaryDirectory();

16Tuesday, February 2, 2010

Page 43: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

File Paths in Your Application// Basic directoriesNSString *homePath = NSHomeDirectory();NSString *tmpPath = NSTemporaryDirectory();

// Documents directoryNSArray *paths = NSSearchPathForDirectoriesInDomains

(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsPath = [paths objectAtIndex:0];

16Tuesday, February 2, 2010

Page 44: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

File Paths in Your Application// Basic directoriesNSString *homePath = NSHomeDirectory();NSString *tmpPath = NSTemporaryDirectory();

// Documents directoryNSArray *paths = NSSearchPathForDirectoriesInDomains

(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsPath = [paths objectAtIndex:0];

// <Application Home>/Documents/foo.plistNSString *fooPath =[documentsPath stringByAppendingPathComponent:@“foo.plist”];

16Tuesday, February 2, 2010

Page 45: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Including Writable Files with Your App• Many applications want to include some starter data• But application bundles are code signed

■ You can’t modify the contents of your app bundle

• To include a writable data file with your app...■ Build it as part of your app bundle■ On first launch, copy it to your Documents directory

17Tuesday, February 2, 2010

Page 46: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Archiving Objects

18Tuesday, February 2, 2010

Page 47: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Archiving Objects• Next logical step from property lists

■ Include arbitrary classes■ Complex object graphs

• Used by Interface Builder for NIBs

19Tuesday, February 2, 2010

Page 48: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Making Objects Archivable• Conform to the <NSCoding> protocol

20Tuesday, February 2, 2010

Page 49: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Making Objects Archivable• Conform to the <NSCoding> protocol

// Encode an object for an archive- (void)encodeWithCoder:(NSCoder *)coder{

[super encodeWithCoder:coder];[coder encodeObject:name forKey:@“Name”];[coder encodeInteger:numberOfSides forKey:@“Sides”];

}

// Decode an object from an archive- (id)initWithCoder:(NSCoder *)coder{

self = [super initWithCoder:coder];name = [[coder decodeObjectForKey:@“Name”] retain];numberOfSides = [coder decodeIntegerForKey:@“Side”];

}

20Tuesday, February 2, 2010

Page 50: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Archiving & Unarchiving Object Graphs

21Tuesday, February 2, 2010

Page 51: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Archiving & Unarchiving Object Graphs• Creating an archive

NSArray *polygons = ...;NSString *path = ...;BOOL result = [NSKeyedArchiver archiveRootObject:polygons toFile:path];

21Tuesday, February 2, 2010

Page 52: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Archiving & Unarchiving Object Graphs• Creating an archive

• Decoding an archive

NSArray *polygons = ...;NSString *path = ...;BOOL result = [NSKeyedArchiver archiveRootObject:polygons toFile:path];

NSArray *polygons = nil;NSString *path = ...;polygons = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

21Tuesday, February 2, 2010

Page 53: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

More on Archiving Objects• “Archives and Serializations Programming Guide for Cocoa”

http://developer.apple.com/documentation/Cocoa/Conceptual/Archiving/

22Tuesday, February 2, 2010

Page 54: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

The Joy of SQLite

23Tuesday, February 2, 2010

Page 55: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

SQLite• Complete SQL database in an ordinary file• Simple, compact, fast, reliable• No server• Free/Open Source Software• Great for embedded devices

■ Included on the iPhone platform

24Tuesday, February 2, 2010

Page 56: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

When Not to Use SQLite• Multi-gigabyte databases• High concurrency (multiple writers)• Client-server applications• “Appropriate Uses for SQLite”

http://www.sqlite.org/whentouse.html

25Tuesday, February 2, 2010

Page 57: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

SQLite C API Basics

26Tuesday, February 2, 2010

Page 58: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

SQLite C API Basics• Open the database

int sqlite3_open(const char *filename, sqlite3 **db);

26Tuesday, February 2, 2010

Page 59: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

SQLite C API Basics• Open the database

• Execute a SQL statement

int sqlite3_open(const char *filename, sqlite3 **db);

int sqlite3_exec(sqlite3 *db, const char *sql, int (*callback)(void*,int,char**,char**), void *context, char **error);

26Tuesday, February 2, 2010

Page 60: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

SQLite C API Basics• Open the database

• Execute a SQL statement

int sqlite3_open(const char *filename, sqlite3 **db);

int sqlite3_exec(sqlite3 *db, const char *sql, int (*callback)(void*,int,char**,char**), void *context, char **error);

// Your callbackint callback(void *context, int count, char **values, char **columns);

26Tuesday, February 2, 2010

Page 61: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

SQLite C API Basics• Open the database

• Execute a SQL statement

• Close the database

int sqlite3_open(const char *filename, sqlite3 **db);

int sqlite3_exec(sqlite3 *db, const char *sql, int (*callback)(void*,int,char**,char**), void *context, char **error);

int sqlite3_close(sqlite3 *db);

// Your callbackint callback(void *context, int count, char **values, char **columns);

26Tuesday, February 2, 2010

Page 62: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Demo:Simple SQLite

27Tuesday, February 2, 2010

Page 63: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

More on SQLite• “SQLite in 5 Minutes Or Less”

http://www.sqlite.org/quickstart.html• “Intro to the SQLite C Interface”

http://www.sqlite.org/cintro.html

28Tuesday, February 2, 2010

Page 64: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Core Data• Object-graph management and persistence framework

■ Makes it easy to save & load model objects■ Properties■ Relationships

■ Higher-level abstraction than SQLite or property lists

• Available on the Mac OS X desktop• Now available on iPhone OS 3.0

29Tuesday, February 2, 2010

Page 65: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Two classes you should know about...

30Tuesday, February 2, 2010

Page 66: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Two classes you should know about... • NSPredicate

30Tuesday, February 2, 2010

Page 67: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Two classes you should know about... • NSPredicate

■ “Used to define logical conditions used to constrain a search either for a fetch or for in-memory filtering.”

30Tuesday, February 2, 2010

Page 68: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Two classes you should know about... • NSPredicate

■ “Used to define logical conditions used to constrain a search either for a fetch or for in-memory filtering.”

■ -[NSPredicate predicateWithFormat:]

30Tuesday, February 2, 2010

Page 69: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Two classes you should know about... • NSPredicate

■ “Used to define logical conditions used to constrain a search either for a fetch or for in-memory filtering.”

■ -[NSPredicate predicateWithFormat:]■ Simple comparisons:

30Tuesday, February 2, 2010

Page 70: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Two classes you should know about... • NSPredicate

■ “Used to define logical conditions used to constrain a search either for a fetch or for in-memory filtering.”

■ -[NSPredicate predicateWithFormat:]■ Simple comparisons:

■ grade == “7”

30Tuesday, February 2, 2010

Page 71: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Two classes you should know about... • NSPredicate

■ “Used to define logical conditions used to constrain a search either for a fetch or for in-memory filtering.”

■ -[NSPredicate predicateWithFormat:]■ Simple comparisons:

■ grade == “7”■ user.firstName like "Tom"

30Tuesday, February 2, 2010

Page 72: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Two classes you should know about... • NSPredicate

■ “Used to define logical conditions used to constrain a search either for a fetch or for in-memory filtering.”

■ -[NSPredicate predicateWithFormat:]■ Simple comparisons:

■ grade == “7”■ user.firstName like "Tom"■ “first contains [c]”chris”

30Tuesday, February 2, 2010

Page 73: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Two classes you should know about... • NSPredicate

■ “Used to define logical conditions used to constrain a search either for a fetch or for in-memory filtering.”

■ -[NSPredicate predicateWithFormat:]■ Simple comparisons:

■ grade == “7”■ user.firstName like "Tom"■ “first contains [c]”chris”

■ Many, many options: http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/Predicates/Articles/pSyntax.html

30Tuesday, February 2, 2010

Page 74: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Two classes you should know about...

31Tuesday, February 2, 2010

Page 75: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Two classes you should know about... • NSEntityDescription

31Tuesday, February 2, 2010

Page 76: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Two classes you should know about... • NSEntityDescription

■ Used for inserting a new object into a Core Data Managed Object context

31Tuesday, February 2, 2010

Page 77: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Two classes you should know about... • NSEntityDescription

■ Used for inserting a new object into a Core Data Managed Object context

■ - [NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:]

31Tuesday, February 2, 2010

Page 78: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Two classes you should know about... • NSEntityDescription

■ Used for inserting a new object into a Core Data Managed Object context

■ - [NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:]

■ See the documentation!

31Tuesday, February 2, 2010

Page 79: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Two classes you should know about... • NSEntityDescription

■ Used for inserting a new object into a Core Data Managed Object context

■ - [NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:]

■ See the documentation!■ http://developer.apple.com/mac/library/documentation/cocoa/

reference/CoreDataFramework/Classes/NSEntityDescription_Class/NSEntityDescription.html

31Tuesday, February 2, 2010

Page 80: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Web Services

32Tuesday, February 2, 2010

Page 81: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Your Application & The Cloud• Store & access remote data• May be under your control or someone else’s• Many Web 2.0 apps/sites provide developer API

33Tuesday, February 2, 2010

Page 82: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

“I made a location-based user-generated video blogging mashup... for pets!”

34Tuesday, February 2, 2010

Page 83: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Integrating with Web Services• Non-goal of this class: teach you all about web services

■ Plenty of tutorials accessible, search on Google

• Many are exposed via RESTful interfaces with XML or JSON■ REpresentational State Transfer

■ Stateless interactions■ Well defined client/server roles & interfaces■ e.g. HTTP

• High level overview of parsing these types of data

35Tuesday, February 2, 2010

Page 84: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

XML

36Tuesday, February 2, 2010

Page 85: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Options for Parsing XML• libxml2

■ Tree-based: easy to parse, entire tree in memory■ Event-driven: less memory, more complex to manage state■ Text reader: fast, easy to write, efficient

• NSXMLParser■ Event-driven API: simpler but less powerful than libxml2

37Tuesday, February 2, 2010

Page 86: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

More on Parsing XML• Brent Simmons, “libxml2 + xmlTextReader on Macs”

http://inessential.com/?comments=1&postid=3489■ Includes example of parsing Twitter XML!

• Big Nerd Ranch, “Parsing XML in Cocoa”http://weblog.bignerdranch.com/?p=48■ Covers the basics of NSXMLReader

38Tuesday, February 2, 2010

Page 87: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

JSON

39Tuesday, February 2, 2010

Page 88: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

JavaScript Object Notation• More lightweight than XML• Looks a lot like a property list

■ Arrays, dictionaries, strings, numbers

• Open source json-framework wrapper for Objective-C

40Tuesday, February 2, 2010

Page 89: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

What does a JSON object look like?

41Tuesday, February 2, 2010

Page 90: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

What does a JSON object look like?

{

41Tuesday, February 2, 2010

Page 91: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

What does a JSON object look like?

{“instructor” : “Josh Shaffer”,

41Tuesday, February 2, 2010

Page 92: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

What does a JSON object look like?

{“instructor” : “Josh Shaffer”,“students” : 60,

41Tuesday, February 2, 2010

Page 93: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

What does a JSON object look like?

{“instructor” : “Josh Shaffer”,“students” : 60,“itunes-u” : true,

41Tuesday, February 2, 2010

Page 94: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

What does a JSON object look like?

{“instructor” : “Josh Shaffer”,“students” : 60,“itunes-u” : true,“midterm-exam” : null,

41Tuesday, February 2, 2010

Page 95: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

What does a JSON object look like?

{“instructor” : “Josh Shaffer”,“students” : 60,“itunes-u” : true,“midterm-exam” : null,“assignments” : [ “WhatATool”,

41Tuesday, February 2, 2010

Page 96: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

What does a JSON object look like?

{“instructor” : “Josh Shaffer”,“students” : 60,“itunes-u” : true,“midterm-exam” : null,“assignments” : [ “WhatATool”,

“HelloPoly”,

41Tuesday, February 2, 2010

Page 97: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

What does a JSON object look like?

{“instructor” : “Josh Shaffer”,“students” : 60,“itunes-u” : true,“midterm-exam” : null,“assignments” : [ “WhatATool”,

“HelloPoly”, “Presence” ]

41Tuesday, February 2, 2010

Page 98: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

What does a JSON object look like?

{“instructor” : “Josh Shaffer”,“students” : 60,“itunes-u” : true,“midterm-exam” : null,“assignments” : [ “WhatATool”,

“HelloPoly”, “Presence” ]

}

41Tuesday, February 2, 2010

Page 99: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Using json-framework• Reading a JSON string into Foundation objects

42Tuesday, February 2, 2010

Page 100: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Using json-framework• Reading a JSON string into Foundation objects

#import <JSON/JSON.h>

42Tuesday, February 2, 2010

Page 101: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Using json-framework• Reading a JSON string into Foundation objects

#import <JSON/JSON.h>

// Get a JSON string from the cloudNSString *jsonString = ...;

42Tuesday, February 2, 2010

Page 102: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Using json-framework• Reading a JSON string into Foundation objects

#import <JSON/JSON.h>

// Get a JSON string from the cloudNSString *jsonString = ...;

// Parsing will result in Foundation objects// Top level may be an NSDictionary or an NSArrayid object = [jsonString JSONValue];

42Tuesday, February 2, 2010

Page 103: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Using json-framework• Writing a JSON string from Foundation objects

43Tuesday, February 2, 2010

Page 104: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Using json-framework• Writing a JSON string from Foundation objects

// Create some data in your app

43Tuesday, February 2, 2010

Page 105: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Using json-framework• Writing a JSON string from Foundation objects

// Create some data in your appNSDictionary *dictionary = ...;

// Convert into a JSON string before sending to the cloud

43Tuesday, February 2, 2010

Page 106: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Using json-framework• Writing a JSON string from Foundation objects

// Create some data in your appNSDictionary *dictionary = ...;

// Convert into a JSON string before sending to the cloudjsonString = [dictionary JSONRepresentation];

43Tuesday, February 2, 2010

Page 107: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Demo:Flickr API with JSON

44Tuesday, February 2, 2010

Page 108: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

More on JSON• “JSON Parser/Generator for Objective-C”

http://code.google.com/p/json-framework/• “Introducing JSON”

http://www.json.org/

45Tuesday, February 2, 2010

Page 109: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Apple Push Notification Service

46Tuesday, February 2, 2010

Page 110: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Overview

47Tuesday, February 2, 2010

Page 111: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

• Show badges, alerts and play sounds without app running

Overview

47Tuesday, February 2, 2010

Page 112: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

• Show badges, alerts and play sounds without app running

• Minimal server infrastructure needed

Overview

47Tuesday, February 2, 2010

Page 113: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

• Show badges, alerts and play sounds without app running

• Minimal server infrastructure needed

• Preserves battery life: 1 versus n TCP/IP connections

Overview

ServerApple Push

Service

47Tuesday, February 2, 2010

Page 114: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Using the Service

Server

48Tuesday, February 2, 2010

Page 115: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

What you needUsing the Service

Server

48Tuesday, February 2, 2010

Page 116: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

What you needUsing the Service

Server

48Tuesday, February 2, 2010

Page 117: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

What you needUsing the Service

edu.stanford.cs193.app

Server

48Tuesday, February 2, 2010

Page 118: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

What you needUsing the Service

edu.stanford.cs193.app

Server

48Tuesday, February 2, 2010

Page 119: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Server

Using the ServiceWhat you do

Apple Push

Service

49Tuesday, February 2, 2010

Page 120: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

1. Register with the serviceUsing the Service

Chris’s iPhoneApple Push

Service

50Tuesday, February 2, 2010

Page 121: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Server

Using the Service

Chris’s iPhone

51Tuesday, February 2, 2010

Page 122: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Server

2. Send token to your serverUsing the Service

Chris’s iPhone

51Tuesday, February 2, 2010

Page 123: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Server

3. Send notificationsUsing the Service

Chris’s iPhone

Apple Push

Service

52Tuesday, February 2, 2010

Page 124: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Server

3. Send notificationsUsing the Service

Chris’s iPhone

Apple Push

ServiceJSON

52Tuesday, February 2, 2010

Page 125: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

4. Receive notificationsUsing the Service

Apple Push

Service

53Tuesday, February 2, 2010

Page 126: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

4. Receive notificationsUsing the Service

Apple Push

ServiceJSON

53Tuesday, February 2, 2010

Page 127: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

1. Register with the serviceUsing the Service

Apple Push

ServiceChris’s iPhone

54Tuesday, February 2, 2010

Page 128: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

1. Register with the serviceUsing the Service

Apple Push

ServiceChris’s iPhone

54Tuesday, February 2, 2010

Page 129: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Application launch

• UIKit API in UIApplication.h to register■ Pass the types you want to receive

Registering with the Service

-(void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options{

// Register this app on this device UIRemoteNotificationType myTypes = UIRemoteNotificationTypeSounds | UIRemoteNotificationTypeBadges; [application registerForRemoteNotificationTypes:myTypes];

}

55Tuesday, February 2, 2010

Page 130: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Delegate callbacksRegistering with the Service

56Tuesday, February 2, 2010

Page 131: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Delegate callbacksRegistering with the Service

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token{ // Phone home with device token}

56Tuesday, February 2, 2010

Page 132: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Delegate callbacksRegistering with the Service

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ // Oh noes! Check your Provisioning Profile on device and in Xcode}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token{ // Phone home with device token}

56Tuesday, February 2, 2010

Page 133: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Registering with the Service

96385da767191121a851963983fdac9bbdf74dcf6219ae14ed8d08228

57Tuesday, February 2, 2010

Page 134: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

The device tokenRegistering with the Service

96385da767191121a851963983fdac9bbdf74dcf6219ae14ed8d08228

57Tuesday, February 2, 2010

Page 135: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

The device token

• Uniquely identifies device

Registering with the Service

96385da767191121a851963983fdac9bbdf74dcf6219ae14ed8d08228

57Tuesday, February 2, 2010

Page 136: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

The device token

• Uniquely identifies device ■ Distinct from -[UIDevice deviceIdentifier]

Registering with the Service

96385da767191121a851963983fdac9bbdf74dcf6219ae14ed8d08228

57Tuesday, February 2, 2010

Page 137: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

The device token

• Uniquely identifies device ■ Distinct from -[UIDevice deviceIdentifier]

• Just call registration API again if token is needed

Registering with the Service

96385da767191121a851963983fdac9bbdf74dcf6219ae14ed8d08228

57Tuesday, February 2, 2010

Page 138: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

• UIApplicationDelegate

Registering for NotificationsOptional callbacks and methods

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

58Tuesday, February 2, 2010

Page 139: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

• UIApplication

• UIApplicationDelegate

Registering for NotificationsOptional callbacks and methods

- (UIRemoteNotificationType)enabledRemoteNotificationTypes

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

58Tuesday, February 2, 2010

Page 140: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Chris’s iPhone

Using the Service

Server

59Tuesday, February 2, 2010

Page 141: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Chris’s iPhone

2. Send token to your serverUsing the Service

Server

59Tuesday, February 2, 2010

Page 142: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Server

3. Send notificationsUsing the Service

Chris’s iPhone

Apple Push

Service

60Tuesday, February 2, 2010

Page 143: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Server

3. Send notificationsUsing the Service

Chris’s iPhone

edu.s.cs193

Apple Push

ServiceJSON

60Tuesday, February 2, 2010

Page 144: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Sending Notifications

{ "aps" : { "alert" : "Jen: Sushi at 10?", "badge" : 1, "sound" : "Jingle.aiff" }, "acme1" : "conversation9964"}

61Tuesday, February 2, 2010

Page 145: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Sending NotificationsMessage payload

{ "aps" : { "alert" : "Jen: Sushi at 10?", "badge" : 1, "sound" : "Jingle.aiff" }, "acme1" : "conversation9964"}

61Tuesday, February 2, 2010

Page 146: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Sending NotificationsMessage payload

• Strict RFC 4627 JSON

{ "aps" : { "alert" : "Jen: Sushi at 10?", "badge" : 1, "sound" : "Jingle.aiff" }, "acme1" : "conversation9964"}

61Tuesday, February 2, 2010

Page 147: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Sending NotificationsMessage payload

• Strict RFC 4627 JSON

• 256 byte maximum

{ "aps" : { "alert" : "Jen: Sushi at 10?", "badge" : 1, "sound" : "Jingle.aiff" }, "acme1" : "conversation9964"}

61Tuesday, February 2, 2010

Page 148: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Message payload

• aps dictionary reserved for the sound, badge or alert keys■ All keys optional

• Rest of payload is for your app

Sending Notifications

{ "aps" : { "alert" : "Jen: Sushi at 10?", "badge" : 1, "sound" : "Jingle.aiff" }, "acme1" : "conversation9964"}

62Tuesday, February 2, 2010

Page 149: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Message payload

• aps dictionary reserved for the sound, badge or alert keys■ All keys optional

• Rest of payload is for your app

Sending Notifications

{ "aps" : { "alert" : "Jen: Sushi at 10?", "badge" : 1, "sound" : "Jingle.aiff" }, "acme1" : "conversation9964"}

62Tuesday, February 2, 2010

Page 150: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Badgesbadge key, integer value

• Positive integer■ Or omit to remove

{ "aps" : { "badge" : 1 }}

63Tuesday, February 2, 2010

Page 151: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Badgesbadge key, integer value

• Positive integer■ Or omit to remove

{ "aps" : { "badge" : 1 }}

63Tuesday, February 2, 2010

Page 152: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Soundssound key, string value

• Either a filename in app bundle■ linear PCM■ MA4■ µLaw■ aLaw

• Or “default”

• Vibration is automatic

{ "aps" : { "sound" : "Jingle.aiff" }}

64Tuesday, February 2, 2010

Page 153: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Alertsalert key, string or dictionary value

• Simplest form is just a string value

• Can be localized (see documentation)

• Can also customize the text on the view button■ or omit it

{ "aps" : { "alert" : "Jen: Sushi at 10?" }}

65Tuesday, February 2, 2010

Page 154: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Alertsalert key, string or dictionary value

• Simplest form is just a string value

• Can be localized (see documentation)

• Can also customize the text on the view button■ or omit it

{ "aps" : { "alert" : "Jen: Sushi at 10?" }}

65Tuesday, February 2, 2010

Page 155: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Send JSON that is stripped of whitespaceSending the Payload

{ "aps" : { "alert" : "Jen: Sushi at 10?", "badge" : 1, "sound" : "Jingle1.aiff" }, "acme1" : "conversation9964"

150 bytes

66Tuesday, February 2, 2010

Page 156: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Send JSON that is stripped of whitespace

96 bytes

{"aps":{"alert":"Jen: Sushi at 10?","badge":1,"sound":"Jingle.aiff"},"acme1":"conversation9964"}

Sending the Payload

66Tuesday, February 2, 2010

Page 157: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Demo:Pushing to the Flickr app

67Tuesday, February 2, 2010

Page 158: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

NSUserDefaults recap(time permitting)

68Tuesday, February 2, 2010

Page 159: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

NSUserDefaults• Convenient way to store settings and lightweight state

■ Arrays, dictionaries, strings, numbers, dates, raw data■ Settings bundles can be created so that user defaults can be set

from Settings app■ Internally stored as property lists

69Tuesday, February 2, 2010

Page 160: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Reading & Writing User Defaults• Key-value store• Base methods accept and return objects for values+ (NSUserDefaults *)standardUserDefaults;

- (id)objectForKey:(NSString *)defaultName;- (void)setObject:(id)value forKey:(NSString *)defaultName;- (void)removeObjectForKey:(NSString *)defaultName;

- (BOOL)synchronize;

70Tuesday, February 2, 2010

Page 161: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Reading & Writing User Defaults• Many convenience methods that ‘box’ and ‘unbox’ the object

■ and perform type checking

- (NSString *)stringForKey:(NSString *)defaultName;- (NSArray *)arrayForKey:(NSString *)defaultName;- (NSDictionary *)dictionaryForKey:(NSString *)defaultName;- (NSData *)dataForKey:(NSString *)defaultName;- (NSArray *)stringArrayForKey:(NSString *)defaultName;- (NSInteger)integerForKey:(NSString *)defaultName;- (float)floatForKey:(NSString *)defaultName;- (double)doubleForKey:(NSString *)defaultName;- (BOOL)boolForKey:(NSString *)defaultName;

- (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName;- (void)setFloat:(float)value forKey:(NSString *)defaultName;- (void)setDouble:(double)value forKey:(NSString *)defaultName;- (void)setBool:(BOOL)value forKey:(NSString *)defaultName;

71Tuesday, February 2, 2010

Page 162: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

-[NSUserDefaults synchronize]• Call [[NSUserDefaults standardUserDefaults] synchronize] to

write changes to disk• Also loads external changes from disk (useful on Mac OS X)

72Tuesday, February 2, 2010

Page 163: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

More on NSUserDefaults• “User Defaults Programming Topics for Cocoa”

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/UserDefaults/Tasks/UsingDefaults.html

73Tuesday, February 2, 2010

Page 164: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Demo:NSUserDefaults and Settings

74Tuesday, February 2, 2010

Page 165: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Recap• Property lists, NSUserDefaults

■ Quick & easy, but limited

• Archived objects■ More flexible, but require writing a lot of code

• SQLite■ Elegant solution for many types of problems

• XML and JSON■ Low-overhead options for talking to “the cloud”■ Apple Push Notification Service pushes JSON from your server to

devices

75Tuesday, February 2, 2010

Page 166: CS193P - Lecture 9 · CS193P - Lecture 9 iPhone Application Development Data in Your iPhone App Chris Marcellino Tuesday, February 2, 2010 1

Questions?

76Tuesday, February 2, 2010