iOS 2 - The practical Stuff

1,549 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,549
On SlideShare
0
From Embeds
0
Number of Embeds
383
Actions
Shares
0
Downloads
16
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

iOS 2 - The practical Stuff

  1. 1. iOS Development Lecture 2 - The practical stuffPetr DvořákPartner & iOS Development Lead@joshis_tweets
  2. 2. Outline
  3. 3. Outline• Using location services and maps • Adding maps to an app • Determining user’s location • Geocoding/Reverse geocoding
  4. 4. Outline• Using system dialogs • Picking a contact • Taking a photo • Composing an e-mail or SMS
  5. 5. Outline• Getting a bit social • Twitter API • Facebook iOS SDK
  6. 6. Outline• Threading • NSThread • GCD (Grand Central Dispatch) • NSOperation
  7. 7. Outline• If there is still time... • Using UIWebView • iOS app localization
  8. 8. Maps & Location
  9. 9. MapKit• Implementation of Google Maps• High-level API• Annotations, Routes, Overlays
  10. 10. MKMapView• UIView subclass• Based on adding “annotations” • = model classes• Support for user’s location• Customizable maps & annotations• Delegate-based API
  11. 11. MKAnnotation• Protocol that enables model class for showing up on maps • coordinate, title, subtitle• MKPlacemark • conforms to MKAnnotation • country, state, city, address
  12. 12. MKAnnotationView• View related to a particular MKAnnotation instance• Reused in the map view• MKPinAnnotationView • The classic “iOS map pin” • Three colors
  13. 13. Core Location• Access to GPS module• Configurable precision (vs. time)• Significant Location Changes• May run on background
  14. 14. CLLocationManager• Provides callbacks for location and heading• Delegate based• Check for availability and state of the location services before using
  15. 15. CLGeocoder• Allows geocoding and reverse geocoding• Does not use Google API • ... doesn’t work perfectly outside the USA• Block based
  16. 16. CLGeocoder* g = [[CLGeocoder alloc] init];[g reverseGeocodeLocation:location completionHandler:^(NSArray *placemark, NSError *error) { // ...}];[g geocodeAddressString:@”Brno” completionHandler: ^(NSArray *placemark, NSError *error) { // ...}];// [g cancelGeocode];
  17. 17. System dialogs
  18. 18. System dialogs• Allow performing usual tasks in a consistent manner• Complete process handling• Delegate based
  19. 19. Address Book• Creating and searching contacts• Allows manual access via C API • ABAddressBookCreate • ABAddressBookCopyArrayOfAllPeople • ABAddressBookSave• Contains predefined dialogs
  20. 20. ABPeoplePickerNavigationController • System dialog for picking a contact • Allows picking a contact or a contact property
  21. 21. 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{ //...}
  22. 22. ABNewPersonViewController• System dialog for creating a contact• May be pre-initializedABPersonViewController• System dialog for displaying a contact• Optional editing
  23. 23. UIImagePickerController• System dialog for picking a photo• Uses “sourceType” flag to determine source • camera, library, saved photos• Check for camera before touching it• Delegate based
  24. 24. MFMailComposeViewController• System dialog for composing an e-mail message• May be pre-filled with e-mail data• Support for attachments• + (BOOL) canSendMail;
  25. 25. MFMessageComposeViewController • System dialog for composing an SMS message • No MMS / attachments • May be pre-filled with message body and recipients (string array) • + (BOOL) canSendText;
  26. 26. Social Networks
  27. 27. 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
  28. 28. TWTweetComposeViewController • System dialog for composing an Tweet • Text, Images, URLs • add methods return NO if length exceeds 140 chars • Block based • TWTweetComposeViewControllerCompletionHandler • + (BOOL) canSendTweet;
  29. 29. Facebook iOS SDK• Facebook publishes official iOS SDK • https://github.com/facebook/facebook-ios-sdk• Requires Facebook application (web)• Uses official Facebook app for authentication • Alternatively: web authentication, embedded FB authentication dialog• Uses URL scheme handling
  30. 30. Threading
  31. 31. Why threads?• Slow operations must not block UI • Network operations • Computations • Filesystem operations
  32. 32. NSThread• Low level thread abstraction• Requires you to do all the synchronisation manually
  33. 33. - (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]; }}
  34. 34. GCD• Working with NSThread is difficult• GCD makes an abstraction above threads• C API, functions/macros starting with “dispatch_” prefix
  35. 35. Dispatch Queue• Queue of blocks to be performed • FIFO, synchronized• Reference-counted • dispatch_queue_create ➞ dispatch_release• One queue per use-case
  36. 36. 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);});
  37. 37. Dispatch Semaphore• dispatch_semaphore_create• dispatch_semaphore_wait• dispatch_semaphore_signal
  38. 38. NSOperation• Abstraction above “operation”• Meant to be subclassed • main - code to be executed • start - start the operation • cancel - set the cancel flag• Operation priority• Dependencies • [op1 addDependency:op2];
  39. 39. NSInvocationOperation• NSOperation subclass• Operation based on target & selector [[NSInvocationOperation alloc] initWithTarget:target selector:selector object:context];
  40. 40. NSBlockOperation• NSOperation subclass• Operation based on block [NSBlockOperation blockOperationWithBlock:^{ // some code... }];
  41. 41. NSOperationQueue• NSOperations are not meant to be run directly• NSOperationQueue runs the operations correctly• Configurable number of concurrent operations
  42. 42. NSOperationQueue NSOperationQueue *queue = [NSOperationQueue mainQueue]; // queue = [NSOperationQueue currentQueue]; // queue = [[NSOperationQueue alloc] init]; [queue addOperation:anOperation];
  43. 43. Singleton pattern• Assures access to a shared instance• Usually allows multiple instance creation• Simple pattern, many problems• Threading
  44. 44. // within class Foo+ (Foo*) getDefault { static Foo *inst = nil; if (!inst) { inst = [[Foo alloc] init]; } return inst;}
  45. 45. // within class Foo+ (Foo*) getDefault { static Foo *inst = nil; @synchronized (self) { if (!inst) { inst = [[Foo alloc] init]; } } return inst;}
  46. 46. // within class Foo+ (Foo*) getDefault { static Foo *inst = nil; if (!inst) { // double-check locking @synchronized (self) { if (!inst) { inst = [[Foo alloc] init]; } } } return inst;}
  47. 47. // 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;}
  48. 48. // within class Foo+ (Foo*) getDefault { static volatile Foo *inst; static dispatch_once_t pred; dispatch_once(&pred, ^ { inst = [[Foo alloc] init]; }); return inst;}
  49. 49. Thank you http://www.inmite.eu/talksPetr DvořákPartner & iOS Development Lead@joshis_tweets

×