Announcements
• Lab 3 is due tonight by 11:59 PM
1E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 1
Today’s Topics
• Web Services (from last class) • WKWebKit
• Threading
2E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 2
Web Services
3E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 3
Your Application & The Cloud
• Store & access remote data
• May be under your control or someone else’s
• Many Web 2.0 apps/sites provide developer API
4E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 4
Integrating with Web Services
• Non-goal of this class: teach you all about web services
– Plenty of tutorials accessible, search on Google
• Many are exposed with XML or JSON
• High level overview of parsing these types of data
5E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 5
XML
6E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 6
Options for Parsing XML
• XMLParser
– Event-driven API
– https://developer.apple.com/reference/foundation
/xmlparser
• SWXMLHash
– Simple Swift XML Parsing
• https://github.com/drmohundro/SWXMLHash
7E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 7
JSON
8E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 8
JavaScript Object Notation
• More lightweight than XML
• Looks a lot like a property list
– Arrays, dictionaries, strings, numbers
• Open source json-framework wrappers for Swift and Objective-C
9E- CxtSeEn4s3i8bl–eMNobeitlewAoprpkliincgatiPonlaDtefovermlopment 9
What does a JSON string look like?
{
}
instructor : Todd Sproull, students : 20,
itunes-u : true, midterm-exam : null, assignments : [ WhatATool,
HelloPoly]
10E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 10
More on JSON
• Introducing JSON
– http://www.json.org/
• Encoding and Decoding Custom Types
– https://developer.apple.com/documentation/foundati on/archives_and_serialization/encoding_and_decodin g_custom_types
• JSON Editor
– https://www.jsoneditoronline.org/
11E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinlicgatPiolnatDfoevrmelopment 11
Recap
• Property lists
– Quick & easy, but limited
• Archived objects
– More flexible, but require writing a lot of code
• SQLite and Core Data
– Elegant solution for many types of problems
• XML and JSON
– Low-overhead options for talking to the cloud
12E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 12
JSON Demo
13E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 13
More on JSON
• Introducing JSON
– http://www.json.org/
• SwiftyJSON
– https://github.com/SwiftyJSON/SwiftyJSON
• JSON Editor
– http://www.jsoneditoronline.org/
14E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 14
Recap
• Property lists
– Quick & easy, but limited
• Archived objects
– More flexible, but require writing a lot of code
• SQLite and Core Data
– Elegant solution for many types of problems
• XML and JSON
– Low-overhead options for talking to the cloud
15E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 15
Firebase Demo
16E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 16
Cloud Firestore
17E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 17
Web Content in iOS
18E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 18
Displaying Web Content
• WebcontentcanbedisplayedwithWKWebView – Introduced in iOS 8, part of WKWebKit Framework
• Replaces UIWebView
• Contentcanbe
– localHTMLstring
– localrawdata+MIMEtype
– remoteURL
• LeveragesWebKit
– fullWKfunctionalitynotcurrentlyexposed – simpleAPIforloading&navigating
– delegateforsomecontrol
– SameJavaScriptenginethatpowersSafari
19E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 19
WKWebView
• WKWebView subclass, configure in Storyboard or in code – Feed it data to display
func loadHTMLString(_ string: String, baseURL:URL?) -> WKNavigation?
func load(_ data: Data,
mimeType: MIMEType: String, characterEncodingName: String, baseURL: URL )-> WKNavigation?
• Or give it a URL request
func load(_ request: URLRequest) -> WKNavigation
– WKNavigation
– Object that contains information for tracking the loading progress of a webpage
• What’s this URLRequest?
– Encapsulates a URL to load and caching policy for fetched data – Older versions of iOS used an NSURL and NSURLRequest
20E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 20
WKWebView
• Properties and actions you’d expect from a web view
isLoading: Bool canGoBack: Bool canGoForward: Bool reload() stopLoading() goBack() goForward()
• A couple others that are handy estimatedProgress: Double evaluateJavaScript(_:completionHandler:)
21E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 21
WKNavigationDelegate
•
•
•
Callbacks for load progress
webView(_: didCommit: ) //called when content starts arriving webView(_: didFinish: ) //called when navigation is complete
Error handling
webView(_: didFail: withError: )
Navigation Loading Policy
//Decides whether to allow or cancel a navigation
webView(_: decidePolicyFor: decisionHandler:)
22E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 22
Demo WKWebView
23E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 23
Multithreading in iOS
• Work done with Queues
• Functions (closures) are assigned as units of work
to the queues
• Queues execute on a CPU thread
• Queues are either serial or concurrent
• Queues are synchronous or asynchronous
24E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 24
Types of Queues
• Main Queue
– Special serial queue where all UI-Activity happens
– Non-UI actions should take place on background queue
• Important to do this to free up main queue
• Global Queues
– Four queues shared by the system with different
priority levels
• Custom Queues
– User generated queues with custom attributes (name,
priority level, etc)
25E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 25
Multithreading (from CS193P)
• Executing a function on another queue let someQueue = DispatchQueue (label: “name”)
someQueue.async { /* do work here */ }
• The main queue (serial queue) – DispatchQueue.main
• All UI work done on main queue
• All time intensive code or synchronous (blocking) done on another
queue
• Swift 3 introduced global queues with different priorities
DispatchQueue.global(qos: .userInitiated) .async {
//do non-UI stuff that may take time DispatchQueue.main.async {
// Call UI functions with with results from other queue }
}
26E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 26
Multithreading (from CS193P)
• Specifying QOS for queues
– userInteractive//quickandhighpriority
– userInitiated //high priority, may take some time
– utility //long running
– background //user not concerned (prefetching)
let queue1 = DispatchQueue(label: “low priority” qos: “DispatchQueue.background”)
let queue2 = DispatchQueue(label: “high priority” qos: “DispatchQueue.userInteractive”)
27E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 27
QoS Demo
28E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 28
Multithreading (CS 193P)
• Multithreaded iOS API
– Many iOS APIs execute on a queue other than the
main queue
– These APIs typically provide a closure as an
argument, which is called upon completion of the
method
– If you want to update the UI, you will need to
dispatch back to the main queue
DispactchQueue.global.async { DispatchQueue.main.async {
// Call UI functions with results from other queue }
29E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 29
}
Multithreading DEMO
30E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 30
More on Concurrent Programming
• Grand Central Dispatch (GCD)
– https://developer.apple.com/library/mac/documen tation/Performance/Reference/GCD_libdispatch_R ef/index.html
– https://developer.apple.com/videos/play/wwdc201 6/720/
• GCD Tutorial with Examples
– https://www.raywenderlich.com/148513/grand- central-dispatch-tutorial-swift-3-part-1
31E-xCteSnEs4i3b8le– MNoebtiwleoArpkpinligcatPiolantDfoevrmelopment 31