ios 2 - the practical stuff

49
iOS Development Lecture 2 - The practical stu Petr Dvořák Partner & iOS Development Lead @joshis_tweets

Upload: petr-dvorak

Post on 14-Jun-2015

1.249 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: iOS 2 - The practical Stuff

iOS DevelopmentLecture 2 - The practical stu!

Petr DvořákPartner & iOS Development Lead@joshis_tweets

Page 2: iOS 2 - The practical Stuff

Outline

Page 3: iOS 2 - The practical Stuff

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

Outline

• Using system dialogs

• Picking a contact

• Taking a photo

• Composing an e-mail or SMS

Page 5: iOS 2 - The practical Stuff

Outline

• Getting a bit social

• Twitter API

• Facebook iOS SDK

Page 6: iOS 2 - The practical Stuff

Outline

• Threading

• NSThread

• GCD (Grand Central Dispatch)

• NSOperation

Page 7: iOS 2 - The practical Stuff

Outline

• If there is still time...

• Using UIWebView

• iOS app localization

Page 8: iOS 2 - The practical Stuff

Maps & Location

Page 9: iOS 2 - The practical Stuff

MapKit

• Implementation of Google Maps

• High-level API

• Annotations, Routes, Overlays

Page 10: iOS 2 - The practical Stuff

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

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

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

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

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

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

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

System dialogs

Page 18: iOS 2 - The practical Stuff

System dialogs

• Allow performing usual tasks in a consistent manner

• Complete process handling

• Delegate based

Page 19: iOS 2 - The practical Stuff

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

ABPeoplePickerNavigationController

• System dialog for picking a contact

• Allows picking a contact or a contact property

Page 21: iOS 2 - The practical Stuff

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

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

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

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

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

Social Networks

Page 27: iOS 2 - The practical Stuff

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

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

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

Threading

Page 31: iOS 2 - The practical Stuff

Why threads?

• Slow operations must not block UI

• Network operations

• Computations

• Filesystem operations

Page 32: iOS 2 - The practical Stuff

NSThread

• Low level thread abstraction

• Requires you to do all the synchronisation manually

Page 33: iOS 2 - The practical Stuff

- (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

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

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

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

Dispatch Semaphore

• dispatch_semaphore_create

• dispatch_semaphore_wait

• dispatch_semaphore_signal

Page 38: iOS 2 - The practical Stuff

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

NSInvocationOperation• NSOperation subclass

• Operation based on target & selector

[[NSInvocationOperation alloc] initWithTarget:target

selector:selector

object:context];

Page 40: iOS 2 - The practical Stuff

NSBlockOperation• NSOperation subclass

• Operation based on block

[NSBlockOperation blockOperationWithBlock:^{

// some code...

}];

Page 41: iOS 2 - The practical Stuff

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

NSOperationQueue

NSOperationQueue *queue = [NSOperationQueue mainQueue];

// queue = [NSOperationQueue currentQueue];

// queue = [[NSOperationQueue alloc] init];

[queue addOperation:anOperation];

Page 43: iOS 2 - The practical Stuff

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

// within class Foo+ (Foo*) getDefault { static Foo *inst = nil; if (!inst) { inst = [[Foo alloc] init]; } return inst;}

Page 45: iOS 2 - The practical Stuff

// 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

// 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

// 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

// 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

Thank youhttp://www.inmite.eu/talks

Petr DvořákPartner & iOS Development Lead@joshis_tweets