09 Data
-
Upload
mahmoud -
Category
Technology
-
view
107 -
download
4
description
Transcript of 09 Data
![Page 1: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/1.jpg)
CS193P - Lecture 9iPhone Application Development
Data in Your iPhone AppChris Marcellino
1Tuesday, February 2, 2010
![Page 2: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/2.jpg)
Today’s Topics• Data in Your iPhone App
■ Saving & loading local data■ Accessing remote data over the Internet
2Tuesday, February 2, 2010
![Page 3: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/3.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/4.jpg)
Property Lists
4Tuesday, February 2, 2010
![Page 5: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/5.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/6.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/7.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/8.jpg)
Writing an Array to Disk
8Tuesday, February 2, 2010
![Page 9: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/9.jpg)
Writing an Array to DiskNSArray *array = [NSArray arrayWithObjects:@“Foo”,
8Tuesday, February 2, 2010
![Page 10: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/10.jpg)
Writing an Array to DiskNSArray *array = [NSArray arrayWithObjects:@“Foo”, [NSNumber numberWithBool:YES],
8Tuesday, February 2, 2010
![Page 11: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/11.jpg)
Writing an Array to DiskNSArray *array = [NSArray arrayWithObjects:@“Foo”, [NSNumber numberWithBool:YES], [NSDate dateWithTimeIntervalSinceNow:60],
8Tuesday, February 2, 2010
![Page 12: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/12.jpg)
Writing an Array to DiskNSArray *array = [NSArray arrayWithObjects:@“Foo”, [NSNumber numberWithBool:YES], [NSDate dateWithTimeIntervalSinceNow:60], nil];
8Tuesday, February 2, 2010
![Page 13: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/13.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/14.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/15.jpg)
Writing a Dictionary to Disk
9Tuesday, February 2, 2010
![Page 16: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/16.jpg)
Writing a Dictionary to DiskNSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
9Tuesday, February 2, 2010
![Page 17: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/17.jpg)
Writing a Dictionary to DiskNSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @“Bob”, @“Name”,
9Tuesday, February 2, 2010
![Page 18: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/18.jpg)
Writing a Dictionary to DiskNSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @“Bob”, @“Name”, [NSNumber numberWithInt:9], @“Lecture”,
9Tuesday, February 2, 2010
![Page 19: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/19.jpg)
Writing a Dictionary to DiskNSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @“Bob”, @“Name”, [NSNumber numberWithInt:9], @“Lecture”, nil];
9Tuesday, February 2, 2010
![Page 20: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/20.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/21.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/22.jpg)
NSPropertyListSerialization• Allows finer-grained control
■ File format■ More descriptive errors■ Mutability
10Tuesday, February 2, 2010
![Page 23: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/23.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/24.jpg)
More on Property Lists• “Property List Programming Guide for Cocoa”
http://developer.apple.com/documentation/Cocoa/Conceptual/PropertyLists/
11Tuesday, February 2, 2010
![Page 25: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/25.jpg)
iPhone’s File System
12Tuesday, February 2, 2010
![Page 26: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/26.jpg)
Keeping Applications Separate
Image (cc) by davidsilver on Flickr
13Tuesday, February 2, 2010
![Page 27: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/27.jpg)
Why Keep Applications Separate?• Security• Privacy• Cleanup after deleting an app
14Tuesday, February 2, 2010
![Page 28: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/28.jpg)
Home Directory Layout
15Tuesday, February 2, 2010
![Page 29: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/29.jpg)
Home Directory Layout• Each app has its own set of directories
15Tuesday, February 2, 2010
![Page 30: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/30.jpg)
Home Directory Layout• Each app has its own set of directories• <Application Home>
15Tuesday, February 2, 2010
![Page 31: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/31.jpg)
Home Directory Layout• Each app has its own set of directories• <Application Home>
■ MyApp.app
15Tuesday, February 2, 2010
![Page 32: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/32.jpg)
Home Directory Layout• Each app has its own set of directories• <Application Home>
■ MyApp.app■ MyApp
15Tuesday, February 2, 2010
![Page 33: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/33.jpg)
Home Directory Layout• Each app has its own set of directories• <Application Home>
■ MyApp.app■ MyApp■ MainWindow.nib
15Tuesday, February 2, 2010
![Page 34: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/34.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/35.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/36.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/37.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/38.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/39.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/40.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/41.jpg)
File Paths in Your Application
16Tuesday, February 2, 2010
![Page 42: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/42.jpg)
File Paths in Your Application// Basic directoriesNSString *homePath = NSHomeDirectory();NSString *tmpPath = NSTemporaryDirectory();
16Tuesday, February 2, 2010
![Page 43: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/43.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/44.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/45.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/46.jpg)
Archiving Objects
18Tuesday, February 2, 2010
![Page 47: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/47.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/48.jpg)
Making Objects Archivable• Conform to the <NSCoding> protocol
20Tuesday, February 2, 2010
![Page 49: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/49.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/50.jpg)
Archiving & Unarchiving Object Graphs
21Tuesday, February 2, 2010
![Page 51: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/51.jpg)
Archiving & Unarchiving Object Graphs• Creating an archive
NSArray *polygons = ...;NSString *path = ...;BOOL result = [NSKeyedArchiver archiveRootObject:polygons toFile:path];
21Tuesday, February 2, 2010
![Page 52: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/52.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/53.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/54.jpg)
The Joy of SQLite
23Tuesday, February 2, 2010
![Page 55: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/55.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/56.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/57.jpg)
SQLite C API Basics
26Tuesday, February 2, 2010
![Page 58: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/58.jpg)
SQLite C API Basics• Open the database
int sqlite3_open(const char *filename, sqlite3 **db);
26Tuesday, February 2, 2010
![Page 59: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/59.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/60.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/61.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/62.jpg)
Demo:Simple SQLite
27Tuesday, February 2, 2010
![Page 63: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/63.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/64.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/65.jpg)
Two classes you should know about...
30Tuesday, February 2, 2010
![Page 66: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/66.jpg)
Two classes you should know about... • NSPredicate
30Tuesday, February 2, 2010
![Page 67: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/67.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/68.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/69.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/70.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/71.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/72.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/73.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/74.jpg)
Two classes you should know about...
31Tuesday, February 2, 2010
![Page 75: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/75.jpg)
Two classes you should know about... • NSEntityDescription
31Tuesday, February 2, 2010
![Page 76: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/76.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/77.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/78.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/79.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/80.jpg)
Web Services
32Tuesday, February 2, 2010
![Page 81: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/81.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/82.jpg)
“I made a location-based user-generated video blogging mashup... for pets!”
34Tuesday, February 2, 2010
![Page 83: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/83.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/84.jpg)
XML
36Tuesday, February 2, 2010
![Page 85: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/85.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/86.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/87.jpg)
JSON
39Tuesday, February 2, 2010
![Page 88: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/88.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/89.jpg)
What does a JSON object look like?
41Tuesday, February 2, 2010
![Page 90: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/90.jpg)
What does a JSON object look like?
{
41Tuesday, February 2, 2010
![Page 91: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/91.jpg)
What does a JSON object look like?
{“instructor” : “Josh Shaffer”,
41Tuesday, February 2, 2010
![Page 92: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/92.jpg)
What does a JSON object look like?
{“instructor” : “Josh Shaffer”,“students” : 60,
41Tuesday, February 2, 2010
![Page 93: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/93.jpg)
What does a JSON object look like?
{“instructor” : “Josh Shaffer”,“students” : 60,“itunes-u” : true,
41Tuesday, February 2, 2010
![Page 94: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/94.jpg)
What does a JSON object look like?
{“instructor” : “Josh Shaffer”,“students” : 60,“itunes-u” : true,“midterm-exam” : null,
41Tuesday, February 2, 2010
![Page 95: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/95.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/96.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/97.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/98.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/99.jpg)
Using json-framework• Reading a JSON string into Foundation objects
42Tuesday, February 2, 2010
![Page 100: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/100.jpg)
Using json-framework• Reading a JSON string into Foundation objects
#import <JSON/JSON.h>
42Tuesday, February 2, 2010
![Page 101: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/101.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/102.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/103.jpg)
Using json-framework• Writing a JSON string from Foundation objects
43Tuesday, February 2, 2010
![Page 104: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/104.jpg)
Using json-framework• Writing a JSON string from Foundation objects
// Create some data in your app
43Tuesday, February 2, 2010
![Page 105: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/105.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/106.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/107.jpg)
Demo:Flickr API with JSON
44Tuesday, February 2, 2010
![Page 108: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/108.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/109.jpg)
Apple Push Notification Service
46Tuesday, February 2, 2010
![Page 110: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/110.jpg)
Overview
47Tuesday, February 2, 2010
![Page 111: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/111.jpg)
• Show badges, alerts and play sounds without app running
Overview
47Tuesday, February 2, 2010
![Page 112: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/112.jpg)
• Show badges, alerts and play sounds without app running
• Minimal server infrastructure needed
Overview
47Tuesday, February 2, 2010
![Page 113: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/113.jpg)
• 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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/114.jpg)
Using the Service
Server
48Tuesday, February 2, 2010
![Page 115: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/115.jpg)
What you needUsing the Service
Server
48Tuesday, February 2, 2010
![Page 116: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/116.jpg)
What you needUsing the Service
Server
48Tuesday, February 2, 2010
![Page 117: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/117.jpg)
What you needUsing the Service
edu.stanford.cs193.app
Server
48Tuesday, February 2, 2010
![Page 118: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/118.jpg)
What you needUsing the Service
edu.stanford.cs193.app
Server
48Tuesday, February 2, 2010
![Page 119: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/119.jpg)
Server
Using the ServiceWhat you do
Apple Push
Service
49Tuesday, February 2, 2010
![Page 120: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/120.jpg)
1. Register with the serviceUsing the Service
Chris’s iPhoneApple Push
Service
50Tuesday, February 2, 2010
![Page 121: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/121.jpg)
Server
Using the Service
Chris’s iPhone
51Tuesday, February 2, 2010
![Page 122: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/122.jpg)
Server
2. Send token to your serverUsing the Service
Chris’s iPhone
51Tuesday, February 2, 2010
![Page 123: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/123.jpg)
Server
3. Send notificationsUsing the Service
Chris’s iPhone
Apple Push
Service
52Tuesday, February 2, 2010
![Page 124: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/124.jpg)
Server
3. Send notificationsUsing the Service
Chris’s iPhone
Apple Push
ServiceJSON
52Tuesday, February 2, 2010
![Page 125: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/125.jpg)
4. Receive notificationsUsing the Service
Apple Push
Service
53Tuesday, February 2, 2010
![Page 126: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/126.jpg)
4. Receive notificationsUsing the Service
Apple Push
ServiceJSON
53Tuesday, February 2, 2010
![Page 127: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/127.jpg)
1. Register with the serviceUsing the Service
Apple Push
ServiceChris’s iPhone
54Tuesday, February 2, 2010
![Page 128: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/128.jpg)
1. Register with the serviceUsing the Service
Apple Push
ServiceChris’s iPhone
54Tuesday, February 2, 2010
![Page 129: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/129.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/130.jpg)
Delegate callbacksRegistering with the Service
56Tuesday, February 2, 2010
![Page 131: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/131.jpg)
Delegate callbacksRegistering with the Service
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token{ // Phone home with device token}
56Tuesday, February 2, 2010
![Page 132: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/132.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/133.jpg)
Registering with the Service
96385da767191121a851963983fdac9bbdf74dcf6219ae14ed8d08228
57Tuesday, February 2, 2010
![Page 134: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/134.jpg)
The device tokenRegistering with the Service
96385da767191121a851963983fdac9bbdf74dcf6219ae14ed8d08228
57Tuesday, February 2, 2010
![Page 135: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/135.jpg)
The device token
• Uniquely identifies device
Registering with the Service
96385da767191121a851963983fdac9bbdf74dcf6219ae14ed8d08228
57Tuesday, February 2, 2010
![Page 136: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/136.jpg)
The device token
• Uniquely identifies device ■ Distinct from -[UIDevice deviceIdentifier]
Registering with the Service
96385da767191121a851963983fdac9bbdf74dcf6219ae14ed8d08228
57Tuesday, February 2, 2010
![Page 137: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/137.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/138.jpg)
• UIApplicationDelegate
Registering for NotificationsOptional callbacks and methods
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
58Tuesday, February 2, 2010
![Page 139: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/139.jpg)
• UIApplication
• UIApplicationDelegate
Registering for NotificationsOptional callbacks and methods
- (UIRemoteNotificationType)enabledRemoteNotificationTypes
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
58Tuesday, February 2, 2010
![Page 140: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/140.jpg)
Chris’s iPhone
Using the Service
Server
59Tuesday, February 2, 2010
![Page 141: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/141.jpg)
Chris’s iPhone
2. Send token to your serverUsing the Service
Server
59Tuesday, February 2, 2010
![Page 142: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/142.jpg)
Server
3. Send notificationsUsing the Service
Chris’s iPhone
Apple Push
Service
60Tuesday, February 2, 2010
![Page 143: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/143.jpg)
Server
3. Send notificationsUsing the Service
Chris’s iPhone
edu.s.cs193
Apple Push
ServiceJSON
60Tuesday, February 2, 2010
![Page 144: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/144.jpg)
Sending Notifications
{ "aps" : { "alert" : "Jen: Sushi at 10?", "badge" : 1, "sound" : "Jingle.aiff" }, "acme1" : "conversation9964"}
61Tuesday, February 2, 2010
![Page 145: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/145.jpg)
Sending NotificationsMessage payload
{ "aps" : { "alert" : "Jen: Sushi at 10?", "badge" : 1, "sound" : "Jingle.aiff" }, "acme1" : "conversation9964"}
61Tuesday, February 2, 2010
![Page 146: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/146.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/147.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/148.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/149.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/150.jpg)
Badgesbadge key, integer value
• Positive integer■ Or omit to remove
{ "aps" : { "badge" : 1 }}
63Tuesday, February 2, 2010
![Page 151: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/151.jpg)
Badgesbadge key, integer value
• Positive integer■ Or omit to remove
{ "aps" : { "badge" : 1 }}
63Tuesday, February 2, 2010
![Page 152: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/152.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/153.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/154.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/155.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/156.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/157.jpg)
Demo:Pushing to the Flickr app
67Tuesday, February 2, 2010
![Page 158: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/158.jpg)
NSUserDefaults recap(time permitting)
68Tuesday, February 2, 2010
![Page 159: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/159.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/160.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/161.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/162.jpg)
-[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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/163.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/164.jpg)
Demo:NSUserDefaults and Settings
74Tuesday, February 2, 2010
![Page 165: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/165.jpg)
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: 09 Data](https://reader033.fdocuments.net/reader033/viewer/2022051613/54c73c284a795927458b45dd/html5/thumbnails/166.jpg)
Questions?
76Tuesday, February 2, 2010