MFF UK - Advanced iOS Topics

1,622 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
1,622
On SlideShare
0
From Embeds
0
Number of Embeds
527
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MFF UK - Advanced iOS Topics

  1. 1. iOS Development Lecture 2 - The practical stuffPetr DvořákPartner & iOS Development Lead@joshis_tweets
  2. 2. Outline
  3. 3. Outline• Network Aware Applications • Connecting to the server • Processing data from the server • Working with UIWebView
  4. 4. Outline• Using system dialogs • Picking a contact • Taking a photo • Composing an e-mail or SMS • Social networks
  5. 5. Outline• Threading • NSThread • GCD (Grand Central Dispatch) • NSOperation
  6. 6. Network Aware Apps
  7. 7. Networking• Three classes to make it work • [NSURL URLWithString:@”http://google.com”]; • [NSURLRequest requestWithUrl:url]; • [NSURLConnection connectionWithRequest:request delegate:self]
  8. 8. Connection callbacks// NSURLConnectionDataDelegate formal protocol since iOS 5// HTTP response- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;// Data chunk received- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;// All done- (void) connectionDidFinishLoading:(NSURLConnection *)conn;// Problem with connection- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
  9. 9. Processing JSON• JSON-Framework • New BSD Licence • JSON parser and generator • http://stig.github.com/json- framework/
  10. 10. Processing XML• TouchXML • Modified BSD Licence • DOM parser + XPath, XML Namespaces • https://github.com/TouchCode/ TouchXML
  11. 11. System dialogs
  12. 12. System dialogs• Allow performing usual tasks in a consistent manner• Complete process handling• Delegate based
  13. 13. MFMailComposeViewController• System dialog for composing an e-mail message• May be pre-filled with e-mail data• Support for attachments• + (BOOL) canSendMail;
  14. 14. MFMessageComposeViewController • System dialog for composing an SMS message • No MMS / attachments • May be pre-filled with message body and recipients (string array) • + (BOOL) canSendText;
  15. 15. 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
  16. 16. Address Book• Creating and searching contacts• Allows manual access via C API • ABAddressBookCreate • ABAddressBookCopyArrayOfAllPeople • ABAddressBookSave• Contains predefined dialogs
  17. 17. ABPeoplePickerNavigationController • System dialog for picking a contact • Allows picking a contact or a contact property
  18. 18. 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{ //...}
  19. 19. ABNewPersonViewController• System dialog for creating a contact• May be pre-initializedABPersonViewController• System dialog for displaying a contact• Optional editing
  20. 20. Twitter API• Native Twitter&Facebook support since iOS 6• SocialFramework• No need to reimplement authentication => • Reasonably secure
  21. 21. Threading
  22. 22. Why threads?• Slow operations must not block UI • Network operations • Computations • Filesystem operations
  23. 23. NSThread• Low level thread abstraction• Requires you to do all the synchronization manually
  24. 24. - (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]; }}
  25. 25. GCD• Working with NSThread is difficult• GCD makes an abstraction above threads• C API, functions/macros starting with “dispatch_” prefix
  26. 26. Dispatch Queue• Queue of blocks to be performed • FIFO, synchronized• Reference-counted • dispatch_queue_create ➞ dispatch_release• One queue per use-case
  27. 27. 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);});
  28. 28. Dispatch Semaphore• dispatch_semaphore_create• dispatch_semaphore_wait• dispatch_semaphore_signal
  29. 29. Dispatch Afterint64_t delayInSeconds = 2.0;dispatch_time_t popTime = dispatch_time( DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);dispatch_after(popTime, dispatch_get_main_queue(), ^{ // executed on the main queue after delay});
  30. 30. Singleton Pattern // within class Foo + (Foo*) getDefault { static dispatch_once_t pred; static Foo *inst; dispatch_once(&pred, ^ { inst = [[Foo alloc] init]; }); return inst; }
  31. 31. 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];
  32. 32. NSInvocationOperation• NSOperation subclass• Operation based on target & selector [[NSInvocationOperation alloc] initWithTarget:target selector:selector object:context];
  33. 33. NSBlockOperation• NSOperation subclass• Operation based on block [NSBlockOperation blockOperationWithBlock:^{ // some code... }];
  34. 34. NSOperationQueue• NSOperations are not meant to be run directly• NSOperationQueue runs the operations correctly• Configurable number of concurrent operations
  35. 35. NSOperationQueue NSOperationQueue *queue = [NSOperationQueue mainQueue]; // queue = [NSOperationQueue currentQueue]; // queue = [[NSOperationQueue alloc] init]; [queue addOperation:anOperation];
  36. 36. Threading summary• UI must run on main thread• Slow task must run on the separate thread• Practically, the most convenient way is using GCD & blocks
  37. 37. Thank you http://www.inmite.eu/talksPetr DvořákPartner & iOS Development Lead@joshis_tweets

×