Skip to content

Commit 71b0aa9

Browse files
committed
starting to revise chapter 24 examples
1 parent e1c2bbf commit 71b0aa9

File tree

4 files changed

+59
-40
lines changed

4 files changed

+59
-40
lines changed

bk2ch24p834simpleHTTP/ch37p1088simpleHTTP/ViewController.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,31 @@ class ViewController: UIViewController {
88

99
@IBAction func doSimpleHTTP (_ sender: Any!) {
1010
self.iv.image = nil
11-
let s = "http://www.apeth.net/matt/images/phoenixnewest.jpg"
11+
let s = "https://www.apeth.net/matt/images/phoenixnewest.jpg"
1212
let url = URL(string:s)!
13-
let session = URLSession.shared()
14-
let task = session.downloadTask(with:url) {
15-
(loc:URL?, response:URLResponse?, error:NSError?) in
13+
let session = URLSession.shared
14+
let task = session.downloadTask(with:url) { loc, resp, err in
1615
print("here")
17-
if error != nil {
18-
print(error)
16+
guard err == nil else {
17+
print(err)
1918
return
2019
}
21-
let status = (response as! HTTPURLResponse).statusCode
20+
let status = (resp as! HTTPURLResponse).statusCode
2221
print("response status: \(status)")
23-
if status != 200 {
24-
print("oh well")
22+
guard status == 200 else {
23+
print(status)
2524
return
2625
}
27-
if let d = try? Data(contentsOf:loc!) {
26+
if let loc = loc, let d = try? Data(contentsOf:loc) {
2827
let im = UIImage(data:d)
2928
DispatchQueue.main.async {
3029
self.iv.image = im
3130
print("done")
3231
}
3332
}
3433
}
34+
// just demonstrating syntax
35+
task.priority = URLSessionTask.defaultPriority
3536
task.resume()
3637
}
3738
}

bk2ch24p837lessSimpleHTTP/ch37p1089lessSimpleHTTP/ViewController.swift

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ViewController: UIViewController, URLSessionDownloadDelegate {
77
@IBOutlet var iv : UIImageView!
88
var task : URLSessionTask!
99

10-
let which = 1 // 0 or 1
10+
let which = 0 // 0 or 1
1111

1212
lazy var session : URLSession = {
1313
let config = URLSessionConfiguration.ephemeral
@@ -23,16 +23,26 @@ class ViewController: UIViewController, URLSessionDownloadDelegate {
2323
return
2424
}
2525

26-
let s = "http://www.apeth.net/matt/images/phoenixnewest.jpg"
26+
let s = "https://www.apeth.net/matt/images/phoenixnewest.jpg"
2727
let url = URL(string:s)!
28-
let req = NSMutableURLRequest(url:url)
29-
if which == 1 { // show how to attach stuff to the request
28+
switch which {
29+
case 0:
30+
let req = URLRequest(url:url)
31+
let task = self.session.downloadTask(with:req)
32+
self.task = task
33+
self.iv.image = nil
34+
task.resume()
35+
36+
case 1:
37+
let req = NSMutableURLRequest(url:url)
3038
URLProtocol.setProperty("howdy", forKey:"greeting", in:req)
39+
let task = self.session.downloadTask(with:req as URLRequest)
40+
self.task = task
41+
self.iv.image = nil
42+
task.resume()
43+
44+
default:break
3145
}
32-
let task = self.session.downloadTask(with:req as URLRequest)
33-
self.task = task
34-
self.iv.image = nil
35-
task.resume()
3646

3747
}
3848

@@ -44,11 +54,12 @@ class ViewController: UIViewController, URLSessionDownloadDelegate {
4454
// unused in this example
4555
}
4656

47-
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: NSError?) {
57+
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
58+
self.task = nil
4859
print("completed: error: \(error)")
4960
}
5061

51-
// this is the only required NSURLSessionDownloadDelegate method
62+
// this is the only required URLSessionDownloadDelegate method
5263

5364
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
5465
if which == 1 {
@@ -58,12 +69,12 @@ class ViewController: UIViewController, URLSessionDownloadDelegate {
5869
}
5970
}
6071
self.task = nil
61-
let response = downloadTask.response as! HTTPURLResponse
62-
let stat = response.statusCode
63-
print("status \(stat)")
64-
if stat != 200 {
65-
return
66-
}
72+
// let response = downloadTask.response as! HTTPURLResponse
73+
// let stat = response.statusCode
74+
// print("status \(stat)")
75+
// if stat != 200 {
76+
// return
77+
// }
6778
if let d = try? Data(contentsOf:location) {
6879
let im = UIImage(data:d)
6980
DispatchQueue.main.async {

bk2ch24p838lessSimpleHTTP2/DataTaskExample/Base.lproj/Main.storyboard

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6250" systemVersion="13F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="GKS-bV-ifY">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11201" systemVersion="15G1004" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" colorMatched="YES" initialViewController="GKS-bV-ifY">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6244"/>
4+
<deployment identifier="iOS"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11161"/>
6+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
57
</dependencies>
68
<scenes>
79
<!--Navigation Controller-->
@@ -29,10 +31,11 @@
2931
<viewControllerLayoutGuide type="bottom" id="Dxo-NP-n3H"/>
3032
</layoutGuides>
3133
<view key="view" contentMode="scaleToFill" id="AwZ-eA-lEq">
32-
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
34+
<rect key="frame" x="0.0" y="64" width="375" height="603"/>
3335
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
34-
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
36+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
3537
</view>
38+
<extendedEdge key="edgesForExtendedLayout"/>
3639
<navigationItem key="navigationItem" id="vcj-bO-Z5u">
3740
<barButtonItem key="rightBarButtonItem" title="Push" id="fFr-67-9up">
3841
<connections>
@@ -54,23 +57,25 @@
5457
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
5558
</layoutGuides>
5659
<view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
57-
<rect key="frame" x="0.0" y="0.0" width="320" height="504"/>
60+
<rect key="frame" x="0.0" y="64" width="375" height="603"/>
5861
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
5962
<subviews>
6063
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="69a-gU-xbG">
61-
<rect key="frame" x="29" y="47" width="46" height="30"/>
64+
<frame key="frameInset" minX="29" minY="47" width="46" height="30"/>
65+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
6266
<state key="normal" title="Button">
63-
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
67+
<color key="titleShadowColor" red="0.5" green="0.5" blue="0.5" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
6468
</state>
6569
<connections>
6670
<action selector="doHTTP:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="ke8-Bi-0bL"/>
6771
</connections>
6872
</button>
6973
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Sum-3l-LjY">
70-
<rect key="frame" x="40" y="108" width="240" height="128"/>
74+
<frame key="frameInset" minX="40" minY="108" width="240" height="128"/>
75+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
7176
</imageView>
7277
</subviews>
73-
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
78+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
7479
</view>
7580
<extendedEdge key="edgesForExtendedLayout"/>
7681
<navigationItem key="navigationItem" id="PLj-fN-2dw"/>
@@ -85,6 +90,6 @@
8590
<simulatedMetricsContainer key="defaultSimulatedMetrics">
8691
<simulatedStatusBarMetrics key="statusBar"/>
8792
<simulatedOrientationMetrics key="orientation"/>
88-
<simulatedScreenMetrics key="destination" type="retina4"/>
93+
<simulatedScreenMetrics key="destination" type="retina4_7.fullscreen"/>
8994
</simulatedMetricsContainer>
9095
</document>

bk2ch24p838lessSimpleHTTP2/DataTaskExample/ViewController.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ViewController: UIViewController, URLSessionDataDelegate {
2020
return
2121
}
2222

23-
let s = "http://www.apeth.net/matt/images/phoenixnewest.jpg"
23+
let s = "https://www.apeth.net/matt/images/phoenixnewest.jpg"
2424
let url = URL(string:s)!
2525
let req = URLRequest(url:url)
2626
let task = self.session.dataTask(with:req) // *
@@ -32,17 +32,19 @@ class ViewController: UIViewController, URLSessionDataDelegate {
3232
}
3333

3434
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
35-
print("received \(data.count) bytes of data")
3635
// do something with the data here!
3736
self.data.append(data)
37+
print("received \(data.count) bytes of data; total \(self.data.count)")
3838
}
3939

4040

41-
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: NSError?) {
41+
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
4242
print("completed: error: \(error)")
4343
self.task = nil
4444
if error == nil {
45-
self.iv.image = UIImage(data:self.data)
45+
DispatchQueue.main.async {
46+
self.iv.image = UIImage(data:self.data)
47+
}
4648
}
4749
}
4850

0 commit comments

Comments
 (0)