186
© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. System Frameworks #WWDC16 Session 711 NSURLSession: New Features and Best Practices JeJenkins Software Engineer, Internet Technologies

NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

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

System Frameworks #WWDC16

Session 711

NSURLSession: New Features and Best Practices

Jeff Jenkins Software Engineer, Internet Technologies

Page 2: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Agenda

Page 3: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Agenda

Evolving NSURLSession API

Page 4: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Agenda

Evolving NSURLSession APISecurity

Page 5: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Agenda

Evolving NSURLSession APISecurityBest practices and tips

Page 6: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Networking API

Page 7: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSStream

NSNetServices

NSURLConnection

Foundation

Networking API

Page 8: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSStream

NSNetServices

NSURLConnection

Foundation

Deprecated2015

Networking API

Page 9: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Foundation NSNetServices

NSStream

NSURLSessionNew2013

NSStream

NSNetServices

NSURLConnection

Foundation

Deprecated2015

Networking API

Page 10: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

ReviewNSURLSession

Page 11: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

BenefitsNSURLSession Example

Page 12: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

BenefitsNSURLSession Example

HTTP/1.1, SPDY, HTTP/2

Page 13: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

BenefitsNSURLSession Example

HTTP/1.1, SPDY, HTTP/2

App Transport Security (ATS)

Page 14: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

BenefitsNSURLSession Example

HTTP/1.1, SPDY, HTTP/2

App Transport Security (ATS)

HTTP Strict Transport Security (HSTS)

Page 15: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

BenefitsNSURLSession Example

HTTP/1.1, SPDY, HTTP/2

App Transport Security (ATS)

HTTP Strict Transport Security (HSTS)

Cache, cookies, proxy, authentication

Page 16: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

BenefitsNSURLSession Example

HTTP/1.1, SPDY, HTTP/2

App Transport Security (ATS)

HTTP Strict Transport Security (HSTS)

Cache, cookies, proxy, authentication

Configuration

Page 17: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

BenefitsNSURLSession Example

NSURLSessionConfiguration

Page 18: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

BenefitsNSURLSession Example

NSURLSessionConfiguration

Transport Layer Security (TLS) version

Page 19: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

BenefitsNSURLSession Example

NSURLSessionConfiguration

Transport Layer Security (TLS) version

Prohibit cellular usage

Page 20: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

BenefitsNSURLSession Example

NSURLSessionConfiguration

Transport Layer Security (TLS) version

Prohibit cellular usage

Network service type

Page 21: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

BenefitsNSURLSession Example

NSURLSessionConfiguration

Transport Layer Security (TLS) version

Prohibit cellular usage

Network service type

Cookie policy

Page 22: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

BenefitsNSURLSession Example

NSURLSessionConfiguration

Transport Layer Security (TLS) version

Prohibit cellular usage

Network service type

Cookie policy

Cache policy

Page 23: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

BenefitsNSURLSession Example

NSURLSessionConfiguration

Transport Layer Security (TLS) version

Prohibit cellular usage

Network service type

Cookie policy

Cache policy

Storage objects

Page 24: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

BenefitsNSURLSession Example

NSURLSessionConfiguration

Transport Layer Security (TLS) version

Prohibit cellular usage

Network service type

Cookie policy

Cache policy

Storage objects

Request and resource timeouts

Page 25: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

ReviewNSURLSession Example

Page 26: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

ReviewNSURLSession Example

let config = NSURLSessionConfiguration.defaultSessionConfiguration()

Page 27: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

ReviewNSURLSession Example

let config = NSURLSessionConfiguration.defaultSessionConfiguration()

let session = NSURLSession(configuration: config)

Page 28: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

ReviewNSURLSession Example

Page 29: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

ReviewNSURLSession Example

let url = NSURL(string: "https://www.example.com/")!

Page 30: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

let task = session.dataTask(with: url) { (data: NSData?, response: NSURLResponse?, error:

NSError?) in

...

}

ReviewNSURLSession Example

let url = NSURL(string: "https://www.example.com/")!

Page 31: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

let task = session.dataTask(with: url) { (data: NSData?, response: NSURLResponse?, error:

NSError?) in

...

}

ReviewNSURLSession Example

let url = NSURL(string: "https://www.example.com/")!

Page 32: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

let task = session.dataTask(with: url) { (data: NSData?, response: NSURLResponse?, error:

NSError?) in

...

}

ReviewNSURLSession Example

let url = NSURL(string: "https://www.example.com/")!

task.resume()

Page 33: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

ReviewNSURLSession Example

Page 34: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

ReviewNSURLSession Example

Summary: Three-step process

Page 35: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

ReviewNSURLSession Example

Summary: Three-step process• Configuration

Page 36: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

ReviewNSURLSession Example

Summary: Three-step process• Configuration• Session

Page 37: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

ReviewNSURLSession Example

Summary: Three-step process• Configuration• Session• Tasks

Page 38: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

ReviewNSURLSession Example

Summary: Three-step process• Configuration• Session• Tasks

Task 1 Session 1

Task 3 Session 3Task 2 Session 2

Page 39: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

ReviewNSURLSession Example

Summary: Three-step process• Configuration• Session• Tasks

Task 1 Session 1

Task 3 Session 3Task 2 Session 2

Task 1Task 2

Task 3

Session 1

Page 40: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

ReviewNSURLSession Example

Summary: Three-step process• Configuration• Session• Tasks

Task 1 Session 1

Task 3 Session 3Task 2 Session 2

Task 1Task 2

Task 3

Session 1

Page 41: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

HTTP/2NSURLSession API

Page 42: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

HTTP/2NSURLSession API NEW

Page 43: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

HTTP/2NSURLSession API NEW

• Apple• Facebook• Instagram• Wikipedia• Twitter• YouTube• Yahoo

Page 44: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

HTTP/2 Protocol

HTTP/2NSURLSession API NEW

• Apple• Facebook• Instagram• Wikipedia• Twitter• YouTube• Yahoo

Page 45: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

HTTP/2 Protocol • Multiplexing and concurrency

HTTP/2NSURLSession API NEW

• Apple• Facebook• Instagram• Wikipedia• Twitter• YouTube• Yahoo

Page 46: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

HTTP/2 Protocol • Multiplexing and concurrency• Header compression

HTTP/2NSURLSession API NEW

• Apple• Facebook• Instagram• Wikipedia• Twitter• YouTube• Yahoo

Page 47: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

HTTP/2 Protocol • Multiplexing and concurrency• Header compression• Stream priorities

HTTP/2NSURLSession API NEW

• Apple• Facebook• Instagram• Wikipedia• Twitter• YouTube• Yahoo

Page 48: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

HTTP/2 Protocol • Multiplexing and concurrency• Header compression• Stream priorities

HTTP/2NSURLSession API

• Server Push

NEW

• Apple• Facebook• Instagram• Wikipedia• Twitter• YouTube• Yahoo

Page 49: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

HTTP/2 Server PushNSURLSession API NEW

Page 50: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

HTTP/2 Server PushNSURLSession API

Prevent network round trips

NEW

Page 51: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

HTTP/2 Server PushNSURLSession API

Prevent network round tripsServer support required

NEW

Page 52: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

HTTP/2 Server PushNSURLSession API

Prevent network round tripsServer support requiredNow available in NSURLSession

NEW

Page 53: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

LoadingHTTP/1.1

App ServerNSURLSessionTi

me

Page 54: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

LoadingHTTP/1.1

App ServerNSURLSessionLatencyTi

me

Page 55: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

LoadingHTTP/1.1

App ServerNSURLSessionLatencyTi

me

Page 56: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

LoadingHTTP/1.1

App Server

GET index.html

NSURLSessionLatencyTi

me

Page 57: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

LoadingHTTP/1.1

App Server

GET index.html

NSURLSession

Response

LatencyTi

me

Page 58: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

LoadingHTTP/1.1

App Server

GET index.html

index.html

NSURLSession

Response

LatencyTi

me

Page 59: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

LoadingHTTP/1.1

App Server

GET index.html

index.html

NSURLSession

GET style.css

Response

LatencyTi

me

Page 60: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

LoadingHTTP/1.1

App Server

GET index.html

index.html

NSURLSession

GET style.css

Response

Response

LatencyTi

me

Page 61: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

LoadingHTTP/1.1

App Server

GET index.html

index.html

style.css

NSURLSession

GET style.css

Response

Response

LatencyTi

me

Page 62: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

LoadingHTTP/1.1

App Server

GET index.html

index.html

style.css

NSURLSession

GET style.css

GET background.jpg

Response

Response

LatencyTi

me

Page 63: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

LoadingHTTP/1.1

App Server

GET index.html

index.html

style.css

NSURLSession

GET style.css

GET background.jpg

Response

Response

Response

LatencyTi

me

Page 64: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

LoadingHTTP/1.1

App Server

GET index.html

index.html

style.css

background.jpg

NSURLSession

GET style.css

GET background.jpg

Response

Response

Response

LatencyTi

me

Page 65: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

LoadingHTTP/1.1

App Server

GET index.html

finish

index.html

style.css

background.jpg

NSURLSession

GET style.css

GET background.jpg

Response

Response

Response

LatencyTi

me

Page 66: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

LoadingHTTP/1.1

App Server

GET index.html

finish

index.html

style.css

background.jpg

HTTP/1.1

NSURLSession

GET style.css

GET background.jpg

Response

Response

Response

LatencyTi

me

Page 67: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App ServerNSURLSession

NEWHTTP/2 Server PushLoading

HTTP/1.1

LatencyTi

me

Page 68: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Server

GET index.html

NSURLSession

NEWHTTP/2 Server PushLoading

HTTP/1.1

LatencyTi

me

Page 69: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Server

GET index.html

Response

NSURLSession

NEWHTTP/2 Server PushLoading

HTTP/1.1

LatencyTi

me

Page 70: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Server

index.html

GET index.html

Response

NSURLSession

NEWHTTP/2 Server PushLoading

HTTP/1.1

LatencyTi

me

Page 71: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Server

index.html

GET index.html

Response

NSURLSession

NEW

PUSH 2, 3, n

HTTP/2 Server PushLoading

HTTP/1.1

LatencyTi

me

Page 72: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Server

GET style.css

index.html

GET index.html

Response

NSURLSession

NEW

PUSH 2, 3, n

HTTP/2 Server PushLoading

HTTP/1.1

LatencyTi

me

Page 73: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Server

ResponseGET style.css

index.html

GET index.html

Response

NSURLSession

NEW

PUSH 2, 3, n

HTTP/2 Server PushLoading

HTTP/1.1

LatencyTi

me

Page 74: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Server

ResponseGET style.css

index.html

style.css

GET index.html

Response

NSURLSession

NEW

PUSH 2, 3, n

HTTP/2 Server PushLoading

HTTP/1.1

LatencyTi

me

Page 75: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Server

ResponseGET style.css

index.html

style.css

GET index.html

Response

GET background.jpg

NSURLSession

NEW

PUSH 2, 3, n

HTTP/2 Server PushLoading

HTTP/1.1

LatencyTi

me

Page 76: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Server

ResponseGET style.css

Response

index.html

style.css

GET index.html

Response

GET background.jpg

NSURLSession

NEW

PUSH 2, 3, n

HTTP/2 Server PushLoading

HTTP/1.1

LatencyTi

me

Page 77: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Server

ResponseGET style.css

Response

index.html

style.cssbackground.jpg

GET index.html

Response

GET background.jpg

NSURLSession

NEW

PUSH 2, 3, n

HTTP/2 Server PushLoading

HTTP/1.1

LatencyTi

me

Page 78: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Server

ResponseGET style.css

Response

index.html

style.cssbackground.jpg

finish

GET index.html

Response

GET background.jpg

NSURLSession

NEW

PUSH 2, 3, n

HTTP/2 Server PushLoading

HTTP/1.1

LatencyTi

me

Page 79: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Server

ResponseGET style.css

Response

index.html

style.cssbackground.jpg

finish

HTTP/2 Server Push

GET index.html

Response

GET background.jpg

NSURLSession

NEW

PUSH 2, 3, n

HTTP/2 Server PushLoading

HTTP/1.1

LatencyTi

me

Page 80: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Server

ResponseGET style.css

Response

index.html

style.cssbackground.jpg

finish

HTTP/2 Server Push

GET index.html

Response

GET background.jpg

NSURLSession

NEW

PUSH 2, 3, n

HTTP/2 Server PushLoading

HTTP/1.1

LatencyTi

me

Page 81: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

DemoHTTP/2 Server Push

Andreas Garkuscha Software Engineer, Internet Technologies

Page 82: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)
Page 83: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)
Page 84: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)
Page 85: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)
Page 86: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

DemoHTTP/2 Server Push

Andreas Garkuscha Software Engineer, Internet Technologies

Page 87: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

SummaryHTTP/2 Server Push

Page 88: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

SummaryHTTP/2 Server Push

Server Push supported in NSURLSession

Page 89: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

SummaryHTTP/2 Server Push

Server Push supported in NSURLSessionNo opt in, just works

Page 90: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

SummaryHTTP/2 Server Push

Server Push supported in NSURLSessionNo opt in, just worksEven better performance

Page 91: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Network statisticsNSURLSession API

Page 92: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Network statisticsNSURLSession API NEW

Page 93: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Network statisticsNSURLSession API

Did you know?

NEW

Page 94: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Network statisticsNSURLSession API

Did you know?98% of all statistics are made up!

NEW

Page 95: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Network statisticsNSURLSession API

Did you know?98% of all statistics are made up!Network statistics: Performance, bugs, understanding

NEW

Page 96: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Network statisticsNSURLSession API

Did you know?98% of all statistics are made up!Network statistics: Performance, bugs, understandingCurrently supported on iOS, macOS, and tvOS

NEW

Page 97: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Network statistics delegateNSURLSession API NEW

Page 98: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Network statistics delegateNSURLSession API

func urlSession(_ session: NSURLSession, task: NSURLSessionTask,

didFinishCollecting metrics: NSURLSessionTaskMetrics)

NEW

Page 99: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskMetricsNSURLSession API NEW

Page 100: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskMetricsNSURLSession API

NSURLSessionTaskMetrics

NEW

Page 101: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskMetricsNSURLSession API

taskInterval: NSDateInterval

NSURLSessionTaskMetrics

NEW

Page 102: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskMetricsNSURLSession API

taskInterval: NSDateInterval

redirectCount: Int

NSURLSessionTaskMetrics

NEW

Page 103: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskMetricsNSURLSession API

taskInterval: NSDateInterval

redirectCount: Int

transactionMetrics: [NSURLSessionTaskTransactionMetrics]

NSURLSessionTaskMetrics

NEW

Page 104: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskMetricsNSURLSession API

taskInterval: NSDateInterval

redirectCount: Int

transactionMetrics: [NSURLSessionTaskTransactionMetrics]

NSURLSessionTaskMetrics

NSURLSessionTaskTransactionMetrics

NEW

Page 105: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Four Categories of Metrics

Page 106: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API NEW

Page 107: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

1 - Request and Response

NEW

Page 108: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

1 - Request and Response

request: NSURLRequest

response: NSURLResponse?

NEW

Page 109: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

1 - Request and Response

request: NSURLRequest

response: NSURLResponse?

…URL loading activity…request response

NEW

Page 110: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API NEW

Page 111: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

2 - Protocol and Connection

NEW

Page 112: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

2 - Protocol and Connection

networkProtocolName: String?

NEW

Page 113: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

2 - Protocol and Connection

networkProtocolName: String?

http/1.1h2spdy/3, spdy/3.1

NEW

Page 114: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

2 - Protocol and Connection

networkProtocolName: String?

isProxyConnection: Bool

http/1.1h2spdy/3, spdy/3.1

NEW

Page 115: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

2 - Protocol and Connection

networkProtocolName: String?

isProxyConnection: Bool

isReusedConnection: Bool

http/1.1h2spdy/3, spdy/3.1

NEW

Page 116: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API NEW

Page 117: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

3 - Load Info

NEW

Page 118: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

3 - Load Info

resourceFetchType: NSURLSessionTaskMetricsResourceFetchType

NEW

Page 119: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

3 - Load Info

resourceFetchType: NSURLSessionTaskMetricsResourceFetchType

.networkLoad

.localCache

.serverPush

NEW

Page 120: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API NEW

Page 121: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

4 - Connection Establishment and Transmission

NEW

Page 122: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

4 - Connection Establishment and Transmission

fetchStartDate: NSDate?

domainLookupStartDate: NSDate?

domainLookupEndDate: NSDate?

connectStartDate: NSDate?

secureConnectionStartDate: NSDate?

secureConnectionEndDate: NSDate?

connectEndDate: NSDate?

NEW

Page 123: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

4 - Connection Establishment and Transmission

fetchStartDate: NSDate?

domainLookupStartDate: NSDate?

domainLookupEndDate: NSDate?

connectStartDate: NSDate?

secureConnectionStartDate: NSDate?

secureConnectionEndDate: NSDate?

connectEndDate: NSDate?

requestStartDate: NSDate?

requestEndDate: NSDate?

responseStartDate: NSDate?

responseEndDate: NSDate?

NEW

Page 124: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API NEW

Page 125: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API NEW

Page 126: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API NEW

Task

fetchStart

Page 127: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Task

fetchStart

NSURLSessionTaskTransactionMetricsNSURLSession API NEW

Page 128: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Task

fetchStart

NSURLSessionTaskTransactionMetricsNSURLSession API NEW

Page 129: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Task

fetchStart

DNS

domainLookupStart

domainLookupEnd

NSURLSessionTaskTransactionMetricsNSURLSession API NEW

Page 130: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Task

fetchStart

DNS

domainLookupStart

domainLookupEnd

NSURLSessionTaskTransactionMetricsNSURLSession API NEW

Page 131: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Task

fetchStart

DNS

domainLookupStart

domainLookupEnd

NSURLSessionTaskTransactionMetricsNSURLSession API NEW

Connection Establishment

connectEndconnectStart

Page 132: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API NEW

Task

fetchStart

DNS

domainLookupStart

domainLookupEnd

Connection Establishment

connectEndconnectStart

Page 133: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API NEW

Task

fetchStart

TCP

DNS

domainLookupStart

domainLookupEnd

Connection Establishment

connectEndconnectStart

Page 134: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Connection Establishment

connectEnd

NSURLSessionTaskTransactionMetricsNSURLSession API NEW

Task

fetchStart TCP

connectStart

DNS

domainLookupStart

domainLookupEnd

Page 135: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

Connection Establishment

connectEnd

NEW

Task

fetchStart TCP TLS

secureConnectionEnd

connectStart

secureConnectionStart

DNS

domainLookupStart

domainLookupEnd

Page 136: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

Connection Establishment

connectEnd

NEW

Task

fetchStart TCP

connectStart

TLS

secureConnectionEndsecureConnectionStart

DNS

domainLookupStart

domainLookupEnd

Page 137: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API NEW

Connection Establishment

connectEnd

Task

fetchStart TCP TLS

secureConnectionEnd

connectStart

secureConnectionStart

DNS

domainLookupStart

domainLookupEnd

Page 138: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

Connection Establishment

connectEnd

NEW

Task

fetchStart

DNS

domainLookupStart

domainLookupEnd

HTTP

TCP TLS

secureConnectionEndsecureConnectionStart

connectStart

Page 139: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

Connection Establishment

connectEnd

NEW

Task

fetchStart

DNS

domainLookupStart

domainLookupEnd

TCP TLS

secureConnectionEnd

HTTPGET

requestStartrequestEnd

secureConnectionStart

connectStart

Page 140: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

Connection Establishment

connectEnd

NEW

Task

fetchStart

DNS

domainLookupStart

domainLookupEnd

TCP TLS

secureConnectionEnd

HTTP

GET

requestStartrequestEnd

secureConnectionStart

connectStart

Page 141: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

Connection Establishment

connectEnd

NEW

Task

fetchStart

DNS

domainLookupStart

domainLookupEnd

TCP TLS

secureConnectionEnd

HTTPGET

requestStartrequestEnd

200 OK

responseStart

responseEnd

secureConnectionStart

connectStart

Page 142: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

Connection Establishment

connectEnd

NEW

Task

fetchStart

DNS

domainLookupStart

domainLookupEnd

TCP TLS

secureConnectionEnd

HTTPGET

requestStartrequestEnd

secureConnectionStart

connectStart

200 OK

responseStart

responseEnd

Page 143: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

Connection Establishment

connectEnd

NEW

Task

fetchStart

DNS

domainLookupStart

domainLookupEnd

TCP TLS

secureConnectionEnd

HTTPGET

requestStartrequestEnd

200 OK

responseStart

responseEnd

secureConnectionStart

connectStart

Page 144: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

Task

fetchStart

HTTPDNS

domainLookupStart

domainLookupEnd

Connection Establishment

connectEnd

200 OK

responseStart

responseEnd

NEW

TCP TLS

secureConnectionStart secureConnectionEnd

GET

requestStartrequestEnd

connectStart

Page 145: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

Task

fetchStart

HTTP

Cache

DNS

domainLookupStart

domainLookupEnd

Connection Establishment

connectEnd

200 OK

responseStart

responseEnd

NEW

TCP TLS

secureConnectionStart secureConnectionEnd

GET

requestStartrequestEnd

connectStart

Page 146: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

Page 147: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

class MySessionDelegate: NSObject, NSURLSessionTaskDelegate { @objc(URLSession:task:didFinishCollectingMetrics:) func urlSession(_ session: NSURLSession, task: NSURLSessionTask, didFinishCollecting metrics: NSURLSessionTaskMetrics) {

//metrics.redirectCount //metrics.taskInterval //metrics.transactionMetrics[0].connectStartDate …

}

Page 148: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

class MySessionDelegate: NSObject, NSURLSessionTaskDelegate { @objc(URLSession:task:didFinishCollectingMetrics:) func urlSession(_ session: NSURLSession, task: NSURLSessionTask, didFinishCollecting metrics: NSURLSessionTaskMetrics) {

//metrics.redirectCount //metrics.taskInterval //metrics.transactionMetrics[0].connectStartDate …

}

let myDelegate = MySessionDelegate()

Page 149: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

class MySessionDelegate: NSObject, NSURLSessionTaskDelegate { @objc(URLSession:task:didFinishCollectingMetrics:) func urlSession(_ session: NSURLSession, task: NSURLSessionTask, didFinishCollecting metrics: NSURLSessionTaskMetrics) {

//metrics.redirectCount //metrics.taskInterval //metrics.transactionMetrics[0].connectStartDate …

}

let myDelegate = MySessionDelegate()

let config = NSURLSessionConfiguration.defaultSessionConfiguration()

Page 150: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

class MySessionDelegate: NSObject, NSURLSessionTaskDelegate { @objc(URLSession:task:didFinishCollectingMetrics:) func urlSession(_ session: NSURLSession, task: NSURLSessionTask, didFinishCollecting metrics: NSURLSessionTaskMetrics) {

//metrics.redirectCount //metrics.taskInterval //metrics.transactionMetrics[0].connectStartDate …

}

let myDelegate = MySessionDelegate()

let config = NSURLSessionConfiguration.defaultSessionConfiguration()

let myDelegateQueue = NSOperationQueue()

Page 151: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

class MySessionDelegate: NSObject, NSURLSessionTaskDelegate { @objc(URLSession:task:didFinishCollectingMetrics:) func urlSession(_ session: NSURLSession, task: NSURLSessionTask, didFinishCollecting metrics: NSURLSessionTaskMetrics) {

//metrics.redirectCount //metrics.taskInterval //metrics.transactionMetrics[0].connectStartDate …

}

let myDelegate = MySessionDelegate()

let config = NSURLSessionConfiguration.defaultSessionConfiguration()

let myDelegateQueue = NSOperationQueue()

let session = NSURLSession(configuration: config, delegate: myDelegate, delegateQueue: myDelegateQueue)

Page 152: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

class MySessionDelegate: NSObject, NSURLSessionTaskDelegate { @objc(URLSession:task:didFinishCollectingMetrics:) func urlSession(_ session: NSURLSession, task: NSURLSessionTask, didFinishCollecting metrics: NSURLSessionTaskMetrics) {

//metrics.redirectCount //metrics.taskInterval //metrics.transactionMetrics[0].connectStartDate …

}

let myDelegate = MySessionDelegate()

let config = NSURLSessionConfiguration.defaultSessionConfiguration()

let myDelegateQueue = NSOperationQueue()

let session = NSURLSession(configuration: config, delegate: myDelegate, delegateQueue: myDelegateQueue)

let url = NSURL(string: "https://www.example.com/")! let task = session.dataTask(with: url) { (data: NSData?, response: NSURLResponse?, error: NSError?) in … }

Page 153: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSessionTaskTransactionMetricsNSURLSession API

class MySessionDelegate: NSObject, NSURLSessionTaskDelegate { @objc(URLSession:task:didFinishCollectingMetrics:) func urlSession(_ session: NSURLSession, task: NSURLSessionTask, didFinishCollecting metrics: NSURLSessionTaskMetrics) {

//metrics.redirectCount //metrics.taskInterval //metrics.transactionMetrics[0].connectStartDate …

}

let myDelegate = MySessionDelegate()

let config = NSURLSessionConfiguration.defaultSessionConfiguration()

let myDelegateQueue = NSOperationQueue()

let session = NSURLSession(configuration: config, delegate: myDelegate, delegateQueue: myDelegateQueue)

let url = NSURL(string: "https://www.example.com/")! let task = session.dataTask(with: url) { (data: NSData?, response: NSURLResponse?, error: NSError?) in … }

task.resume()

Page 154: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

NSURLSession APINetwork statistics

Page 155: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Summary

NSURLSession APINetwork statistics

Page 156: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Summary

Deep view into network loading

NSURLSession APINetwork statistics

Page 157: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Summary

Deep view into network loading

Faster debugging, easier profiling, better performance

NSURLSession APINetwork statistics

Page 158: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Summary

Deep view into network loading

Faster debugging, easier profiling, better performance

Available in NSURLSession

NSURLSession APINetwork statistics

Page 159: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Never an afterthoughtSecurity

Page 160: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

SecurityTransport Layer Security

Page 161: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

SecurityTransport Layer Security

Protects data

Page 162: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

SecurityTransport Layer Security

Protects dataCipher—algorithm for encrypting/decrypting data

Page 163: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

SecurityTransport Layer Security

Protects dataCipher—algorithm for encrypting/decrypting data

The RC4 cipher is no longer supported

Page 164: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Security

• Tuesday—Security session and labs

How iOS Security Really Works Nob Hill Tuesday 4:00PM

What's New in Security Nob Hill Tuesday 5:00PM

Transport Layer Security

Protects dataCipher—algorithm for encrypting/decrypting data

The RC4 cipher is no longer supported

Page 165: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

nscurlSecurity

Page 166: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

nscurlSecurity

$ nscurl https://insecure.example.com/

Page 167: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

nscurlSecurity

$ nscurl https://insecure.example.com/

nscurl[1234:123456] NSURLSession/NSURLConnection HTTP load failed

(kCFStreamErrorDomainSSL, -9824)

Page 168: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

nscurlSecurity

$ nscurl —-enable-rc4 https://insecure.example.com/

Page 169: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

nscurlSecurity

$ nscurl —-enable-rc4 https://insecure.example.com/

Enabling RC4 cipher suites

<html><body><h1>It works!</h1></body></html>

Page 170: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Transport Security

Page 171: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Transport Security

NSAllowsArbitraryLoadsInWebContent

Page 172: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Transport Security

NSAllowsArbitraryLoadsInWebContentNSRequiresCertificateTransparency

Page 173: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

App Transport Security

NSAllowsArbitraryLoadsInWebContentNSRequiresCertificateTransparency

How iOS Security Really Works Nob Hill Tuesday 4:00PM

What's New in Security Nob Hill Tuesday 5:00PM

Page 174: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Summary

Page 175: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Summary

NSURLSession

Page 176: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Summary

• HTTP/2 Server Push

NSURLSession

Page 177: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Summary

• HTTP/2 Server Push Performance!

NSURLSession

Page 178: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Summary

• HTTP/2 Server Push Performance!

• Network statistics

NSURLSession

Page 179: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Summary

• HTTP/2 Server Push Performance!

• Network statistics Faster and better analysis of applications

NSURLSession

Page 180: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Summary

• HTTP/2 Server Push Performance!

• Network statistics• Security

Faster and better analysis of applications

NSURLSession

Page 181: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Summary

• HTTP/2 Server Push Performance!

• Network statistics• Security

Faster and better analysis of applications

Protect user data

NSURLSession

Page 182: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Summary

• HTTP/2 Server Push Performance!

• Network statistics• Security

Faster and better analysis of applications

Protect user data

NSURLSession in your applications!

NSURLSession

Page 183: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

More Information

https://developer.apple.com/wwdc16/711

Page 184: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Related Sessions

What’s New in Foundation for Swift Mission Tuesday 4:00PM

What’s New in Security Nob Hill Tuesday 5:00PM

Engineering Privacy for Your Users Pacific Heights Wednesday 4:00PM

Networking for the Modern Internet Pacific Heights Thursday 3:00PM

Page 185: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)

Related Labs

Networking Lab 1 Frameworks Lab B Thursday 4:00PM

Networking Lab 2 Frameworks Lab D Friday 2:00PM

Page 186: NSURLSession: New Features and Best Practices · Benefits NSURLSession Example HTTP/1.1, SPDY, HTTP/2 App Transport Security (ATS)