Try!Swift India 2017: All you need is Swift

80
All you need is Swift Safe | Expressive | Fast Chris Bailey @Chris__Bailey AB Vijay Kumar @abvijaykumar

Transcript of Try!Swift India 2017: All you need is Swift

Page 1: Try!Swift India 2017: All you need is Swift

All you need is

SwiftSafe | Expressive | Fast

Chris Bailey @Chris__Bailey

AB Vijay Kumar @abvijaykumar

Page 2: Try!Swift India 2017: All you need is Swift

We delivered >100 apps that change the way people work

We created Accelerators for iOS custom app delivery

We now bring everything together for enterprise wide integration of iOS app

services

In July 2014 Apple and IBM announced a partnership to transform enterprise mobility.

Page 3: Try!Swift India 2017: All you need is Swift

100+ Apps

14 Industries

50+ Clients

Page 4: Try!Swift India 2017: All you need is Swift

PUBLIC NETWORK CLOUD NETWORK ENTERPRISE NETWORK

Mobile App

EDGE SERVICES

LOAD BALANCER

CONNECTIVITY

ENTERPRISEAPPLICATION

Mobile Services Enterprise Integration

DevOps Services

MOBILE MIDDLEWARE

PUSN NOTIFICATION

MOBILE DATA STOREMOBILE SECURITY

API MANAGEMENT

STORAGE

MOBILE ANALYTICS

Data Services

NOTIFICATION SERVICEPROVIDER

API Services CLIENTFIREWALL

MOBILE APPLIFECYCLE

CONTINUOUSRELEASE

CONTINUOUS BUSINESSPLANNING

CONTINUOUSMONITORING & FEEDBACK

CONFIGURATIONMANAGEMENT CONTINUOUS

DEPLOYMENT

CACHING

ENTERPRISE DATA

ENTERPRISE DIRECTORY

API INTEGRATION

Page 5: Try!Swift India 2017: All you need is Swift

Rapid App Development

Full Stack Development

Scalable & High performance Mobile backend

Easy Integration with other Cloud Services & Backend System

Page 6: Try!Swift India 2017: All you need is Swift

Rapid App Development

Full Stack Development

Scalable & High performance Mobile backend

Easy Integration with other Cloud Services & Backend System

Page 7: Try!Swift India 2017: All you need is Swift
Page 8: Try!Swift India 2017: All you need is Swift

Mouse Maze Free: © Top Free Games

Page 9: Try!Swift India 2017: All you need is Swift

Mouse Maze Free: © Top Free Games

Page 10: Try!Swift India 2017: All you need is Swift

Mouse Maze Free: © Top Free Games

Page 11: Try!Swift India 2017: All you need is Swift

Mouse Maze Free: © Top Free Games

Page 12: Try!Swift India 2017: All you need is Swift

IBM Expert Seller

Page 13: Try!Swift India 2017: All you need is Swift

IBM Expert Seller

Page 14: Try!Swift India 2017: All you need is Swift

Icons made by Freepik from www.flaticon.com

Page 15: Try!Swift India 2017: All you need is Swift

Icons made by Freepik from www.flaticon.com

Page 16: Try!Swift India 2017: All you need is Swift

Icons made by Freepik from www.flaticon.com

Page 17: Try!Swift India 2017: All you need is Swift

struct Profile { var name: String var photo: Data var dateOfBirth: Date}

Swift

Page 18: Try!Swift India 2017: All you need is Swift

{ "name": "", "photo": "", "dateOfBirth": ""}

{ "name": "", "photo": "", "dateOfBirth": ""}

struct Profile { var name: String var photo: Data var dateOfBirth: Date}

Swift

Page 19: Try!Swift India 2017: All you need is Swift

Swiftparsed = JSON.parse(data)

name = parsed["name"] photo = parsed["photo"] dateOfBirth = parsed["dateOfBirth"]

Node.js

struct Profile { var name: String var photo: Data var dateOfBirth: Date}

{ "name": "", "photo": "", "dateOfBirth": ""}

Page 20: Try!Swift India 2017: All you need is Swift

parsed = JSON.parse(data)

name = parsed["name"] photo = parsed["photo"] dateOfBirth = parsed["dateOfBirth"]

{ "name": "", "photo": "", "dateOfBirth": ""}

struct Profile { var name: String var photo: Data var dateOfBirth: Date}

Swift Node.js

Page 21: Try!Swift India 2017: All you need is Swift

parsed = JSON.parse(data)

name = parsed["name"] photo = parsed["photo"] dateOfBirth = parsed["dateOfBirth"]

{ "name": "", "photo": "", "dateOfBirth": ""}

struct Profile { var name: String var photo: Data var dateOfBirth: Date}

Swift Node.js

Data

Page 22: Try!Swift India 2017: All you need is Swift

parsed = JSON.parse(data)

name = parsed["name"] photo = parsed["photo"] dateOfBirth = parsed["dateOfBirth"]

{ "name": "", "photo": "", "dateOfBirth": ""}

struct Profile { var name: String var photo: Data var dateOfBirth: Date}

Swift Node.js

Data String

Page 23: Try!Swift India 2017: All you need is Swift

parsed = JSON.parse(data)

name = parsed["name"] photo = parsed["photo"] dateOfBirth = parsed["dateOfBirth"]

{ "name": "", "photo": "", "dateOfBirth": ""}

struct Profile { var name: String var photo: Data var dateOfBirth: Date}

Swift Node.js

Data String String of data

Page 24: Try!Swift India 2017: All you need is Swift

parsed = JSON.parse(data)

name = parsed["name"] photo = parsed["photo"] dateOfBirth = parsed["dateOfBirth"]

{ "name": "", "photo": "", "dateOfBirth": ""}

struct Profile { var name: String var photo: Data var dateOfBirth: Date}

Swift Node.js

Page 25: Try!Swift India 2017: All you need is Swift

parsed = JSON.parse(data)

name = parsed["name"] photo = parsed["photo"] dateOfBirth = parsed["dateOfBirth"]

{ "name": "", "photo": "", "dateOfBirth": ""}

struct Profile { var name: String var photo: Data var dateOfBirth: Date}

Swift Node.js

Date

Page 26: Try!Swift India 2017: All you need is Swift

parsed = JSON.parse(data)

name = parsed["name"] photo = parsed["photo"] dateOfBirth = parsed["dateOfBirth"]

{ "name": "", "photo": "", "dateOfBirth": ""}

struct Profile { var name: String var photo: Data var dateOfBirth: Date}

Swift Node.js

Date String

Page 27: Try!Swift India 2017: All you need is Swift

parsed = JSON.parse(data)

name = parsed["name"] photo = parsed["photo"] dateOfBirth = parsed["dateOfBirth"]

{ "name": "", "photo": "", "dateOfBirth": ""}

struct Profile { var name: String var photo: Data var dateOfBirth: Date}

Swift Node.js

Date String String

Page 28: Try!Swift India 2017: All you need is Swift

parsed = JSON.parse(data)

name = parsed["name"] photo = parsed["photo"] dateOfBirth = parsed["dateOfBirth"]

struct Profile { var name: String var photo: Data var dateOfBirth: Date}

{ "name": "", "photo": "", "dateOfBirth": ""}

{ "name": "", "photo": "", "dateOfBirth": { "year": "month": "day": } }

Swift Node.js

Date String

Page 29: Try!Swift India 2017: All you need is Swift

struct Profile { var name: String var photo: Data var dateOfBirth: Date}

{ "name": "", "photo": "", "dateOfBirth": ""}

{ "name": "", "photo": "", "dateOfBirth": { "year": "month": "day": }}

Swiftparsed = JSON.parse(data)

name = parsed["name"] photo = parsed["photo"] dateOfBirth = parsed["dateOfBirth"] year = dateOfBirth["year"] month = dateOfBirth["month"] day = dateOfBirth[“day"]

Node.js

Date Strings

Page 30: Try!Swift India 2017: All you need is Swift

I have to learn Node.js? (or Ruby, Python, Java, etc)

Page 31: Try!Swift India 2017: All you need is Swift

Swift

struct Profile { var name: String var photo: Data var dateOfBirth: Date}

Page 32: Try!Swift India 2017: All you need is Swift

Swift Swift

KITURA

struct Profile { var name: String var photo: Data var dateOfBirth: Date}

Page 33: Try!Swift India 2017: All you need is Swift

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

Swift Swift

KITURA

Codable

Page 34: Try!Swift India 2017: All you need is Swift

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

Swift Swift

KITURA

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

Shared

Page 35: Try!Swift India 2017: All you need is Swift

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

Swift Swift

let encoder = JSONEncoder() let data = try encoder.encode(profile)

KITURA

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

Page 36: Try!Swift India 2017: All you need is Swift

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

Swift Swift

let decoder = JSONDecoder() let person = try decoder.decode(Person.self, from: jsonData)

KITURA

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

Page 37: Try!Swift India 2017: All you need is Swift

{ "name": "", "photo": "", "dateOfBirth": ""}

{ "name": "", "photo": "", "dateOfBirth": ""}

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

Swift Swift

KITURA

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

Page 38: Try!Swift India 2017: All you need is Swift

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

Swift Swift

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

KITURA

struct Profile: Codable { var name: String var photo: Data var dateOfBirth: Date}

Page 39: Try!Swift India 2017: All you need is Swift
Page 40: Try!Swift India 2017: All you need is Swift

Deploy Deploy

KITURA

Page 41: Try!Swift India 2017: All you need is Swift

Deploy DeployGenerate

KITURA

Page 42: Try!Swift India 2017: All you need is Swift

Icons made by Freepik from www.flaticon.com

Page 43: Try!Swift India 2017: All you need is Swift

Icons made by Freepik from www.flaticon.com

Page 44: Try!Swift India 2017: All you need is Swift

Fetch the Profile for a user from the Backend

Page 45: Try!Swift India 2017: All you need is Swift
Page 46: Try!Swift India 2017: All you need is Swift

var urlComponents = URLComponents()urlComponents.scheme = "https"urlComponents.host = "myhost.myserver.com"urlComponents.path = “/profile"

Set up Backend URL

Page 47: Try!Swift India 2017: All you need is Swift

var urlComponents = URLComponents()urlComponents.scheme = "https"urlComponents.host = "myhost.myserver.com"urlComponents.path = “/profile"

let userIdItem = URLQueryItem(name: "id", value: "\(userId)")urlComponents.queryItems = [userIdItem]guard let url = urlComponents.url else { fatalError("Could not create URL from components") }

Set up Person ID

Page 48: Try!Swift India 2017: All you need is Swift

var urlComponents = URLComponents()urlComponents.scheme = "https"urlComponents.host = "myhost.myserver.com"urlComponents.path = “/profile"

let userIdItem = URLQueryItem(name: "id", value: "\(userId)")urlComponents.queryItems = [userIdItem]guard let url = urlComponents.url else { fatalError("Could not create URL from components") } var request = URLRequest(url: url)request.httpMethod = "GET"

Set up “GET” request

Page 49: Try!Swift India 2017: All you need is Swift

var urlComponents = URLComponents()urlComponents.scheme = "https"urlComponents.host = "myhost.myserver.com"urlComponents.path = “/profile"

let userIdItem = URLQueryItem(name: "id", value: "\(userId)")urlComponents.queryItems = [userIdItem]guard let url = urlComponents.url else { fatalError("Could not create URL from components") } var request = URLRequest(url: url)request.httpMethod = "GET" let session = URLSession(configuration: URLSessionConfiguration.default)let task = session.dataTask(with: request) { (responseData, response, responseError) in }task.resume()

Set up URLSession

Page 50: Try!Swift India 2017: All you need is Swift

var urlComponents = URLComponents()urlComponents.scheme = "https"urlComponents.host = "myhost.myserver.com"urlComponents.path = “/profile"

let userIdItem = URLQueryItem(name: "id", value: "\(userId)")urlComponents.queryItems = [userIdItem]guard let url = urlComponents.url else { fatalError("Could not create URL from components") } var request = URLRequest(url: url)request.httpMethod = "GET" let session = URLSession(configuration: URLSessionConfiguration.default)let task = session.dataTask(with: request) { (responseData, response, responseError) in }task.resume()

Web RequestWeb Response

Error

Call URLSession

Page 51: Try!Swift India 2017: All you need is Swift

var urlComponents = URLComponents()urlComponents.scheme = "https"urlComponents.host = "myhost.myserver.com"urlComponents.path = “/profile"

let userIdItem = URLQueryItem(name: "id", value: "\(userId)")urlComponents.queryItems = [userIdItem]guard let url = urlComponents.url else { fatalError("Could not create URL from components") } var request = URLRequest(url: url)request.httpMethod = "GET" let session = URLSession(configuration: URLSessionConfiguration.default)let task = session.dataTask(with: request) { (responseData, response, responseError) in DispatchQueue.main.async { guard responseError == nil else { completion?(.failure(responseError!)) } guard let jsonData = responseData else { let error = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: “No data"]) as Error completion?(.failure(error)) } } }task.resume()

Error Checking

Page 52: Try!Swift India 2017: All you need is Swift

var urlComponents = URLComponents()urlComponents.scheme = "https"urlComponents.host = "myhost.myserver.com"urlComponents.path = “/profile"

let userIdItem = URLQueryItem(name: "id", value: "\(userId)")urlComponents.queryItems = [userIdItem]guard let url = urlComponents.url else { fatalError("Could not create URL from components") } var request = URLRequest(url: url)request.httpMethod = "GET" let session = URLSession(configuration: URLSessionConfiguration.default)let task = session.dataTask(with: request) { (responseData, response, responseError) in DispatchQueue.main.async { guard responseError == nil else { completion?(.failure(responseError!)) } guard let jsonData = responseData else { let error = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: “No data"]) as Error completion?(.failure(error)) } let decoder = JSONDecoder() do { let person = try decoder.decode(Profile.self, from: jsonData) completion?(.success(person)) } catch { completion?(.failure(error)) } }}task.resume()

Convert Response

Page 53: Try!Swift India 2017: All you need is Swift
Page 54: Try!Swift India 2017: All you need is Swift

router.get(“/profile", handler: getProfile)

func getProfile(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) { guard let contentType = request.headers["Content-Type"], contentType.hasPrefix("application/json") else { response.status(.unsupportedMediaType) response.send(error) return next() } let name = request.parameters["name"] ?? "" let encoder = JSONEncoder() let profile = profileStore[name] var data: Data

do { data = try encoder.encode(profile) } catch let error { response.status(.unprocessableEntity) response.send(error) next() } response.send(data) next() }

Page 55: Try!Swift India 2017: All you need is Swift

router.get(“/profile", handler: getProfile)

func getProfile(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) { guard let contentType = request.headers["Content-Type"], contentType.hasPrefix("application/json") else { response.status(.unsupportedMediaType) response.send(error) return next() } let name = request.parameters["name"] ?? "" let encoder = JSONEncoder() let profile = profileStore[name] var data: Data

do { data = try encoder.encode(profile) } catch let error { response.status(.unprocessableEntity) response.send(error) next() } response.send(data) next() }

Web Request

Page 56: Try!Swift India 2017: All you need is Swift

router.get(“/profile", handler: getProfile)

func getProfile(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) { guard let contentType = request.headers["Content-Type"], contentType.hasPrefix("application/json") else { response.status(.unsupportedMediaType) response.send(error) return next() } let name = request.parameters["name"] ?? "" let encoder = JSONEncoder() let profile = profileStore[name] var data: Data

do { data = try encoder.encode(profile) } catch let error { response.status(.unprocessableEntity) response.send(error) next() } response.send(data) next() }

Web RequestWeb Response

Page 57: Try!Swift India 2017: All you need is Swift

router.get(“/profile", handler: getProfile)

func getProfile(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) { guard let contentType = request.headers["Content-Type"], contentType.hasPrefix("application/json") else { response.status(.unsupportedMediaType) response.send(error) return next() } let name = request.parameters["name"] ?? "" let encoder = JSONEncoder() let profile = profileStore[name] var data: Data

do { data = try encoder.encode(profile) } catch let error { response.status(.unprocessableEntity) response.send(error) next() } response.send(data) next() }

Web RequestWeb Response

Validate and Convert parameters

Page 58: Try!Swift India 2017: All you need is Swift

router.get(“/profile", handler: getProfile)

func getProfile(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) { guard let contentType = request.headers["Content-Type"], contentType.hasPrefix("application/json") else { response.status(.unsupportedMediaType) response.send(error) return next() } let name = request.parameters["name"] ?? "" let encoder = JSONEncoder() let profile = profileStore[name] var data: Data

do { data = try encoder.encode(profile) } catch let error { response.status(.unprocessableEntity) response.send(error) next() } response.send(data) next() }

Web RequestWeb Response

Validate and Convert parameters

Encode and Send Result

Page 59: Try!Swift India 2017: All you need is Swift

I have to learn Web Programming?

Page 60: Try!Swift India 2017: All you need is Swift

KITURA+

Kitura 2.0KituraKit

Page 61: Try!Swift India 2017: All you need is Swift

KITURAKITvar urlComponents = URLComponents()urlComponents.scheme = "https"urlComponents.host = "myhost.myserver.com"urlComponents.path = “/profile"

let userIdItem = URLQueryItem(name: "id", value: "\(userId)")urlComponents.queryItems = [userIdItem]guard let url = urlComponents.url else { fatalError("Could not create URL from components") } var request = URLRequest(url: url)request.httpMethod = "GET" let session = URLSession(configuration: URLSessionConfiguration.default)let task = session.dataTask(with: request) { (responseData, response, responseError) in DispatchQueue.main.async { guard responseError == nil else { completion?(.failure(responseError!)) } guard let jsonData = responseData else { let error = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: “No data"]) as Error completion?(.failure(error)) } let decoder = JSONDecoder() do { let person = try decoder.decode(Person.self, from: jsonData) completion?(.success(person)) } catch { completion?(.failure(error)) } }}task.resume()

Page 62: Try!Swift India 2017: All you need is Swift

guard let backend = KituraKit(baseURL: "http://localhost:8080") else { print("Error creating KituraKit client") return}

KITURAKIT

Page 63: Try!Swift India 2017: All you need is Swift

guard let backend = KituraKit(baseURL: "http://localhost:8080") else { print("Error creating KituraKit client") return}

backend.get("/profile", id: userId) { (profile: Profile?, error: RequestError?) in ...

}

KITURAKIT

Page 64: Try!Swift India 2017: All you need is Swift

guard let backend = KituraKit(baseURL: "http://localhost:8080") else { print("Error creating KituraKit client") return}

backend.get("/profile", id: userId) { (profile: Profile?, error: RequestError?) in ...

}

KITURAKIT

Swift Type (Identifier)

Page 65: Try!Swift India 2017: All you need is Swift

guard let backend = KituraKit(baseURL: "http://localhost:8080") else { print("Error creating KituraKit client") return}

backend.get("/profile", id: userId) { (profile: Profile?, error: RequestError?) in ...

}

KITURAKIT

Swift Type (Codable)

Swift Type (Identifier)

Page 66: Try!Swift India 2017: All you need is Swift

guard let backend = KituraKit(baseURL: "http://localhost:8080") else { print("Error creating KituraKit client") return}

backend.get("/profile", id: userId) { (profile: Profile?, error: RequestError?) in ...

}

KITURAKIT

Swift Type (Codable)

Swift Type (RequestError)Swift Type

(Identifier)

Page 67: Try!Swift India 2017: All you need is Swift

KITURAKITURA 2.0

router.get(“/profile", handler: getProfile)

func getProfile(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) { guard let contentType = request.headers["Content-Type"], contentType.hasPrefix("application/json") else { response.status(.unsupportedMediaType) response.send(error) return next() } let name = request.parameters["name"] ?? "" let encoder = JSONEncoder() let profile = profileStore[name] var data: Data

do { data = try encoder.encode(profile) } catch let error { response.status(.unprocessableEntity) response.send(error) next() } response.send(data) next() }

Page 68: Try!Swift India 2017: All you need is Swift

router.get(“/profile”) { (userId: Int, respondWith: @escaping (Profile?, RequestError?) -> Void) in ...

}

KITURAKITURA 2.0

Page 69: Try!Swift India 2017: All you need is Swift

router.get(“/profile”) { (userId: Int, respondWith: @escaping (Profile?, RequestError?) -> Void) in ...

} Swift Type (Identifier)

KITURAKITURA 2.0

Page 70: Try!Swift India 2017: All you need is Swift

router.get(“/profile”) { (userId: Int, respondWith: @escaping (Profile?, RequestError?) -> Void) in ...

}

Swift Type (Codable)

Swift Type (Identifier)

KITURAKITURA 2.0

Page 71: Try!Swift India 2017: All you need is Swift

router.get(“/profile”) { (userId: Int, respondWith: @escaping (Profile?, RequestError?) -> Void) in ...

}

Swift Type (Codable)

Swift Type (RequestError)

Swift Type (Identifier)

KITURAKITURA 2.0

Page 72: Try!Swift India 2017: All you need is Swift

Icons made by Freepik from www.flaticon.com

Page 73: Try!Swift India 2017: All you need is Swift

Icons made by Freepik from www.flaticon.com

Page 74: Try!Swift India 2017: All you need is Swift

Can I use it in for real apps?

Page 75: Try!Swift India 2017: All you need is Swift

Commercial Support for Swift and Kitura

from

Page 76: Try!Swift India 2017: All you need is Swift

from

IBM Cloud

Commercial Support for Swift and Kitura

Page 77: Try!Swift India 2017: All you need is Swift

Become a SwiftStack Developer Today

kitura.io

Page 78: Try!Swift India 2017: All you need is Swift

Become a SwiftStack Developer Today

kitura.ioswift-at-ibm-slack .mybluemix.net

Page 79: Try!Swift India 2017: All you need is Swift

Become a SwiftStack Developer Today

kitura.io github.com/ibm-swiftswift-at-ibm-slack .mybluemix.net

Page 80: Try!Swift India 2017: All you need is Swift

Join IBM http://ibmreferrals.reppify.com/