49
sabato 26 ottobre 13

What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Embed Size (px)

DESCRIPTION

Le novità di iOS 7 che riguardano il multitasking. Fetiching, notifiche "zitte" e background transfer

Citation preview

Page 1: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

sabato 26 ottobre 13

Page 2: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Francesco NovelliiOS and Web Developer [email protected]@gmail.com

What’s new? Multitasking API on iOS 7

sabato 26 ottobre 13

Page 3: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Novità per l’utente

‣Nuova grafica‣Nuove impostazioni‣Resta tutto trasparente

sabato 26 ottobre 13

Page 4: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Grafica iOS 6 vs. iOS 7

sabato 26 ottobre 13

Page 5: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Le nuove impostazioni

sabato 26 ottobre 13

Page 6: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Per noi, fan di #pragma mark

sabato 26 ottobre 13

Page 7: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Modalità di Background fino a iOS 6

‣Background Task Completion ‣Background Audio‣Location Services‣VoIP‣Newsstand

sabato 26 ottobre 13

Page 8: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Novità di iOS 7

‣Background Fetch‣Silent Notifications‣Background Transfer Service‣Bluetooth LE

sabato 26 ottobre 13

Page 9: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Impostazioni in Xcode 5

sabato 26 ottobre 13

Page 10: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

ATTENZIONE!

Inserire SOLO le opzioni che vengono utilizzate nell’app

Apple boccia le app che hanno opzioni inutilizzate!

sabato 26 ottobre 13

Page 11: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Background Fetch

‣Registrare l’app per richiederlo‣Impostare quanto spesso‣Il resto... ci pensa iOS (più o meno)

sabato 26 ottobre 13

Page 12: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Background Fetch

iOS registra l’attività degli utenti, capendo quanto e quando un utente usa l’app

Usando tutti i giorni un pattern di apertura app, iOS IMPARA

sabato 26 ottobre 13

Page 13: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Adapts to User Activity

Device observes the pattern

9:15 AM

10:00 AM

1:15 PM

5:00 PM

5:15 PM

1:00 PM

Day n

9:15 AM

10:30 AM

1:00 PM

3:00 PM

5:00 PM

5:15 PM

Day 2

9:15 AM

9:30 AM

10:30 AM

1:00 PM

3:00 PM

5:00 PM

5:15 PM

Day 1

sabato 26 ottobre 13

Page 14: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Quando usarlo

‣Social Networks apps‣News Apps‣Contenuto aggiornato spesso‣Applicazione consultata spesso‣Contenuto di poco peso

sabato 26 ottobre 13

Page 15: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Come implementarlo

Registrare nel plist (o impostazioni app)

UIBackgroundModes: fetch

sabato 26 ottobre 13

Page 16: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Come implementarlo

Impostare il fetching time

[app setMinimumBackgroundFetchInterval: UIApplicationBackgroundFetchIntervalMinimum]

Opzioni:•UIApplicationBackgroundFetchIntervalMinimum•UIApplicationBackgroundFetchIntervalNever (default)

sabato 26 ottobre 13

Page 17: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Come implementarloMetodo del delegate richiamata:

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result)) completionHandler;

UIApplicationDelegate, da implementare (solitamente) nel file AppDelegate.m

sabato 26 ottobre 13

Page 18: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Come implementarlo

Eseguire il codice all’interno del metodo del delegate:

Alla fine dell’esecuzione:

- incrementare l’app badge- richiamare il completition handler

se c’è nuovo contenuto: UIBackgroundFetchResultNewData

se non c’è nuovo contenuto:UIBackgroundFetchResultNoData

se c’è stato errore:UIBackgroundFetchResultFailed

sabato 26 ottobre 13

Page 19: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Testare il fetching

Attivare nello Schema di Debug

sabato 26 ottobre 13

Page 20: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Testare il fetching

Simulare il background fetch

sabato 26 ottobre 13

Page 21: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Consigli

- Non fare affidamento SOLO sul fetch- Scaricare meno dati possibili- Non scaricare video, foto o contenuti multimediali- Ricaricare le view, così da aggiornare l’anteprima- Aggiornare il badge- Usare le Notifiche locali

sabato 26 ottobre 13

Page 22: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

NeXT...

sabato 26 ottobre 13

Page 23: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Cosa conosciamo e usiamo sempre?- Notifiche Push- Notifiche locali

Il nuovo arrivato?

Silent Notification

sabato 26 ottobre 13

Page 24: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Silent Notification

- Funzionano via web- Certificati per notifiche push- Sono notifiche push! Identiche, ma...

Non “suonano”

invsibili per l’utente

sabato 26 ottobre 13

Page 25: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Silent Notification

- Permettono di “svegliare” l’app- Nuovo metodo dell’AppDelegate- Possibile eseguire codice

sabato 26 ottobre 13

Page 26: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Quando usarlo

- Aggiornamenti sporadici- Aggiornamenti corposi (video, podcast, etc)- Sincronizzare contenuti

sabato 26 ottobre 13

Page 27: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Come implementarlo

Registrare nel plist (o impostazioni app)

UIBackgroundModes: remote-notification

sabato 26 ottobre 13

Page 28: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Come implementarlo

Registrare le notifiche (come per le push):

[application registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];

sabato 26 ottobre 13

Page 29: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Come implementarloMetodo del delegate richiamata:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler;

UIApplicationDelegate, da implementare (solitamente) nel file AppDelegate.m

sabato 26 ottobre 13

Page 30: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Come implementarlo

Eseguire il codice all’interno del metodo del delegate:

Alla fine dell’esecuzione:

- incrementare l’app badge- richiamare il completition handler

se c’è nuovo contenuto: UIBackgroundFetchResultNewData

se non c’è nuovo contenuto:UIBackgroundFetchResultNoData

se c’è stato errore:UIBackgroundFetchResultFailed

sabato 26 ottobre 13

Page 31: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Com’è fatta una notificaPayload notifiche push:

{"aps": {"alert": "Ufficiale: Messi al Milan","sound": "default" }}

Payload silent notification:

{"aps": {"content-available": 1 }}

sabato 26 ottobre 13

Page 32: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Consigli

- Non esagerare- Non sempre consegnate appena inviate- Troppe silent notification vengono messe in coda e consegnate insieme- Le notifiche push normali possono avere l’attributo content-available e richiamare il codice

sabato 26 ottobre 13

Page 33: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Last but not least...

sabato 26 ottobre 13

Page 34: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Background TransferService

sabato 26 ottobre 13

Page 35: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Background Transfer Service

- In iOS 6 solo qualche minuto per i download (10 minuti)- Non ha bisogno di autorizzazioni utente- Permette di scaricare grossi file- Notifiche per gestire errori- Tutto gestito da iOS! Trasparente per i dev e gli utenti

sabato 26 ottobre 13

Page 36: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Background Transfer Service

Come implementarlo?

Usando NSURLSession e NSURLSessionDownloadTask

sabato 26 ottobre 13

Page 37: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Background Transfer Service

if (!self.urlSession) { NSString* sessionID = @"ajeje123"; NSURLSessionConfiguration* config = [NSURLSessionConfiguration backgroundSessionConfiguration:sessionID]; self.urlSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]]; } NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.auby.no/files/video_tests/h264_720p_hp_5.1_6mbps_ac3_planet.mp4"]]; self.downloadTask = [self.urlSession downloadTaskWithRequest:request]; [self.downloadTask resume];

sabato 26 ottobre 13

Page 38: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

NSURLSessionDownloadTask

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWrittentotalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { dispatch_async(dispatch_get_main_queue(), ^{ self.downloadProgress.progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite; }); }

sabato 26 ottobre 13

Page 39: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

NSURLSessionDownloadTask- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTaskdidFinishDownloadingToURL:(NSURL *)downloadURL { NSString* lastPathComponent = [downloadTask.originalRequest.URL lastPathComponent]; NSString* destinationPath = [[self docPath] stringByAppendingPathComponent:lastPathComponent]; NSURL* destinationURL = [NSURL fileURLWithPath:destinationPath]; NSError* error; BOOL copySuccessful = [[NSFileManager defaultManager] copyItemAtURL:downloadURL toURL:destinationURL error:&error]; dispatch_async(dispatch_get_main_queue(), ^{ self.downloadProgress.hidden = YES; });}

sabato 26 ottobre 13

Page 40: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

NSURLSessionDownloadTask

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffsetexpectedTotalBytes:(int64_t)expectedTotalBytes {

}

sabato 26 ottobre 13

Page 41: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

NSURLSessionTaskDelegate

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)taskdidCompleteWithError:(NSError *)error { if (error) { NSString* lastPathComponent = [task.originalRequest.URL lastPathComponent]; NSString* filePath = [[self docPath] stringByAppendingPathComponent:lastPathComponent]; [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil]; }}

sabato 26 ottobre 13

Page 42: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Notifiche (AppDelegate)

- (void)application:(UIApplication *)applicationhandleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler { NSDictionary* userInfo = @{@"completionHandler" : completionHandler, @"sessID" : identifier}; [[NSNotificationCenter defaultCenter] postNotificationName:@"BgNotification" object:nil userInfo:userInfo];

}

sabato 26 ottobre 13

Page 43: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Registrare la notifica

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleBackgroundTransfer:) name:@"BgNotification" object:nil];

sabato 26 ottobre 13

Page 44: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Gestire il background

- (void)handleBackgroundTransfer:(NSNotification*)notification { dispatch_async(dispatch_get_main_queue(), ^{ self.downloadProgress.hidden = YES; void(^completionHandler)(void) = notification.userInfo[@"completionHandler"]; if (completionHandler) { completionHandler(); } });}

sabato 26 ottobre 13

Page 45: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Consigli

- Eseguire SOLO i task necessari. Non aggiungere cose inutili- Non eseguire codice di OpenGL ES- Usare il minor spazio di memoria possibile, altrimenti iOS ci potrebbe killare- Salvare lo stato dell’app, in caso fosse chiusa

sabato 26 ottobre 13

Page 46: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Background LE

Sessione

“Realizzare accessori iOS con Arduino e Bluetooth 4.0”.

Fiore BasileSala Craig Federighi.14:15

sabato 26 ottobre 13

Page 47: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Novità di iOS 7

‣Background Fetch‣Silent Notifications‣Background Transfer Service

sabato 26 ottobre 13

Page 48: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

Francesco NovelliiOS and Web Developer [email protected]@gmail.com

Thanks!

sabato 26 ottobre 13

Page 49: What's New in Multitasking on iOS 7 [#PragmaConf 2013]

sabato 26 ottobre 13