what's new in multitasking on ios 7 [#pragmaconf 2013]
DESCRIPTION
Le novità di iOS 7 che riguardano il multitasking. Fetiching, notifiche "zitte" e background transferTRANSCRIPT
sabato 26 ottobre 13
Francesco NovelliiOS and Web Developer [email protected]@gmail.com
What’s new? Multitasking API on iOS 7
sabato 26 ottobre 13
Novità per l’utente
‣Nuova grafica‣Nuove impostazioni‣Resta tutto trasparente
sabato 26 ottobre 13
Grafica iOS 6 vs. iOS 7
sabato 26 ottobre 13
Le nuove impostazioni
sabato 26 ottobre 13
Per noi, fan di #pragma mark
sabato 26 ottobre 13
Modalità di Background fino a iOS 6
‣Background Task Completion ‣Background Audio‣Location Services‣VoIP‣Newsstand
sabato 26 ottobre 13
Novità di iOS 7
‣Background Fetch‣Silent Notifications‣Background Transfer Service‣Bluetooth LE
sabato 26 ottobre 13
Impostazioni in Xcode 5
sabato 26 ottobre 13
ATTENZIONE!
Inserire SOLO le opzioni che vengono utilizzate nell’app
Apple boccia le app che hanno opzioni inutilizzate!
sabato 26 ottobre 13
Background Fetch
‣Registrare l’app per richiederlo‣Impostare quanto spesso‣Il resto... ci pensa iOS (più o meno)
sabato 26 ottobre 13
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
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
Quando usarlo
‣Social Networks apps‣News Apps‣Contenuto aggiornato spesso‣Applicazione consultata spesso‣Contenuto di poco peso
sabato 26 ottobre 13
Come implementarlo
Registrare nel plist (o impostazioni app)
UIBackgroundModes: fetch
sabato 26 ottobre 13
Come implementarlo
Impostare il fetching time
[app setMinimumBackgroundFetchInterval: UIApplicationBackgroundFetchIntervalMinimum]
Opzioni:•UIApplicationBackgroundFetchIntervalMinimum•UIApplicationBackgroundFetchIntervalNever (default)
sabato 26 ottobre 13
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
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
Testare il fetching
Attivare nello Schema di Debug
sabato 26 ottobre 13
Testare il fetching
Simulare il background fetch
sabato 26 ottobre 13
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
NeXT...
sabato 26 ottobre 13
Cosa conosciamo e usiamo sempre?- Notifiche Push- Notifiche locali
Il nuovo arrivato?
Silent Notification
sabato 26 ottobre 13
Silent Notification
- Funzionano via web- Certificati per notifiche push- Sono notifiche push! Identiche, ma...
Non “suonano”
invsibili per l’utente
sabato 26 ottobre 13
Silent Notification
- Permettono di “svegliare” l’app- Nuovo metodo dell’AppDelegate- Possibile eseguire codice
sabato 26 ottobre 13
Quando usarlo
- Aggiornamenti sporadici- Aggiornamenti corposi (video, podcast, etc)- Sincronizzare contenuti
sabato 26 ottobre 13
Come implementarlo
Registrare nel plist (o impostazioni app)
UIBackgroundModes: remote-notification
sabato 26 ottobre 13
Come implementarlo
Registrare le notifiche (come per le push):
[application registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
sabato 26 ottobre 13
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
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
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
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
Last but not least...
sabato 26 ottobre 13
Background TransferService
sabato 26 ottobre 13
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
Background Transfer Service
Come implementarlo?
Usando NSURLSession e NSURLSessionDownloadTask
sabato 26 ottobre 13
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
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
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
NSURLSessionDownloadTask
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffsetexpectedTotalBytes:(int64_t)expectedTotalBytes {
}
sabato 26 ottobre 13
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
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
Registrare la notifica
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleBackgroundTransfer:) name:@"BgNotification" object:nil];
sabato 26 ottobre 13
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
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
Background LE
Sessione
“Realizzare accessori iOS con Arduino e Bluetooth 4.0”.
Fiore BasileSala Craig Federighi.14:15
sabato 26 ottobre 13
Novità di iOS 7
‣Background Fetch‣Silent Notifications‣Background Transfer Service
sabato 26 ottobre 13
sabato 26 ottobre 13