what’s new in foundation networking€¦ · what’s new in foundation networking •new...

89
These are confidential sessions—please refrain from streaming, blogging, or taking pictures A session on NSURLSession Session 705 What’s New in Foundation Networking Steve Algernon Senior Wrangler

Upload: others

Post on 09-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

These are confidential sessions—please refrain from streaming, blogging, or taking pictures

A session on NSURLSession

Session 705

What’s New inFoundation Networking

Steve AlgernonSenior Wrangler

Page 2: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

What’s New in Foundation Networking

•New NSURLSession API■ iOS 7, OS X 10.9■ Out-of-process background transfers

• Framework Enhancements■ NSNetServices■ Single sign-on■ iCloud credential syncing

Page 3: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Foundation Networking

WebKit

Safari

MapKit, etc.

Your App

BSD Networking

Foundation

CoreFoundation / CFNetwork

Page 4: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Foundation Networking

NSNetServices

NSURLConnection

NSStream

CFStream

CFNetServices

CFHTTPReadStream

BSD Networking BSD Sockets

Foundation

CoreFoundation / CFNetwork

Page 5: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Foundation Networking

NSNetServices

NSURLConnection

Page 6: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Foundation Networking

NSURLSession

NSURLConnection

Page 7: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLConnection

• Both a technology and a specific class•Originally written for Safari, available via Foundation

■ WWDC 2003 - Session 418

•URL resolution and loading■ file:// http:// https:// data://■ Extensible via NSURLProtocol

•URL Loading machinery, policies■ Configured via NSURLRequest properties■ Shared Persistent Storage: Cache, Credentials, Cookies

•Authentication and Proxies

Page 8: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLConnection

Page 9: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLConnection

NSURLRequest

Page 10: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLConnection

NSURLRequest

Configuration

Page 11: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLConnection

NSURLConnection

Delegate

NSURLRequest

Configuration

Page 12: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLConnection

GlobalStorageObjects

NSURLConnection

Delegate

NSURLRequest

Configuration

Page 13: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLConnection

GlobalStorageObjects

NSData, NSData…

NSURLConnection

Delegate

NSURLResponse

NSURLRequest

Configuration

Page 14: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLConnection

NSURLConnection

Delegate

<head><title>Meh</title></head><body>

GlobalStorageObjects

HTTP/1.1 200 OK

GET /secret.html HTTP/1.1

cellular: NO

Page 15: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Your Networking Needs

NSURLConnection

GET /secret.html HTTP/1.1

HTTP/1.1 200 OK

<head><title>Meh</title></head><body>

NSURLConnection

Delegate GlobalStorageObjects

Page 16: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Your Networking Needs

NSURLConnection

GET /secret.html HTTP/1.1

HTTP/1.1 200 OK

<head><title>Meh</title></head><body>

NSURLConnection

Delegate GlobalStorageObjects

Page 17: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Your Networking Needs

NSURLConnection

GET /secret.html HTTP/1.1

HTTP/1.1 200 OK

<head><title>Meh</title></head><body>

NSURLConnection

Delegate

Other Framework

GET /secret.html HTTP/1.1

HTTP/1.1 200 OK

<head><title>Meh</title></head><body>

NSURLConnection

DelegateGlobalStorageObjects

Page 18: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession

• Both a technology and a specific class• Replaces NSURLConnection

■ Preserves existing concepts and objects■ NSURLRequest, NSURLResponse, etc.

• Configurable Container■ HTTP options■ Subclassable and private storage

• Improved authentication handling■ Connection vs. Request authentication

• Rich delegate model

Page 19: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession

•Uploads/Downloads through the filesystem• Encourages separation of Data from Meta-Data

■ NSURLRequst + payload■ NSURLResponse + payload

•Out-of-process Uploads and Downloads■ Uses same delegate model as in-process transfers■ Optimizes battery life■ Supports UIKit multitasking

Page 20: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession

Page 21: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession

NSURLSession

Page 22: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession

NSURLSession

CacheCookiesCreds

Protocols

OptionsConfiguration

Page 23: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession

NSURLSession

CacheCookiesCreds

Protocols

OptionsConfiguration

Delegate

Page 24: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession

NSURLSession

CacheCookiesCreds

Protocols

OptionsConfiguration

Delegate

HTTP/1.1 200 OK

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

GET /foo.html HTTP/1.1

Page 25: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession

NSURLSession

CacheCookiesCreds

Protocols

Options

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>weeble</title></head><body>

GET /foo.html HTTP/1.1

Page 26: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession

NSURLSession

CacheCookiesCreds

Protocols

Options

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

HTTP/1.1 200 OK

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

GET /foo.html HTTP/1.1

Page 27: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession API

•NSURLSessionConfiguration■ Connection/HTTP policies■ Cache, Credentials, Cookie storage

•NSURLSessionTask■ Unit of “work” for a session

•NSURLSessionDelegate•NSURLSession

■ Created with configuration, optional delegate■ Creates one NSURLSessionTask per request■ Long lived object

Page 28: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession–Adoption

// NSURLConnection example:id<NSURLConnectionDelegate> myDelegate = [[MyDelegate alloc] init];

for (int i = 0; i < 10; i++) {NSURL* myURL = [NSURL URLWithString:@”http://www.apple.com/”];

NSURLRequest* myRequest = [NSURLRequest requestWithURL:myURL];[myRequest setAllowsCellularAccess:NO];

NSURLConnection* conn;conn = [NSURLConnection connectionWithRequest:myRequest delegate:myDelegate];

}

Page 29: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession–Adoption

// NSURLConnection example:id<NSURLConnectionDelegate> myDelegate = [[MyDelegate alloc] init];

for (int i = 0; i < 10; i++) {NSURL* myURL = [NSURL URLWithString:@”http://www.apple.com/”];

NSURLRequest* myRequest = [NSURLRequest requestWithURL:myURL];[myRequest setAllowsCellularAccess:NO];

NSURLConnection* conn;conn = [NSURLConnection connectionWithRequest:myRequest delegate:myDelegate];

}

Page 30: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession–Adoption

// NSURLConnection example:id<NSURLConnectionDelegate> myDelegate = [[MyDelegate alloc] init];

for (int i = 0; i < 10; i++) {NSURL* myURL = [NSURL URLWithString:@”http://www.apple.com/”];

NSURLRequest* myRequest = [NSURLRequest requestWithURL:myURL];[myRequest setAllowsCellularAccess:NO];

NSURLConnection* conn;conn = [NSURLConnection connectionWithRequest:myRequest delegate:myDelegate];

}

Page 31: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession–Adoption

// NSURLConnection example:id<NSURLConnectionDelegate> myDelegate = [[MyDelegate alloc] init];

for (int i = 0; i < 10; i++) {NSURL* myURL = [NSURL URLWithString:@”http://www.apple.com/”];

NSURLRequest* myRequest = [NSURLRequest requestWithURL:myURL];[myRequest setAllowsCellularAccess:NO];

NSURLConnection* conn;conn = [NSURLConnection connectionWithRequest:myRequest delegate:myDelegate];

}

Page 32: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession–Adoption

// NSURLConnection example:id<NSURLConnectionDelegate> myDelegate = [[MyDelegate alloc] init];

for (int i = 0; i < 10; i++) {NSURL* myURL = [NSURL URLWithString:@”http://www.apple.com/”];

NSURLRequest* myRequest = [NSURLRequest requestWithURL:myURL];[myRequest setAllowsCellularAccess:NO];

NSURLConnection* conn;conn = [NSURLConnection connectionWithRequest:myRequest delegate:myDelegate];

}

Page 33: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession–Adoption

// NSURLSession example:id<NSURLSessionDelegate> myDelegate = [[MyDelegate alloc] init];

NSURLSessionConfiguration* myConfiguration = [NSURLSession defaultSessionConfiguration];myConfiguration.allowsCellularAccess = NO;

NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myConfiguration delegate:myDelegate delegateQueue:[NSOperationQueue mainQueue];

for (int i = 0; i < 10; i++) {NSURL* myURL = [NSURL URLWithString:@”http://www.apple.com/”];NSURLSessionDataTask* task;task = [mySession dataTaskWithHTTPGetRequest:myURL];

}

Page 34: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession–Adoption

// NSURLSession example:id<NSURLSessionDelegate> myDelegate = [[MyDelegate alloc] init];

NSURLSessionConfiguration* myConfiguration = [NSURLSession defaultSessionConfiguration];myConfiguration.allowsCellularAccess = NO;

NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myConfiguration delegate:myDelegate delegateQueue:[NSOperationQueue mainQueue];

for (int i = 0; i < 10; i++) {NSURL* myURL = [NSURL URLWithString:@”http://www.apple.com/”];NSURLSessionDataTask* task;task = [mySession dataTaskWithHTTPGetRequest:myURL];

}

Page 35: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession–Adoption

// NSURLSession example:id<NSURLSessionDelegate> myDelegate = [[MyDelegate alloc] init];

NSURLSessionConfiguration* myConfiguration = [NSURLSession defaultSessionConfiguration];myConfiguration.allowsCellularAccess = NO;

NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myConfiguration delegate:myDelegate delegateQueue:[NSOperationQueue mainQueue];

for (int i = 0; i < 10; i++) {NSURL* myURL = [NSURL URLWithString:@”http://www.apple.com/”];NSURLSessionDataTask* task;task = [mySession dataTaskWithHTTPGetRequest:myURL];

}

Page 36: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession–Adoption

// NSURLSession example:id<NSURLSessionDelegate> myDelegate = [[MyDelegate alloc] init];

NSURLSessionConfiguration* myConfiguration = [NSURLSession defaultSessionConfiguration];myConfiguration.allowsCellularAccess = NO;

NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myConfiguration delegate:myDelegate delegateQueue:[NSOperationQueue mainQueue];

for (int i = 0; i < 10; i++) {NSURL* myURL = [NSURL URLWithString:@”http://www.apple.com/”];NSURLSessionDataTask* task;task = [mySession dataTaskWithHTTPGetRequest:myURL];

}

Page 37: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession–Adoption

// NSURLSession example:id<NSURLSessionDelegate> myDelegate = [[MyDelegate alloc] init];

NSURLSessionConfiguration* myConfiguration = [NSURLSession defaultSessionConfiguration];myConfiguration.allowsCellularAccess = NO;

NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myConfiguration delegate:myDelegate delegateQueue:[NSOperationQueue mainQueue];

for (int i = 0; i < 10; i++) {NSURL* myURL = [NSURL URLWithString:@”http://www.apple.com/”];NSURLSessionDataTask* task;task = [mySession dataTaskWithHTTPGetRequest:myURL];

}

Page 38: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession–Adoption

// NSURLSession example:id<NSURLSessionDelegate> myDelegate = [[MyDelegate alloc] init];

NSURLSessionConfiguration* myConfiguration = [NSURLSession defaultSessionConfiguration];myConfiguration.allowsCellularAccess = NO;

NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myConfiguration delegate:myDelegate delegateQueue:[NSOperationQueue mainQueue];

for (int i = 0; i < 10; i++) {NSURL* myURL = [NSURL URLWithString:@”http://www.apple.com/”];NSURLSessionDataTask* task;task = [mySession dataTaskWithHTTPGetRequest:myURL];

}

Page 39: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSessionConfiguration

• Per-session policies■ Cache, Cookies, Credential stores■ Cell usage, network service type■ Number of connections■ Resource and network timeouts■ TLS protocols■ HTTP proxies, cookies, pipelining, headers■ Protocol handlers

• Storage subclasses• Factory constructors for standard configurations

Page 40: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSessionConfiguration

•Default for access to global singleton storage, settings:+(NSURLSessionConfiguration*) defaultConfiguration;

• Private storage, in-memory only storage:+(NSURLSessionConfiguration*) ephemeralSessionConfiguration;

•Out-of-process session configuration, keyed to identifier string+(NSURLSessionConfiguration*) backgroundSessionConfiguration:(NSString*) identifier;

• Configuration objects are mutable, but copied when accessed-(NSURLSessionConfiguration*) copy;

Page 41: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSessionTask

• Replaces NSURLConnection class• Provides status and progress properties• Cancel, Suspend, Resume•Data and Upload tasks provided to differentiate•Download task allows for capturing download state-[NSURLSessionDownloadTask cancelByProducingResumeData:]

•NSURLSessionDelegate methods keyed to task type-URLSession:task:didCompleteWithError:-URLSession:dataTask:didReceiveData:-URLSession:downloadTask:didFinishDownloadingToURL:

Page 42: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSessionTask

Page 43: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSessionTask

NSURLSessionTask

Page 44: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSessionTask

NSURLSessionTask

-cancel-suspend

-resume

Page 45: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSessionTask

NSURLSessionTask

NSURLSessionDataTask

-cancel-suspend

-resume

Page 46: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSessionTask

NSURLSessionUploadTask

NSURLSessionTask

NSURLSessionDataTask

-cancel-suspend

-resume

Page 47: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSessionTask

NSURLSessionUploadTask

NSURLSessionTask

NSURLSessionDataTask

NSURLSessionDownloadTask

-cancelByProducingResumeData

-cancel-suspend

-resume

Page 48: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSessionDelegate

• Single delegate for all NSURLSession messages■ Session, Task, DataTask, DownloadTask

• Strongly referenced until session invalidated•Delegate messages may block loading

■ Invoke the completion handler to continue

Page 49: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Delegate messages for a sessionNSURLSessionDelegate

-URLSession:didReceiveAuthenticationChallenge:completionHandler:-URLSession:didBecomeInvalidWithError:

• For connection level auth■ NTLM■ Server Trust evaluation■ Client Certificate■ Kerberos implicitly handled

Page 50: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Delegate messages for any taskNSURLSessionTaskDelegate

...task:willPerformHTTPRedirection:newRequest:completionHandler:

...task:didReceiveAuthenticationChallenge:completionHandler:■ Request based challenges■ Basic, Digest, Proxies

...task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:

...task:needsNewBodyStream:

■ Not needed if uploading from a file:// or NSData■ May be called multiple times

...task:didCompleteWithError:■ Error will be nil for successful requests

Page 51: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Delegate messages for data tasksNSURLSessionDataDelegate

...dataTask:didReceiveResponse:completionHandler:■ Allows you to turn a DataTask into a DownloadTask

...dataTask:didBecomeDownloadTask:■ No more messages for this data task

...dataTask:didReceiveData:■ Incremental data loading

-[NSData enumerateByteRangesUsingBlock:]...dataTask:willCacheResponse:completionHandler:

■ Default is to attempt to cache

Page 52: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Delegate messages for download tasksNSURLSessionDownloadDelegate

...downloadTask:didFinishDownloadingToURL:■ Open or move the file during the callback

...downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:

...downloadTask:didResumeAtOffset:expectedTotalBytes:■ Resume offset may be less than previous reported totalBytesWritten

Page 53: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession

•Default Session shares NSURLConnection stack[NSURLSession sharedSession].configuration.HTTPCookieStorage == [NSHTTPCookieStorage sharedStorage]

• Custom sessions with private configuration• Invalidation required for your sessions-URLSession:didBecomeInvalidWithError:

• Creates Data, Upload, Download task objects•Asynchronous convenience APIs

■ Can share delegate for auth■ Cancelable

Page 54: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Custom configurationNSURLSession

NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myPrivateSession];NSURL* myPrivateURL = [NSURL URLWithString:@”http://apple.com/secret/”];[mySession dataTaskWithHTTPGetRequest:myPrivateURL completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) { [self gotSecret:data]; }

• Copy or create a configuration• If using the task convenience routines, no delegate required• Private Browsing example:NSURLSessionConfiguration* myConfig = [NSURLSessionConfiguration ephemeralConfiguration];

Page 55: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Custom configurationNSURLSession

NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myPrivateSession];NSURL* myPrivateURL = [NSURL URLWithString:@”http://apple.com/secret/”];[mySession dataTaskWithHTTPGetRequest:myPrivateURL completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) { [self gotSecret:data]; }

• Copy or create a configuration• If using the task convenience routines, no delegate required• Private Browsing example:NSURLSessionConfiguration* myConfig = [NSURLSessionConfiguration ephemeralConfiguration];

Page 56: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Custom configurationNSURLSession

NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myPrivateSession];NSURL* myPrivateURL = [NSURL URLWithString:@”http://apple.com/secret/”];[mySession dataTaskWithHTTPGetRequest:myPrivateURL completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) { [self gotSecret:data]; }

• Copy or create a configuration• If using the task convenience routines, no delegate required• Private Browsing example:NSURLSessionConfiguration* myConfig = [NSURLSessionConfiguration ephemeralConfiguration];

Page 57: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Custom configurationNSURLSession

NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myPrivateSession];NSURL* myPrivateURL = [NSURL URLWithString:@”http://apple.com/secret/”];[mySession dataTaskWithHTTPGetRequest:myPrivateURL completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) { [self gotSecret:data]; }

• Copy or create a configuration• If using the task convenience routines, no delegate required• Private Browsing example:NSURLSessionConfiguration* myConfig = [NSURLSessionConfiguration ephemeralConfiguration];

Page 58: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Custom configurationNSURLSession

NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myPrivateSession];NSURL* myPrivateURL = [NSURL URLWithString:@”http://apple.com/secret/”];[mySession dataTaskWithHTTPGetRequest:myPrivateURL completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) { [self gotSecret:data]; }

• Copy or create a configuration• If using the task convenience routines, no delegate required• Private Browsing example:NSURLSessionConfiguration* myConfig = [NSURLSessionConfiguration ephemeralConfiguration];

Page 59: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Custom configurationNSURLSession

myConfig.allowsCellularAccess = NO;NSURLSession* mySession = [NSURLSession sessionWithConfiguration:myPrivateSession];NSURL* myPrivateURL = [NSURL URLWithString:@”http://apple.com/secret/”];[mySession dataTaskWithHTTPGetRequest:myPrivateURL completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) { [self gotSecret:data]; }

• Copy or create a configuration• If using the task convenience routines, no delegate required• Private Browsing example:NSURLSessionConfiguration* myConfig = [NSURLSessionConfiguration ephemeralConfiguration];

Page 60: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Data Task CreationNSURLSession

•Delegate-based tasks-(NSURLSessionDataTask*) dataTaskWithRequest:(NSURLRequest*) request;-(NSURLSessionDataTask*) dataTaskWithHTTPGetRequest:(NSURL*) url;

•Asynchronous Task Conveniences-(NSURLSessionDataTask*) dataTaskWithRequest:(NSURLRequest*) request completionHandler:(void (^) (NSData* data, NSURLResponse* response, NSError* error) completionHandler;

Page 61: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Upload Task CreationNSURLSession

•Upload tasks-(NSURLSessionUploadTask*) uploadTaskWithRequest:(NSURLRequest*) request fromFile:(NSURL*) file;

-(NSURLSessionUploadTask*) uploadTaskWithRequest:(NSURLRequest*) request fromData:(NSData*) data;

-(NSURLSessionUploadtask*) uploadTaskWithStreamedRequest:(NSURLRequest*) r;

■ Your delegate must implement -needsNewBodyStream:

•Asynchronous Upload Conveniences...:fromFile:completion:^(NSData*, NSURLResponse*, NSError*) completion;...:fromdata:completion:

Page 62: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Download Task CreationNSURLSession

•Download tasks-(NSURLSessionDownloadTask*) downloadTaskWithRequest:(NSURLRequest*) request;-(NSURLSessionDownloadTask*) downloadTaskWithResumeData:(NSData*) data;

•Asynchronous Download Conveniences...downloadTaskWithRequest:completionHandler:^(NSURL* fileURL NSURLResponse* response, NSError* error) completionhandler;...downloadTaskWithResumeData:completionHandler:

• Connection errors produce resume data too[[error userInfo] objectForKey:NSURLSessionDownloadTaskResumeData]

Page 63: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Background TransfersNSURLSession

• Supports upload and download using HTTP(S)• Requires a delegate for event delivery

■ Uses same Upload and Download task delegates as in-process

• Redirections are always taken• -discretionary configuration property

■ Available on iOS, only applies to background transfers■ Optimizes for power and network

Page 64: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Background TransfersNSURLSession

Page 65: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Background TransfersNSURLSession

NSURLSessionIdentifier: “MySession”

Page 66: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Background TransfersNSURLSession

NSURLSessionBackground Daemon

NSURLSessionIdentifier: “MySession”

Page 67: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Background TransfersNSURLSession

NSURLSessionBackground Daemon

NSURLSessionIdentifier: “MySession”

Create Download

Page 68: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Background TransfersNSURLSession

NSURLSessionBackground Daemon

NSURLSessionIdentifier: “MySession”

Progress

Progress

Create Download

Page 69: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSessionIdentifier: “MySession”

Background TransfersNSURLSession

NSURLSessionBackground Daemon

Progress

Progress

Create Download

Page 70: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Background TransfersNSURLSession

NSURLSessionBackground Daemon

Progress

Progress

Create Download

NSURLSessionIdentifier: “MySession”

Page 71: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession

NSURLSessionBackground Daemon

NSURLSessionIdentifier: “MySession”

NSURLSessionIdentifier: “MySession”

Background Transfers

Page 72: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession

NSURLSessionBackground Daemon

NSURLSessionIdentifier: “MySession”

NSURLSessionIdentifier: “MySession”

Background Transfers

Page 73: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession

NSURLSessionBackground Daemon

Relaunched

NSURLSessionIdentifier: “MySession”

Background Transfers

Page 74: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession

NSURLSessionBackground Daemon

Reconnect

Relaunched

NSURLSessionIdentifier: “MySession”

Background Transfers

Page 75: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession

NSURLSessionBackground Daemon

Reconnect

Relaunched

Completed

NSURLSessionIdentifier: “MySession”

Background Transfers

Page 76: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Out-of-process TransfersNSURLSession

•Delegate messages received while you’re running• Your app will be launched in the background…

■ to service auth requests■ when all tasks complete

• Creating a session from same identifier “reconnects" you to existing background session

-(void) getTasksWithCompletionHandler:^(NSArray* dataTasks, NSArray* uploadTasks, NSArray* downloadTasks) completion;

Page 77: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

DemoNSURLSession - Background Requests

Dan VinegradSoftware Engineer

Page 78: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSURLSession vs. NSURLConnection

• Connection based auth schemes•HTTP configuration options• Private, subclassable storage• Background, out-of-process transfers•API baseline

Page 79: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

What’s New in Foundation Networking

•New NSURLSession API■ iOS 7, OS X 10.9■ Out-of-process background transfers

• Framework Enhancements■ NSNetServices■ Single sign-on■ iCloud credential syncing

Page 80: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSNetServices

• Browse for and connect to Bonjour services-(BOOL) getInputStream:(NSInputStream**) inputStreamPtr outputStream:(NSOutputStream**) outputStreamPtr;

•New property: includesPeerToPeer■ Browsing and publishing on Peer to Peer Wi-Fi and Bluetooth■ Peer to Peer Wi-Fi new in iOS 7

Page 81: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

NSNetServices

•New option: NSNetServiceListenForConnections-(void) [NSNetService publishWithOptions:(NSNetServiceOptions) options

• Binds IPv4 and IPv6 listening sockets• Invokes delegate on incoming connections:-(void) netService:(NSNetService*) service didAcceptConnectionWithInputStream:(NSInputStream*) is outputStream:(NSOutputStream*) os;

Page 82: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Single sign-onAuthentication

• Kerberos Authentication•Available in MDM environments•Device Managers specify:

■ Applicable URLs■ Applications

• Kerberos authentication challenges are handled by the system• See “Extending Your Apps for Enterprise and Education Use” session

Page 83: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

iCloud Credential Syncing

• Credentials synced between devices through iCloud• Credential persistence option:NSURLCredentialPersistenceSynchronizable

• Credential storage API:-(void) removeCredential:(NSURLCredential*) credential forProtectionSpace:(NSURLProtectionSpace*) protectionSpace options:(NSDictionary*) options;

Key: NSURLCredentialStorageRemoveSynchronizableCredentialsRemoves a credential across all participating devices

Page 84: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Summary

•NSURLSession■ New API for iOS 7.0 and OS X 10.9■ Replaces NSURLConnection■ Extensive Customization■ Out-of-process background transfers

Page 85: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Summary

•NSNetServices■ Peer to Peer support■ Server creation

•NSURLAuthentication■ Kerberos single sign-on■ iCloud credential syncing

Page 86: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

More Information

Paul DanboldCore OS Technologies [email protected]

DocumentationFoundation Class Referencehttp://developer.apple.com/

Apple Developer Forumshttp://devforums.apple.com

Page 87: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

What’s New in State Restoration MissionThursday 3:15PM

What’s New with Multitasking PresidioTuesday 2:00PM

Nearby Networking with Multipeer Connectivity MissionWednesday 10:15AM

Extending Your Apps for Enterprise and Education Use Nob HillTuesday 3:15PM

Related Sessions

Managing Apple Devices Pacific HeightsTuesday 11:30AM

Page 88: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements

Labs

Multipeer Connectivity Lab Core OS Lab AWednesday 11:30AM

Foundation Networking Lab Core OS Lab BWednesday 10:15AM

Cocoa and Foundation Lab Frameworks Lab AWednesday 11:30AM

Multipeer Connectivity Lab Core OS Lab BFriday 9:00AM

Multitasking Lab Services Lab BThursday 9:00AM

Networking Lab Core OS Lab AThursday 9:00AM

Page 89: What’s New in Foundation Networking€¦ · What’s New in Foundation Networking •New NSURLSession API iOS 7, OS X 10.9 Out-of-process background transfers •Framework Enhancements