iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods,...
Transcript of iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods,...
![Page 1: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/1.jpg)
iOS DeCal : Lecture 6Networking, CocoaPods, Alamofire, and
SwiftyJSON
March 14, 2017
![Page 2: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/2.jpg)
Lab 4 and Proj 2 Pt 1 due tonight (11:59pm) Office hours after lecture in 341A Soda (8-10pm)
Custom App Proposal due 3/21 (next Tuesday) See the spec posted on the website for guidelines
We now have a room for Tuesday office hours! 341A Soda (in the undergraduate lounge) 8-10pm
Announcements - 3/14
![Page 3: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/3.jpg)
For this week’s lab, you’ll need a device to test on.
If you have a iPhone or iPad, please bring it along with a lightning cable that connects to your computer.
No worries if you don’t have one - you can work with a partner who does (the lab should be short enough to finish during the lab period)
Lab 5 : Snapchat Camera
![Page 4: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/4.jpg)
May work individually or with a groups Up to 4 people total per group
Students with the top submissions will be given the opportunity to present at the Final Presentation (Friday, May 5 at 10am)
Attendance is mandatory for a project grade Dan Garcia & Recruiters will be attending
Please see the spec for more information!
Custom App : Final Project
![Page 5: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/5.jpg)
moodmaps
Nithi Narayanan
![Page 6: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/6.jpg)
cloub
Chan Hee Park
![Page 7: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/7.jpg)
Networking
CocoaPods
Alamofire
SwiftyJSON
Overview : Today’s Lecture
![Page 8: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/8.jpg)
Networking
![Page 9: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/9.jpg)
Networking is acquiring/passing data to/from some URL that exists on the world wide web or local.
General structure as it relates to iOS • Recipient Address • Parameters • Response
Networking & iOS
![Page 10: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/10.jpg)
Making an "Application Programming Interface" Call
Parameters
Response
![Page 11: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/11.jpg)
• Hypertext Transfer Protocol • "GET" Request: Getting data at a URL • "POST" Request: Sending data to a URL
The HTTP Request
![Page 12: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/12.jpg)
Alerts
![Page 13: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/13.jpg)
let alertController = UIAlertController( title: "Location", message: "Enabling Wi-Fi in Settings will improve your location accuracy.", preferredStyle: .alert)
Making an Alert Controller
![Page 14: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/14.jpg)
let openAction = UIAlertAction(title: "Open Settings", style: .default) { (action) in if let url = URL(string:UIApplicationOpenSettingsURLString) { UIApplication.shared.open(url, options: [:], completionHandler: nil) } }
alertController.addAction(openAction)
Making an Alert Controller
![Page 15: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/15.jpg)
Can capture and store references to any constants and variables from the context in which they are defined
• Global closure functions • Named, do not capture values
• Nested closure functions • Named, capture values from enclosing
function
Closures
![Page 16: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/16.jpg)
let intPow = {(val1: Int, val2: Int) -> Int in return Int(pow(Double(val1), Double(val2))) }
let result = intPow(2, 10) print (result)
Global Closures : Example
![Page 17: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/17.jpg)
func makeIncrementer(forIncrement amount: Int) -> () -> Int { var runningTotal = 0 func incrementer() -> Int { runningTotal += amount return runningTotal } return incrementer } var incrementer = makeIncrementer(forIncrement: 5)
Nested Closures : Example
![Page 18: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/18.jpg)
{ (parameters) -> return type in statements }
Closures : Format
![Page 19: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/19.jpg)
let openAction = UIAlertAction(title: "Open Settings", style: .default) { (action) in if let url = URL(string:UIApplicationOpenSettingsURLString) { UIApplication.shared.open(url, options: [:], completionHandler: nil) } }
alertController.addAction(openAction)
Making an Alert Controller
![Page 20: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/20.jpg)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
Making an Alert Controller
![Page 21: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/21.jpg)
URL Session
![Page 22: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/22.jpg)
Apple’s API for downloading content
Support various URL schemes HTTP, HTTPS, FTP, Data, File
Pass in a URL URL object, allocated from String
URLSession
![Page 23: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/23.jpg)
URL Object that contains URL URLRequest Contains URL, request method, etc. URLResponse Contains info for server’s response
Some Relevant Classes
![Page 24: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/24.jpg)
1) Create URL from a String
2) Create URLSession
3) Create a URLSessionDataTask Get data from the task and save it
URLSession Workflow
![Page 25: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/25.jpg)
URLSession.shared() Basic session, un-customizable We’ll stick to this for the rest of the class
URLSessionDataTask dataTaskWithURL - Default HTTP GET dataTaskWithRequest - Can specify HTTP
URLSession
![Page 26: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/26.jpg)
func loadImage() { let url = URL(string:”https://instagram.com/img.jpg")
let session = URLSession.shared
let task = session.dataTask(with: url!, completionHandler: { (data, response, error) -> Void in if error == nil { let img = UIImage.init(data: data!) self.imageView.image = img } }) task.resume() }
URLSession
![Page 27: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/27.jpg)
CocoaPods
![Page 28: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/28.jpg)
A dependency manager for iOS Projects
Cocoapods are essentially Swift classes that other people write for you that you can use in your project:
• Make life more efficient • Make life easier
What are CocoaPods
![Page 29: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/29.jpg)
Adding A Year To the Current Date:
Without Timepiece:
let calendar = NSCalendar.currentCalendar() let newDate = calendar.dateByAddingUnit(.Year, value: 1, toDate: NSDate(), options: NSCalendarOptions.MatchNextTime)
With Timepiece
let newDate = now + 1.year
Timepiece
![Page 30: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/30.jpg)
How to use Cocoapods
![Page 31: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/31.jpg)
sudo gem install cocoapods
Install Cocoapods
![Page 32: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/32.jpg)
1. Just named Podfile with no extension 2. Format (Trick: Use “pod init”):
Make a Podfile
![Page 33: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/33.jpg)
Update Dependencies
pod install
![Page 34: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/34.jpg)
Open myApp.xcworkspace
![Page 35: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/35.jpg)
Check Ins
![Page 36: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/36.jpg)
Alamofire
![Page 37: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/37.jpg)
HTTP networking library written in Swift
Simplifies common networking tasks
Request/Response methods JSON serialization Authentication
GitHub link
Alamofire : Networking in Swift
![Page 38: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/38.jpg)
.request: HTTP requests
.upload: Upload large files
.download: Download large files or resume a download already in progress.
Alamofire : Requests
![Page 39: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/39.jpg)
Alamofire.request("https://httpbin.org/get") // default is get
Alamofire.request("https://httpbin.org/post", method: .post)
Alamofire : Request types
![Page 40: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/40.jpg)
// Response Data Handler - Serialized into Data func responseData(queue: DispatchQueue?, completionHandler: @escaping (DataResponse<Data>) -> Void) -> Self
// Response JSON Handler - Serialized into Any func responseJSON(queue: DispatchQueue?, completionHandler: @escaping (DataResponse<Any>) -> Void) -> Self
Alamofire : Response Handlers
![Page 41: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/41.jpg)
Alamofire.request("https://httpbin.org/get”) .validate().responseJSON { response in switch response.result { case .success: print("Validation Successful") case .failure(let error): print(error) } }
Alamofire : Response Validation
![Page 42: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/42.jpg)
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters)
Alamofire.request("https://httpbin.org/post",
method: .post, parameters: parameters, encoding: URLEncoding.default)
Alamofire : Parameters
![Page 43: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/43.jpg)
let user = "user" let password = "password"
Alamofire.request(“https:// httpbin.org/basic-auth/\(user)/\(password)")
.authenticate(user: user, password: password)
.responseJSON { response in debugPrint(response)
}
Alamofire : Authentication
![Page 44: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/44.jpg)
SwiftyJSON
![Page 45: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/45.jpg)
Easy to use JSON parsing library
Makes it easier to handle JSON in your project
Avoids strict Swift type checking to make JSON parsing less verbose
GitHub installation link
SwiftyJSON : JSON Parsing
swifty
![Page 46: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/46.jpg)
if let statuses = try JSONSerialization. jsonObject(with: data, options: .allowFragments) as? [[String: Any]],
let user = statuses[0]["user"] as? [String: Any],
let username = user["name"] as? String { // Finally we got the username } }
JSON parsing (without SwiftyJSON)
Example Above —> Retrieving a username name from a Tweet using Twitter’s API (example link)
![Page 47: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/47.jpg)
let json = JSON(data: dataFromNetworking) if let userName = json[0][“user"]
["name"].string { // Now we have the username!
}
JSON parsing (using SwiftyJSON)
Same example as the previous slide, except here we are using SwiftyJSON (example link)
![Page 48: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/48.jpg)
//Getting a double from a JSON Array let name = json[0].double //Getting a string from a JSON Dictionary let name = json["name"].stringValue //Getting an array of string from a JSON Array let arrayNames = json["users"].arrayValue.map({$0["name"].stringValue})
Using SwiftyJSON
![Page 49: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/49.jpg)
Demo
![Page 50: iOS DeCal : Lecture 6akile.sh/iosdecal_spring17/Lectures/Lecture6.pdf · Networking, CocoaPods, Alamofire, and SwiftyJSON March 14, 2017. Lab 4 and Proj 2 Pt 1 due tonight (11:59pm)](https://reader034.fdocuments.net/reader034/viewer/2022042219/5ec524b19d698e19070723c1/html5/thumbnails/50.jpg)
Custom App Proposal Due next Tuesday at 11:59pm
Project 2 Part 1 and Lab 4 Due Tonight at 11:59pm
Remember to bring your iPhone / iPad + cable to Thursday’s Lab!