25.12.2013 Views

URL Loading System Programming Guide - Apple Developer

URL Loading System Programming Guide - Apple Developer

URL Loading System Programming Guide - Apple Developer

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using NS<strong>URL</strong>Session<br />

Creating and Configuring a Session<br />

NSString *cachePath = [NSTemporaryDirectory()<br />

stringByAppendingPathComponent:@"/nsurlsessiondemo.cache"];<br />

#endif<br />

NSLog(@"Cache path: %@\n", cachePath);<br />

NS<strong>URL</strong>Cache *myCache = [[NS<strong>URL</strong>Cache alloc] initWithMemoryCapacity: 16384<br />

diskCapacity: 268435456 diskPath: cachePath];<br />

defaultConfigObject.<strong>URL</strong>Cache = myCache;<br />

defaultConfigObject.requestCachePolicy = NS<strong>URL</strong>RequestUseProtocolCachePolicy;<br />

/* Create a session for each configurations. */<br />

self.defaultSession = [NS<strong>URL</strong>Session sessionWithConfiguration: defaultConfigObject<br />

delegate: self delegateQueue: [NSOperationQueue mainQueue]];<br />

self.backgroundSession = [NS<strong>URL</strong>Session sessionWithConfiguration:<br />

backgroundConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];<br />

self.ephemeralSession = [NS<strong>URL</strong>Session sessionWithConfiguration:<br />

ephemeralConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];<br />

With the exception of background configurations, you can reuse session configuration objects to create<br />

additional sessions. (You cannot reuse background session configurations because the behavior of two<br />

background session objects sharing the same identifier is undefined.)<br />

You can also safely modify the configuration objects at any time. When you create a session, the session<br />

performs a deep copy on the configuration object, so modifications affect only new sessions, not existing<br />

sessions. For example, you might create a second session for content that should be retrieved only if you are<br />

on a Wi-Fi connection as shown in Listing 1-3.<br />

Listing 1-3<br />

Creating a second session with the same configuration object<br />

ephemeralConfigObject.allowsCellularAccess = YES;<br />

// ...<br />

2013-10-22 | Copyright © 2003, 2013 <strong>Apple</strong> Inc. All Rights Reserved.<br />

21

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!