O'Reilly logo

Swift 2 By Example by Giordano Scalzo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Connecting the ASAP app

After implementing the ASAP server, let's add the capability of communicating with a server to our ASAP app.

The products

The first gateway to implement is the one that handles the product, implementing the ProductGateway protocol:

import Foundation class ServerProductGateway: ProductGateway { func getProducts(completion: (String) -> Void) { let session = NSURLSession.sharedSession() let task = session.dataTaskWithURL(EndPoint.Products.url()) { (data, response, error) -> Void in if error != nil { print(error!.localizedDescription) return } guard let data = data,let products = NSString(data: data,encoding: NSUTF8StringEncoding) as? String else { return } dispatch_async(dispatch_get_main_queue()) { completion(products) } } task.resume() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required