what's new in foundation networking · foundation networking session 707 steve algernon senior...

173
© 2014 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. #WWDC14 What's New in Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS

Upload: others

Post on 11-Jul-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

© 2014 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

#WWDC14

What's New in Foundation Networking

Session 707 Steve Algernon Senior Wrangler

Core OS

Page 2: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Introduction

Foundation Networking provides high-level, secure communication APIs and provides the basis for iOS and Mac OS X application networking.

Page 3: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Foundation Networking

WebKit

Safari

MapKit, etc.

Your App

BSD Networking

Foundation

CoreFoundation / CFNetwork

Extension

Page 4: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Foundation Networking

WebKit

Safari

MapKit, etc.

Your App

BSD Networking

Foundation

CoreFoundation / CFNetwork

Extension

Page 5: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Foundation Networking

NSNetServices

NSURLConnection

NSStream

CFStream

CFNetServices

CFHTTPReadStream

BSD Networking BSD Sockets

Foundation

CoreFoundation / CFNetwork

Page 6: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Foundation Networking

NSNetServices

NSStream

CFStream

CFNetServices

CFHTTPReadStream

BSD Networking BSD Sockets

Foundation

CoreFoundation / CFNetwork

NSURLSession

Page 7: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

What You Will Learn

Page 8: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

What You Will Learn

New API • NSNetServies

• NSStream

• NSURLSession

Page 9: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

What You Will Learn

New API • NSNetServies

• NSStream

• NSURLSession

Review of NSURLSession

Page 10: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

What You Will Learn

New API • NSNetServies

• NSStream

• NSURLSession

Review of NSURLSession

New protocol support

Page 11: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

What You Will Learn

New API • NSNetServies

• NSStream

• NSURLSession

Review of NSURLSession

New protocol support

Background sessions and extensions—Best practices

Page 12: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Foundation NetworkingNew API

NSNetServices

@property BOOL includesPeerToPeer NS_AVAILABLE(10_10, 7_0);

Page 13: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Foundation NetworkingNew API

NSStream

+[NSStream getStreamsToHostWithName:(NSString *)host port:(NSInteger) port inputStream:(NSInputStream **) sin outputStream:(NSOutputStream *) sout];

+[NSStream getBoundStreamsWithBufferSize:(NSUInteger)bufferSize inputStream:(NSInputStream **) sin outputStream:(NSOutputStream **) sout];

Page 14: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSession

Cache Cookies Creds

Protocols

Options

Page 15: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSession

NSURLSession

Configuration Cache Cookies Creds

Protocols

Options

Page 16: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSession

NSURLSession

Configuration

Delegate

Cache Cookies Creds

Protocols

Options

Page 17: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSession

NSURLSession

HTTP/1.1 200 OK

<head><title>wobble</title></head><body>

GET /bar.html HTTP/1.1

Configuration

Delegate

HTTP/1.1 200 OK

<head><title>woo</title></head><body>

GET /baz.html HTTP/1.1

Cache Cookies Creds

Protocols

Options

HTTP/1.1 200 OK

<head><title>weeble</title></head><body>

GET /foo.html HTTP/1.1

Page 18: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionConcepts

Page 19: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionConcepts

Session and configuration

Page 20: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionConcepts

Session and configuration

Session tasks

Page 21: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionConcepts

Session and configuration

Session tasks

Session delegate

Page 22: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionConcepts

Session and configuration

Session tasks

Session delegate

Credentials, credential storage

Page 23: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionConcepts

Session and configuration

Session tasks

Session delegate

Credentials, credential storage

Cookies, cookie storage

Page 24: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionConcepts

Session and configuration

Session tasks

Session delegate

Credentials, credential storage

Cookies, cookie storage

Protocols

Page 25: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionConcepts

Session and configuration

Session tasks

Session delegate

Credentials, credential storage

Cookies, cookie storage

Protocols

URL cache

Page 26: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionConfiguration

Page 27: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionConfiguration

NSURLSessionConfiguration object

Page 28: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionConfiguration

NSURLSessionConfiguration object

Properties that affect transfers

TLS levels

Cellular usage

Network service type

Cookie policies

Cache policies

Storage objects

Request & resource timeouts

Page 29: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionConfiguration

Page 30: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionConfiguration

+ [NSURLSessionConfiguration defaultSessionConfiguration]

Best place to start for customization

Modifications only affect this configuration object

Page 31: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionConfiguration

+ [NSURLSessionConfiguration defaultSessionConfiguration]

Best place to start for customization

Modifications only affect this configuration object+ [NSURLSessionConfiguration ephemeralSessionConfiguration]

Does not persist cache, credentials or cookies

Page 32: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionConfiguration

+ [NSURLSessionConfiguration defaultSessionConfiguration]

Best place to start for customization

Modifications only affect this configuration object+ [NSURLSessionConfiguration ephemeralSessionConfiguration]

Does not persist cache, credentials or cookies+ [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:]

Create or reassociate with a background session

Page 33: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionCreation

Page 34: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionCreation

+ [NSURLSession sharedSession]

For delegate-less, simple asynchronous requests

Page 35: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionCreation

+ [NSURLSession sharedSession]

For delegate-less, simple asynchronous requests+ [NSURLSession sessionWithConfiguration:]

Custom configuration, but no delegate

Great to use with ephemeral configuration

Page 36: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionCreation

+ [NSURLSession sharedSession]

For delegate-less, simple asynchronous requests+ [NSURLSession sessionWithConfiguration:]

Custom configuration, but no delegate

Great to use with ephemeral configuration+ [NSURLSession sessionWithConfiguration:delegate:delegateQueue:]

Maximum flexibility through delegates

Required interface for background sessions

delegateQueue may be concurrent

Page 37: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionTasks

Page 38: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionTasks

NSURLSessionTask

-cancel

-suspend

-resume

Page 39: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionTasks

NSURLSessionUploadTask

NSURLSessionTask

NSURLSessionDataTask

-cancel

-suspend

-resume

Page 40: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionTasks

NSURLSessionUploadTask

NSURLSessionTask

NSURLSessionDataTask

NSURLSessionDownloadTask

-cancelByProducingResumeData:

-cancel

-suspend

-resume

Page 41: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionTaskCreation

Page 42: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionTaskCreation

Data task -dataTaskWithURL: -dataTaskWithRequest:

Page 43: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionTaskCreation

Data task -dataTaskWithURL: -dataTaskWithRequest:

Upload task -uploadTaskWithRequest:fromFile: -uploadTaskWithRequest:fromData: -uploadTaskWithStreamedRequest:

Page 44: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionTaskCreation

Download task

-downloadTaskWithURL: -downloadTaskWithRequest: -downloadTaskWithResumeData:

Page 45: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDataTask

NSURLSessionData task

Page 46: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDataTask

NSURLSessionData task

State: Suspended

GET /foo.html HTTP/1.1

Page 47: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDataTask

NSURLSessionData task

State: Suspended

State: Running

GET /foo.html HTTP/1.1

Page 48: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDataTask

NSURLSessionData task

State: Suspended

State: Running

:didReceiveResponse:

HTTP/1.1 200 OK

GET /foo.html HTTP/1.1

Page 49: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDataTask

NSURLSessionData task

State: Suspended

State: Running

:didReceiveData:

:didReceiveResponse:

HTTP/1.1 200 OK

<head><title>weeble</title></head><body>

GET /foo.html HTTP/1.1

Page 50: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDataTask

NSURLSessionData task

State: Suspended

State: Running

:didReceiveData:

:willCacheResponse:

:didReceiveResponse:

HTTP/1.1 200 OK

<head><title>weeble</title></head><body>

GET /foo.html HTTP/1.1

Page 51: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDataTask

NSURLSessionData task

State: Suspended

State: Running

State: Finished

:didReceiveData:

:willCacheResponse:

:didCompleteWithError:

:didReceiveResponse:

HTTP/1.1 200 OK

<head><title>weeble</title></head><body>

GET /foo.html HTTP/1.1

Page 52: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDownloadTask

NSURLSessionDownload task

Page 53: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDownloadTask

NSURLSessionDownload task

State: Suspended

GET /BigFile.tgz

Page 54: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDownloadTask

NSURLSessionDownload task

State: Suspended

State: Running

GET /BigFile.tgz

Page 55: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDownloadTask

NSURLSessionDownload task

State: Suspended

State: Running

:didWriteData:

HTTP/1.1 200 OK

GET /BigFile.tgz

Page 56: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDownloadTask

NSURLSessionDownload task

State: Suspended

State: Running

:didFinishDownloadingToURL:

:didWriteData:

HTTP/1.1 200 OK

file:///Location/BigFile.tgz

GET /BigFile.tgz

Page 57: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDownloadTask

NSURLSessionDownload task

State: Suspended

State: Running

State: Finished

:didFinishDownloadingToURL:

:didCompleteWithError:

:didWriteData:

HTTP/1.1 200 OK

file:///Location/BigFile.tgz

GET /BigFile.tgz

Page 58: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionTaskCreation

NSURLSessionAsynchronousConvenience

Tasks may be canceled

Ignores session delegate—except for authentication challenges

Cannot be used with background sessions -dataTaskWithURL:completionHandler: -downloadTaskWithURL:completionHandler: -uploadTaskWithRequest:fromFile:completionHandler:

Page 59: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionTaskCreation

NSURL *myPrivateURL = [NSURL URLWithString:@“http://example.com/secret”]; NSURLSessionConfiguration* myConfiguration = [NSURLSessionConfiguration ephemeralSessionConfiguration]; NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myConfiguration]; NSURLSessionTask* myTask = [mySession dataTaskWithURL:mySecretURL completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) { [self gotSecret:data]; }]; [myTask resume]

Page 60: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionTaskCreation

NSURL *myPrivateURL = [NSURL URLWithString:@“http://example.com/secret”]; NSURLSessionConfiguration* myConfiguration = [NSURLSessionConfiguration ephemeralSessionConfiguration]; NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myConfiguration]; NSURLSessionTask* myTask = [mySession dataTaskWithURL:mySecretURL completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) { [self gotSecret:data]; }]; [myTask resume]

Page 61: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionTaskCreation

NSURL *myPrivateURL = [NSURL URLWithString:@“http://example.com/secret”]; NSURLSessionConfiguration* myConfiguration = [NSURLSessionConfiguration ephemeralSessionConfiguration]; NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myConfiguration]; NSURLSessionTask* myTask = [mySession dataTaskWithURL:mySecretURL completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) { [self gotSecret:data]; }]; [myTask resume]

Page 62: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionTaskCreation

NSURL *myPrivateURL = [NSURL URLWithString:@“http://example.com/secret”]; NSURLSessionConfiguration* myConfiguration = [NSURLSessionConfiguration ephemeralSessionConfiguration]; NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myConfiguration]; NSURLSessionTask* myTask = [mySession dataTaskWithURL:mySecretURL completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) { [self gotSecret:data]; }]; [myTask resume]

Page 63: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionTaskCreation

NSURL *myPrivateURL = [NSURL URLWithString:@“http://example.com/secret”]; NSURLSessionConfiguration* myConfiguration = [NSURLSessionConfiguration ephemeralSessionConfiguration]; NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myConfiguration]; NSURLSessionTask* myTask = [mySession dataTaskWithURL:mySecretURL completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) { [self gotSecret:data]; }]; [myTask resume]

Page 64: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionTaskCreation

NSURL *myPrivateURL = [NSURL URLWithString:@“http://example.com/secret”]; NSURLSessionConfiguration* myConfiguration = [NSURLSessionConfiguration ephemeralSessionConfiguration]; NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myConfiguration]; NSURLSessionTask* myTask = [mySession dataTaskWithURL:mySecretURL completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) { [self gotSecret:data]; }]; [myTask resume]

Page 65: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionTaskCreation

NSURL *myPrivateURL = [NSURL URLWithString:@“http://example.com/secret”]; NSURLSessionConfiguration* myConfiguration = [NSURLSessionConfiguration ephemeralSessionConfiguration]; NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myConfiguration]; NSURLSessionTask* myTask = [mySession dataTaskWithURL:mySecretURL completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) { [self gotSecret:data]; }]; [myTask resume]

Page 66: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDelegate

Page 67: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDelegate

NSURLSessionDelegate

• Session-related delegate messages

• Connection authentication handling

• Session invalidation/errors

!

NSURLSessionTaskDelegate

• Extends NSURLSessionDelegate

• Request authentication handling

• Task completion/errors

TaskSession

Page 68: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDelegate

NSURLSessionDataDelegate

• Extends NSURLSessionTask protocol

• Delivers bytes as they are transferred

• :didReceiveResponse: disposition

DataTask

Page 69: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDelegate

NSURLSessionDataDelegate

• Extends NSURLSessionTask protocol

• Delivers bytes as they are transferred

• :didReceiveResponse: disposition

Download

DataTask

NSURLSessionDownloadDelegate

• Extends NSURLSessionTask protocol

• Delivers progress during a transfer

• Provides a local file URL of the transferred resource

Page 70: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDelegate queue–Serialized

Task 1

Task 2

Task 3

Task 4

Page 71: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDelegate queue–Serialized

-[delegate URLSession:task:]

Task 1

Task 2

Task 3

Task 4

Page 72: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDelegate queue–Serialized

Task 1

Task 2

Task 3

Task 4

Page 73: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDelegate queue–Concurrency

Task 1

Task 2

Task 3

Task 4

Page 74: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionDelegate queue–Concurrency

Task 1

Task 2

Task 3

Task 4

Page 75: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionNew API

Page 76: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionNew API

Storage objects • NSURLSessionTaskAdditions category

• Provides asynchronous storage access

Page 77: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionNew API

Storage objects • NSURLSessionTaskAdditions category

• Provides asynchronous storage access

NSHTTPCookieStorage - storeCookies:forTask: - getCookiesForTask:completionHandler:

Page 78: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionNew API

Storage objects • NSURLSessionTaskAdditions category

• Provides asynchronous storage access

NSHTTPCookieStorage - storeCookies:forTask: - getCookiesForTask:completionHandler:

NSURLCredentialStorage - getCredential:forProtectionSpace:task:completionHandler:

Page 79: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionNew API

Storage objects • NSURLSessionTaskAdditions category

• Provides asynchronous storage access

NSHTTPCookieStorage - storeCookies:forTask: - getCookiesForTask:completionHandler:

NSURLCredentialStorage - getCredential:forProtectionSpace:task:completionHandler:

NSURLCache - getCachedResponseForDataTask:completionHandler:

Page 80: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

New Protocol Support

Scott Marshall Software Engineer

Page 81: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Faster HTTP—SPDYWhat is SPDY?

SPDY protocol support is now available in NSURLSession on OS X Yosemite and iOS 8 • Available in Safari and for use in your apps

• Leveraged by other Apple frameworks (e.g., UIWebView)

Page 82: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Faster HTTP—SPDYWhat is SPDY?

SPDY protocol support is now available in NSURLSession on OS X Yosemite and iOS 8 • Available in Safari and for use in your apps

• Leveraged by other Apple frameworks (e.g., UIWebView)

SPDY is a protocol that attempts to make the web faster

Page 83: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Faster HTTP—SPDYWhat is SPDY?

SPDY protocol support is now available in NSURLSession on OS X Yosemite and iOS 8 • Available in Safari and for use in your apps

• Leveraged by other Apple frameworks (e.g., UIWebView)

SPDY is a protocol that attempts to make the web faster

Serves as the base for the HTTP/2.0 draft specification

Page 84: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Faster HTTP—SPDYWhat is SPDY?

SPDY protocol support is now available in NSURLSession on OS X Yosemite and iOS 8 • Available in Safari and for use in your apps

• Leveraged by other Apple frameworks (e.g., UIWebView)

SPDY is a protocol that attempts to make the web faster

Serves as the base for the HTTP/2.0 draft specification

Allows exchange of multiple HTTP messages simultaneously (and out-of-order) over a single TCP connection

Page 85: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using SPDY

Page 86: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using SPDY

Available on both OS X Yosemite and iOS 8

Page 87: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using SPDY

Available on both OS X Yosemite and iOS 8

SPDY/2, SPDY/3, and SPDY/3.1 are supported

Page 88: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using SPDY

Available on both OS X Yosemite and iOS 8

SPDY/2, SPDY/3, and SPDY/3.1 are supported

Supported transparently by NSURLSession

Page 89: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using SPDY

Available on both OS X Yosemite and iOS 8

SPDY/2, SPDY/3, and SPDY/3.1 are supported

Supported transparently by NSURLSession

No source changes needed—it just works

Page 90: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using SPDY

Available on both OS X Yosemite and iOS 8

SPDY/2, SPDY/3, and SPDY/3.1 are supported

Supported transparently by NSURLSession

No source changes needed—it just works

!

NSURL *url = [NSURL URLWithString:@“https://www.example.com/"]; NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {...}]; [task resume];

Page 91: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY Benefits

Single, long-lived, TCP connection • Mitigates latency penalty for setting up new connections

• May reduce resource requirements on your server

Page 92: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY Benefits

Single, long-lived, TCP connection • Mitigates latency penalty for setting up new connections

• May reduce resource requirements on your server

Request/response multiplexing: no head-of-line blocking • Head-of-Line Blocking: when a response blocks other responses from being received

• A large response (an image) might be less important than a small response (a CSS or JS file)

Page 93: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY Benefits

Single, long-lived, TCP connection • Mitigates latency penalty for setting up new connections

• May reduce resource requirements on your server

Request/response multiplexing: no head-of-line blocking • Head-of-Line Blocking: when a response blocks other responses from being received

• A large response (an image) might be less important than a small response (a CSS or JS file)

Priorities • The order requests are issued no longer impacts the order responses are received

Page 94: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY BenefitsMultiplexing avoids Head-of-Line Blocking

Time

HTTP/1.1: Head-of-Line Blocking

SPDY: Multiplexing

Priority Legend

Low

Medium

High

Page 95: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY BenefitsMultiplexing avoids Head-of-Line Blocking

Time

HTTP/1.1: Head-of-Line Blocking

SPDY: Multiplexing

Priority Legend

Low

Medium

High

(without Pipelining)

Page 96: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY BenefitsMultiplexing avoids Head-of-Line Blocking

Time

HTTP/1.1: Head-of-Line Blocking

SPDY: Multiplexing

Priority Legend

Low

Medium

High

image.jpg

styles.css

data.xml

(without Pipelining)

Page 97: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY BenefitsMultiplexing avoids Head-of-Line Blocking

Time

HTTP/1.1: Head-of-Line Blocking

SPDY: Multiplexing

Priority Legend

Low

Medium

High

image.jpg

styles.css

data.xml

GET

(without Pipelining)

Page 98: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY BenefitsMultiplexing avoids Head-of-Line Blocking

Time

HTTP/1.1: Head-of-Line Blocking

SPDY: Multiplexing

Priority Legend

Low

Medium

High

image.jpg

styles.css

data.xml

✔GET 200 OK

(without Pipelining)

Page 99: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY BenefitsMultiplexing avoids Head-of-Line Blocking

Time

HTTP/1.1: Head-of-Line Blocking

SPDY: Multiplexing

Priority Legend

Low

Medium

High

image.jpg

styles.css

data.xml

(without Pipelining)

Page 100: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY BenefitsMultiplexing avoids Head-of-Line Blocking

Time

HTTP/1.1: Head-of-Line Blocking

SPDY: Multiplexing

Priority Legend

Low

Medium

High

image.jpg

styles.css

data.xml

(without Pipelining)

Page 101: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY BenefitsMultiplexing avoids Head-of-Line Blocking

Time

HTTP/1.1: Head-of-Line Blocking

SPDY: Multiplexing

Priority Legend

Low

Medium

High

image.jpg

styles.css

data.xml

(with Pipelining)

Page 102: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY BenefitsMultiplexing avoids Head-of-Line Blocking

Time

SPDY: Multiplexing

Priority Legend

Low

Medium

High

image.jpg

styles.css

data.xml

HTTP/1.1: Head-of-Line Blocking (with Pipelining)

Page 103: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY BenefitsMultiplexing avoids Head-of-Line Blocking

Time

SPDY: Multiplexing

Priority Legend

Low

Medium

High

image.jpg

styles.css

data.xml

image.jpg

styles.css

data.xml

HTTP/1.1: Head-of-Line Blocking (with Pipelining)

Page 104: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY BenefitsMultiplexing avoids Head-of-Line Blocking

Time

SPDY: Multiplexing

Priority Legend

Low

Medium

High

image.jpg

styles.css

data.xml

image.jpg

styles.css

data.xml

HTTP/1.1: Head-of-Line Blocking (with Pipelining)

Page 105: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY BenefitsMultiplexing avoids Head-of-Line Blocking

Time

SPDY: Multiplexing

Priority Legend

Low

Medium

High

image.jpg

styles.css

data.xml

image.jpg

styles.css

data.xml

HTTP/1.1: Head-of-Line Blocking (with Pipelining)

Page 106: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY BenefitsMultiplexing avoids Head-of-Line Blocking

Time

SPDY: Multiplexing

Priority Legend

Low

Medium

High

image.jpg

styles.css

data.xml

image.jpg

styles.css

data.xml

HTTP/1.1: Head-of-Line Blocking (with Pipelining)

Page 107: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY BenefitsMultiplexing avoids Head-of-Line Blocking

Time

SPDY: Multiplexing

Priority Legend

Low

Medium

High

image.jpg

styles.css

data.xml

image.jpg

styles.css

data.xml

HTTP/1.1: Head-of-Line Blocking

(with Pipelining)

Page 108: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Why Should I Adopt SPDY?

Can Give Better User Experience • Reduced latency from long-lived connection is conducive to interactive behavior

(especially over cellular)

• Our findings: SPDY is up to 25% faster than HTTP/1.1 in some cases

Page 109: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Why Should I Adopt SPDY?

Can Give Better User Experience • Reduced latency from long-lived connection is conducive to interactive behavior

(especially over cellular)

• Our findings: SPDY is up to 25% faster than HTTP/1.1 in some cases

Additional Benefits • Reduced CPU use (one SSL handshake instead of several)

• May support more clients with same server-side infrastructure

Page 110: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Adopting SPDY

Page 111: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Adopting SPDY

SPDY does require server-side support • Client negotiates with server during TLS handshake

• Uses https:// URLs

• Existing web server software and many CDNs already support SPDY

Page 112: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Adopting SPDY

SPDY does require server-side support • Client negotiates with server during TLS handshake

• Uses https:// URLs

• Existing web server software and many CDNs already support SPDY

Will not interfere with your NSURLProtocol subclasses • Apps might have their own implementation of SPDY or other protocols

Page 113: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY Caveats

SPDY will not always outperform HTTP/1.1 • Parallel TCP connections (used by HTTP/1.1) can be faster than SPDY’s single connection

• Performance benefits are influenced by your workload

Page 114: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY Caveats

SPDY will not always outperform HTTP/1.1 • Parallel TCP connections (used by HTTP/1.1) can be faster than SPDY’s single connection

• Performance benefits are influenced by your workload

SPDY compression of HTTP headers is disabled • Susceptible to CRIME vulnerability

• Disabled by many SPDY implementations

Page 115: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY Caveats

SPDY will not always outperform HTTP/1.1 • Parallel TCP connections (used by HTTP/1.1) can be faster than SPDY’s single connection

• Performance benefits are influenced by your workload

SPDY compression of HTTP headers is disabled • Susceptible to CRIME vulnerability

• Disabled by many SPDY implementations

SPDY is not an IETF-recognized standard, but paves the way for HTTP/2.0

Page 116: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY Best Practices

Page 117: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY Best Practices

Issue multiple requests to let multiplexing handle your workload; you no longer need to take steps to avoid head-of-line blocking

Page 118: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

SPDY Best Practices

Issue multiple requests to let multiplexing handle your workload; you no longer need to take steps to avoid head-of-line blocking

Consolidate server hostnames • Hostname sharding (e.g., css.apple.com, images.apple.com) causes multiple TCP

connections to open

• Using a single hostname (and port!) for all requests allows for optimal connection sharing and re-use

Page 119: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Background Networking

Dan Vinegrad Software Engineer

Page 120: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Background SessionsOverview

Page 121: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Background SessionsOverview

Why use background sessions?

Page 122: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Background SessionsOverview

Why use background sessions?

Background sessions in app extensions

Page 123: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Background SessionsOverview

Why use background sessions?

Background sessions in app extensions

Discretionary networking

Page 124: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Background SessionsOverview

Why use background sessions?

Background sessions in app extensions

Discretionary networking

Using background sessions properly • Handling app launches

• Data tasks

• Pitfalls and best practices

Page 125: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Why Use Background Sessions?

Page 126: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Why Use Background Sessions?

Uploads and downloads continue while app isn’t running • App can be suspended or crash

• App will be woken up to handle auth and completion

Page 127: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Why Use Background Sessions?

Uploads and downloads continue while app isn’t running • App can be suspended or crash

• App will be woken up to handle auth and completion

We monitor the environment for you

Page 128: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Why Use Background Sessions?

Uploads and downloads continue while app isn’t running • App can be suspended or crash

• App will be woken up to handle auth and completion

We monitor the environment for you• Network reachability and connectivity

Page 129: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Why Use Background Sessions?

Uploads and downloads continue while app isn’t running • App can be suspended or crash

• App will be woken up to handle auth and completion

We monitor the environment for you• Network reachability and connectivity

• Automatic retry after network failures

Page 130: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Why Use Background Sessions?

Uploads and downloads continue while app isn’t running • App can be suspended or crash

• App will be woken up to handle auth and completion

We monitor the environment for you• Network reachability and connectivity

• Automatic retry after network failures

• Battery monitoring

Page 131: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Why Use Background Sessions?

Uploads and downloads continue while app isn’t running • App can be suspended or crash

• App will be woken up to handle auth and completion

We monitor the environment for you• Network reachability and connectivity

• Automatic retry after network failures

• Battery monitoring

• Bandwidth monitoring

Page 132: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Background Sessions in App Extensions

Extensions are short-lived processes • In-process networking won’t suffice for large uploads/downloads

• We will wake the containing app to handle events

Background Daemon

Task

Page 133: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Background Sessions in App Extensions

Extensions are short-lived processes • In-process networking won’t suffice for large uploads/downloads

• We will wake the containing app to handle events

Background Daemon

Task

Page 134: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Background Sessions in App Extensions

Extensions are short-lived processes • In-process networking won’t suffice for large uploads/downloads

• We will wake the containing app to handle events

Background Daemon

Task

Page 135: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Background Sessions in App Extensions

Extensions are short-lived processes • In-process networking won’t suffice for large uploads/downloads

• We will wake the containing app to handle events

Background Daemon

Task

Page 136: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Background Sessions in App ExtensionsAdditional constraints

Page 137: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Background Sessions in App ExtensionsAdditional constraints

Must use a shared data container • App and Extension won’t have access to same files

Page 138: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Background Sessions in App ExtensionsAdditional constraints

Must use a shared data container • App and Extension won’t have access to same files

Only one process can be “connected” to the same background session at a time • Can create a new session with a different identifier if another process is already

connected

Page 139: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier: @“com.mycompany.myapp.bgsession”]; !

config.sharedContainerIdentifier = @“com.mycompany.mysharedcontainer”; !

NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

Background Sessions in App ExtensionsSpecifying a shared container

Page 140: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Discretionary Networking

Page 141: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Discretionary Networking

Allows tasks to be scheduled by the system at a “good time” • WiFi vs. cellular

• Battery considerations

Page 142: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Discretionary Networking

Allows tasks to be scheduled by the system at a “good time” • WiFi vs. cellular

• Battery considerations

Takes into account how often app is launched

Page 143: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Discretionary Networking

Tasks treated with more urgency as time goes on • May restrict to WiFi and plugged in to power source at first

• Constraints will relax as timeoutIntervalForResource approaches

Page 144: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Discretionary Networking

Tasks treated with more urgency as time goes on • May restrict to WiFi and plugged in to power source at first

• Constraints will relax as timeoutIntervalForResource approaches

timeoutIntervalForResourceTime

Page 145: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Discretionary Networking

Tasks treated with more urgency as time goes on • May restrict to WiFi and plugged in to power source at first

• Constraints will relax as timeoutIntervalForResource approaches

timeoutIntervalForResourceTime

Page 146: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Discretionary Networking

Page 147: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Discretionary Networking

Opt-in on NSURLSessionConfiguration: config.discretionary = YES

Page 148: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Discretionary Networking

Opt-in on NSURLSessionConfiguration: config.discretionary = YES

Non-user-initiated tasks • Pre-fetching the next episode

• Upload syncing

Page 149: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Discretionary Networking

Opt-in on NSURLSessionConfiguration: config.discretionary = YES

Non-user-initiated tasks • Pre-fetching the next episode

• Upload syncing

Tasks created while app is running in the background are automatically discretionary • Work performed during background fetch/push is not user-initiated

• Tasks become non-discretionary when user brings the app to the foreground

Page 150: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using Background SessionsHandling app launching

Page 151: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using Background SessionsHandling app launching

UIApplicationDelegate method: -application:handleEventsForBackgroundURLSession:completionHandler:

Page 152: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using Background SessionsHandling app launching

UIApplicationDelegate method: -application:handleEventsForBackgroundURLSession:completionHandler:

Reconnect to the background session • Create background session with the provided identifier

• Receive delegate messages

• Call the completionHandler when finished handling the events

Page 153: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using Background SessionsHandling app launching

UIApplicationDelegate method: -application:handleEventsForBackgroundURLSession:completionHandler:

Reconnect to the background session • Create background session with the provided identifier

• Receive delegate messages

• Call the completionHandler when finished handling the events

-URLSessionDidFinishEventsForBackgroundURLSession:

Page 154: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using Background SessionsData tasks

Page 155: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using Background SessionsData tasks

Will only run while app is running • Will fail with NSURLErrorBackgroundSessionWasDisconnected when app is suspended

or exits

Page 156: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using Background SessionsData tasks

Will only run while app is running • Will fail with NSURLErrorBackgroundSessionWasDisconnected when app is suspended

or exits

Can convert to download task when response is received • Will continue after app is suspended - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler; NSURLSessionResponseBecomeDownload

Page 157: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using Background Sessions Things to avoid

Page 158: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using Background Sessions Things to avoid

Creating one task at a time • Tasks created in background will be discretionary

• The system will prevent your app from being launched too frequently

Page 159: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using Background Sessions Things to avoid

Creating one task at a time • Tasks created in background will be discretionary

• The system will prevent your app from being launched too frequently

Downloading lots of small assets • Much more efficient to download one large, zipped asset

Page 160: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using Background Sessions Things to avoid

Creating one task at a time • Tasks created in background will be discretionary

• The system will prevent your app from being launched too frequently

Downloading lots of small assets • Much more efficient to download one large, zipped asset

Blocking while waiting for transfers to complete

Page 161: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using Background SessionsBest practices

Page 162: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using Background SessionsBest practices

Can still use in-process networking while running in the background • Smaller, time-sensitive assets

• Larger uploads/downloads should use background sessions

Page 163: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using Background SessionsBest practices

Can still use in-process networking while running in the background • Smaller, time-sensitive assets

• Larger uploads/downloads should use background sessions

Support resumable downloads (Range GET requests)

Page 164: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Using Background SessionsBest practices

Can still use in-process networking while running in the background • Smaller, time-sensitive assets

• Larger uploads/downloads should use background sessions

Support resumable downloads (Range GET requests)

Handle launch events properly • Reconnect to your background session when we launch your app

• Call the completion handler

Page 165: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Summary

Page 166: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Summary

New APIs on NSStream and NSNetService

Page 167: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Summary

New APIs on NSStream and NSNetService

Using NSURLSession

Page 168: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Summary

New APIs on NSStream and NSNetService

Using NSURLSession

New protocol support

Page 169: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Summary

New APIs on NSStream and NSNetService

Using NSURLSession

New protocol support

Background networking

Page 170: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

More Information

Paul Danbold Core OS Technologies Evangelist [email protected]

Documentation NSURLSession Class Reference https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/Introduction/Introduction.html

Apple Developer Forums http://devforums.apple.com

Page 171: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Related Sessions

• Creating Extensions for iOS and OS X, Part 1 Mission Tuesday 2:00PM

• Creating Extensions for iOS and OS X, Part 2 Mission Wednesday 11:30AM

• Cross Platform Nearby Networking Nob Hill Wednesday 9:00AM

• Fix Bugs Faster Using Activity Tracing Russian Hill Thursday 11:30AM

• Power, Performance, and Diagnostics: What’s New in GCD and XPC

Russian Hill Thursday 2:00PM

Page 172: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication

Labs

• Networking Lab Core OS Lab A Tuesday 4:30PM

• Networking Lab Core OS Lab B Wednesday 9:00AM

• Multipeer Connectivity Lab Core OS Lab A Wednesday 10:15AM

• Multipeer Connectivity Lab Core OS Lab B Friday 9:00AM

• Extensions Lab Frameworks Lab A Tuesday 3:15PM

• Extensions Lab Frameworks Lab B Thursday 2:00PM

Page 173: What's New in Foundation Networking · Foundation Networking Session 707 Steve Algernon Senior Wrangler Core OS. Introduction Foundation Networking provides high-level, secure communication