iOS 2 - The practical Stuff
-
Upload
petr-dvorak -
Category
Technology
-
view
1.249 -
download
1
Transcript of iOS 2 - The practical Stuff
![Page 1: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/1.jpg)
iOS DevelopmentLecture 2 - The practical stu!
Petr DvořákPartner & iOS Development Lead@joshis_tweets
![Page 2: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/2.jpg)
Outline
![Page 3: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/3.jpg)
Outline
• Using location services and maps
• Adding maps to an app
• Determining user’s location
• Geocoding/Reverse geocoding
![Page 4: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/4.jpg)
Outline
• Using system dialogs
• Picking a contact
• Taking a photo
• Composing an e-mail or SMS
![Page 5: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/5.jpg)
Outline
• Getting a bit social
• Twitter API
• Facebook iOS SDK
![Page 6: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/6.jpg)
Outline
• Threading
• NSThread
• GCD (Grand Central Dispatch)
• NSOperation
![Page 7: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/7.jpg)
Outline
• If there is still time...
• Using UIWebView
• iOS app localization
![Page 8: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/8.jpg)
Maps & Location
![Page 9: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/9.jpg)
MapKit
• Implementation of Google Maps
• High-level API
• Annotations, Routes, Overlays
![Page 10: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/10.jpg)
MKMapView
• UIView subclass
• Based on adding “annotations”
• = model classes
• Support for user’s location
• Customizable maps & annotations
• Delegate-based API
![Page 11: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/11.jpg)
MKAnnotation
• Protocol that enables model class for showing up on maps
• coordinate, title, subtitle
• MKPlacemark
• conforms to MKAnnotation
• country, state, city, address
![Page 12: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/12.jpg)
MKAnnotationView
• View related to a particular MKAnnotation instance
• Reused in the map view
• MKPinAnnotationView
• The classic “iOS map pin”
• Three colors
![Page 13: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/13.jpg)
Core Location
• Access to GPS module
• Con#gurable precision (vs. time)
• Signi#cant Location Changes
• May run on background
![Page 14: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/14.jpg)
CLLocationManager
• Provides callbacks for location and heading
• Delegate based
• Check for availability and state of the location services before using
![Page 15: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/15.jpg)
CLGeocoder
• Allows geocoding and reverse geocoding
• Does not use Google API
• ... doesn’t work perfectly outside the USA
• Block based
![Page 16: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/16.jpg)
CLGeocoder* g = [[CLGeocoder alloc] init];
[g reverseGeocodeLocation:location completionHandler:^(NSArray *placemark, NSError *error) { // ...}];
[g geocodeAddressString:@”Brno” completionHandler: ^(NSArray *placemark, NSError *error) { // ...}];
// [g cancelGeocode];
![Page 17: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/17.jpg)
System dialogs
![Page 18: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/18.jpg)
System dialogs
• Allow performing usual tasks in a consistent manner
• Complete process handling
• Delegate based
![Page 19: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/19.jpg)
Address Book
• Creating and searching contacts
• Allows manual access via C API
• ABAddressBookCreate
• ABAddressBookCopyArrayOfAllPeople
• ABAddressBookSave
• Contains prede#ned dialogs
![Page 20: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/20.jpg)
ABPeoplePickerNavigationController
• System dialog for picking a contact
• Allows picking a contact or a contact property
![Page 21: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/21.jpg)
ABPeoplePickerNavigationController *pp = [[ABPeoplePickerNavigationController alloc] init];pp.peoplePickerDelegate = self;[self presentModalViewController:pp animated:YES];
//...
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)p shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{ //...}
![Page 22: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/22.jpg)
ABNewPersonViewController
• System dialog for creating a contact
• May be pre-initialized
ABPersonViewController
• System dialog for displaying a contact
• Optional editing
![Page 23: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/23.jpg)
UIImagePickerController
• System dialog for picking a photo
• Uses “sourceType” %ag to determine source
• camera, library, saved photos
• Check for camera before touching it
• Delegate based
![Page 24: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/24.jpg)
MFMailComposeViewController
• System dialog for composing ane-mail message
• May be pre-#lled with e-mail data
• Support for attachments
• + (BOOL) canSendMail;
![Page 25: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/25.jpg)
MFMessageComposeViewController
• System dialog for composing anSMS message
• No MMS / attachments
• May be pre-#lled with message body and recipients (string array)
• + (BOOL) canSendText;
![Page 26: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/26.jpg)
Social Networks
![Page 27: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/27.jpg)
Twitter API• Native Twitter support
since iOS 5
• Uses Twitter app for authentication
• Twitter uses OAuth 1.0a under the hood => secure, hard to implement
![Page 28: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/28.jpg)
TWTweetComposeViewController
• System dialog for composing anTweet
• Text, Images, URLs• add methods return NO if length exceeds 140 chars
• Block based• TWTweetComposeViewControllerCompletionHandler
• + (BOOL) canSendTweet;
![Page 29: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/29.jpg)
Facebook iOS SDK
• Facebook publishes o&cial iOS SDK
• https://github.com/facebook/facebook-ios-sdk
• Requires Facebook application (web)
• Uses o&cial Facebook app for authentication
• Alternatively: web authentication, embedded FB authentication dialog
• Uses URL scheme handling
![Page 30: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/30.jpg)
Threading
![Page 31: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/31.jpg)
Why threads?
• Slow operations must not block UI
• Network operations
• Computations
• Filesystem operations
![Page 32: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/32.jpg)
NSThread
• Low level thread abstraction
• Requires you to do all the synchronisation manually
![Page 33: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/33.jpg)
- (void) detachAsyncOperation { [NSThread detachNewThreadSelector:@selector(operation:) toTarget:self withObject:contextData];}
- (void) operation:(id)contextData { @autorelease { // slow code here // ... [self performSelectorOnMainThread:@selector(updateUI:) withObject:contextData waitUntilDone:NO]; }}
![Page 34: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/34.jpg)
GCD
• Working with NSThread is di&cult
• GCD makes an abstraction above threads
• C API, functions/macros starting with “dispatch_” pre#x
![Page 35: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/35.jpg)
Dispatch Queue
• Queue of blocks to be performed
• FIFO, synchronized
• Reference-counted
• dispatch_queue_create ➞ dispatch_release
• One queue per use-case
![Page 36: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/36.jpg)
dispatch_queue_t main_queue = dispatch_get_main_queue()dispatch_queue_t network_queue = dispatch_get_global_queue(priority, 0);// dispatch_queue_t network_queue2 =// dispatch_queue_create("eu.inmite.net", NULL);
dispatch_async(network_queue, ^ { // some slow code dispatch_async(main_queue, ^{ // update UI }); // dispatch_release(network_queue2);});
![Page 37: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/37.jpg)
Dispatch Semaphore
• dispatch_semaphore_create
• dispatch_semaphore_wait
• dispatch_semaphore_signal
![Page 38: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/38.jpg)
NSOperation• Abstraction above “operation”
• Meant to be subclassed• main - code to be executed
• start - start the operation
• cancel - set the cancel %ag
• Operation priority
• Dependencies• [op1 addDependency:op2];
![Page 39: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/39.jpg)
NSInvocationOperation• NSOperation subclass
• Operation based on target & selector
[[NSInvocationOperation alloc] initWithTarget:target
selector:selector
object:context];
![Page 40: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/40.jpg)
NSBlockOperation• NSOperation subclass
• Operation based on block
[NSBlockOperation blockOperationWithBlock:^{
// some code...
}];
![Page 41: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/41.jpg)
NSOperationQueue• NSOperations are not meant to be
run directly
• NSOperationQueue runs the operations correctly
• Con#gurable number of concurrent operations
![Page 42: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/42.jpg)
NSOperationQueue
NSOperationQueue *queue = [NSOperationQueue mainQueue];
// queue = [NSOperationQueue currentQueue];
// queue = [[NSOperationQueue alloc] init];
[queue addOperation:anOperation];
![Page 43: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/43.jpg)
Singleton pattern
• Assures access to a shared instance
• Usually allows multiple instance creation
• Simple pattern, many problems
• Threading
![Page 44: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/44.jpg)
// within class Foo+ (Foo*) getDefault { static Foo *inst = nil; if (!inst) { inst = [[Foo alloc] init]; } return inst;}
![Page 45: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/45.jpg)
// within class Foo+ (Foo*) getDefault { static Foo *inst = nil; @synchronized (self) { if (!inst) { inst = [[Foo alloc] init]; } } return inst;}
![Page 46: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/46.jpg)
// within class Foo+ (Foo*) getDefault { static Foo *inst = nil; if (!inst) { // double-check locking @synchronized (self) { if (!inst) { inst = [[Foo alloc] init]; } } } return inst;}
![Page 47: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/47.jpg)
// within class Foo+ (Foo*) getDefault { static volatile Foo *inst = nil; if (!inst) { // double-check locking @synchronized (self) { if (!inst) { Foo *tmp = [[Foo alloc] init]; OSMemoryBarrier(); inst = tmp; } } } OSMemoryBarrier(); return inst;}
![Page 48: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/48.jpg)
// within class Foo+ (Foo*) getDefault { static volatile Foo *inst; static dispatch_once_t pred; dispatch_once(&pred, ^ { inst = [[Foo alloc] init]; }); return inst;}
![Page 49: iOS 2 - The practical Stuff](https://reader033.fdocuments.net/reader033/viewer/2022052621/557d3677d8b42ac2788b490a/html5/thumbnails/49.jpg)
Thank youhttp://www.inmite.eu/talks
Petr DvořákPartner & iOS Development Lead@joshis_tweets