diff --git a/APIClient.xcodeproj/project.pbxproj b/APIClient.xcodeproj/project.pbxproj index a549a3b..9344f31 100644 --- a/APIClient.xcodeproj/project.pbxproj +++ b/APIClient.xcodeproj/project.pbxproj @@ -8,6 +8,8 @@ /* Begin PBXBuildFile section */ 078B558C6C77F3C6DCDC4678 /* Pods_APIClientUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82531BA6D6228998A00AB5B6 /* Pods_APIClientUITests.framework */; }; + 17B83C331D0313F600164152 /* PostCodeApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17B83C321D0313F600164152 /* PostCodeApi.swift */; }; + 17B83C371D046BF600164152 /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17B83C361D046BF600164152 /* MainViewController.swift */; }; 872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 872A275B1CF0D87100A988C4 /* APIResource.swift */; }; 874D06591CEF295E009A494D /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 874D06581CEF295E009A494D /* AppDelegate.swift */; }; 874D065E1CEF295E009A494D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 874D065C1CEF295E009A494D /* Main.storyboard */; }; @@ -35,6 +37,8 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 17B83C321D0313F600164152 /* PostCodeApi.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PostCodeApi.swift; sourceTree = ""; }; + 17B83C361D046BF600164152 /* MainViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = ""; }; 198B23CD94C6D84016A5208A /* Pods-APIClientUITests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-APIClientUITests.release.xcconfig"; path = "Pods/Target Support Files/Pods-APIClientUITests/Pods-APIClientUITests.release.xcconfig"; sourceTree = ""; }; 4FFA0A7AA37697BD5CBC3211 /* Pods_APIClientTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_APIClientTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 65C820A0F52E3181871F5ECA /* Pods-APIClientTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-APIClientTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-APIClientTests/Pods-APIClientTests.release.xcconfig"; sourceTree = ""; }; @@ -88,6 +92,7 @@ 872A27571CF0D4E200A988C4 /* View Controller */ = { isa = PBXGroup; children = ( + 17B83C361D046BF600164152 /* MainViewController.swift */, ); name = "View Controller"; sourceTree = ""; @@ -142,6 +147,7 @@ 874D06571CEF295E009A494D /* APIClient */ = { isa = PBXGroup; children = ( + 17B83C321D0313F600164152 /* PostCodeApi.swift */, 874D06581CEF295E009A494D /* AppDelegate.swift */, 874D065C1CEF295E009A494D /* Main.storyboard */, 872A27571CF0D4E200A988C4 /* View Controller */, @@ -469,7 +475,9 @@ buildActionMask = 2147483647; files = ( 872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */, + 17B83C331D0313F600164152 /* PostCodeApi.swift in Sources */, 874D06591CEF295E009A494D /* AppDelegate.swift in Sources */, + 17B83C371D046BF600164152 /* MainViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/APIClient/AppDelegate.swift b/APIClient/AppDelegate.swift index 630c3ed..1cf1e1a 100644 --- a/APIClient/AppDelegate.swift +++ b/APIClient/AppDelegate.swift @@ -7,14 +7,27 @@ // import UIKit +import Moya + @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? + + // PostCodeApi Provider represents the Server + let postCodeApi = MoyaProvider() func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + + // pass PostCodeApi Provider on to the root vier controller + if let mainViewController = (window?.rootViewController as? UINavigationController)?.topViewController as? MainViewController { + + // Pass the PokeAPI Provider on to the root view controller + mainViewController.postCodeApi = postCodeApi + + } return true } diff --git a/APIClient/Base.lproj/LaunchScreen.storyboard b/APIClient/Base.lproj/LaunchScreen.storyboard index ebf48f6..0a546bb 100644 --- a/APIClient/Base.lproj/LaunchScreen.storyboard +++ b/APIClient/Base.lproj/LaunchScreen.storyboard @@ -1,5 +1,5 @@ - + diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index 69b2fbf..6914c85 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -1,8 +1,9 @@ - + + @@ -21,10 +22,10 @@ - + - + @@ -32,13 +33,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + diff --git a/APIClient/Info.plist b/APIClient/Info.plist index 40c6215..114eedc 100644 --- a/APIClient/Info.plist +++ b/APIClient/Info.plist @@ -22,6 +22,17 @@ 1 LSRequiresIPhoneOS + NSAppTransportSecurity + + NSExceptionDomains + + api.zippopotam.us + + NSExceptionAllowsInsecureHTTPLoads + + + + UILaunchStoryboardName LaunchScreen UIMainStoryboardFile diff --git a/APIClient/MainViewController.swift b/APIClient/MainViewController.swift new file mode 100644 index 0000000..04f137d --- /dev/null +++ b/APIClient/MainViewController.swift @@ -0,0 +1,88 @@ +// +// MainViewController.swift +// APIClient +// +// Created by Florian M. on 05/06/16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import UIKit +import Freddy +import Moya + +class MainViewController: UIViewController { + + + var postCodeApi: MoyaProvider? = nil + var placeInfo: PlaceInfo? { + didSet { + placeNameLabel.text = self.placeInfo?.placeName + positionLabel.text = "x: " + (self.placeInfo?.placeLatitude)! + " y: " + (self.placeInfo?.placeLongitude)! + } + } + + // MARK : OUTLETS + + @IBOutlet weak var postCodeTextfield: UITextField! + @IBOutlet weak var placeNameLabel: UILabel! + @IBOutlet weak var positionLabel: UILabel! + + override func viewDidLoad() { + super.viewDidLoad() + + // Do any additional setup after loading the view. + } + + override func didReceiveMemoryWarning() { + super.didReceiveMemoryWarning() + // Dispose of any resources that can be recreated. + } + + // MARK: - Actions + + + @IBAction func entered(sender: AnyObject) { + print("editing did end") + postCodeApi?.request(.germany(postalCode: postCodeTextfield.text!)) {response in + print(response) + + switch response { + case .Success(let response): + do { + try response.filterSuccessfulStatusCodes() + // Try to parse the response to JSON + let json = try JSON(data: response.data) + // Try to decode the JSON to the required type + let placeInfo = try PlaceInfo(json: json) + // Configure view according to model + self.placeInfo = placeInfo + } catch { + print(error) + } + case .Failure(let error): + print(error) + } + } + + } + } +// @IBAction func postCodeEntered(sender: AnyObject) { +// print("editing did end") +// postCodeApi?.request(.germany(postalCode: postCodeTextfield.text!)) {response in +// print(response) +// +// } +// } + + + /* + // MARK: - Navigation + + // In a storyboard-based application, you will often want to do a little preparation before navigation + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { + // Get the new view controller using segue.destinationViewController. + // Pass the selected object to the new view controller. + } + */ + + diff --git a/APIClient/PostCodeApi.swift b/APIClient/PostCodeApi.swift new file mode 100644 index 0000000..fe10daa --- /dev/null +++ b/APIClient/PostCodeApi.swift @@ -0,0 +1,47 @@ +// +// PostCodeApi.swift +// Pods +// +// Created by Florian M. on 04/06/16. +// +// + +import Foundation +import Freddy +import Moya + + +enum Zippopotam: Moya.TargetType { + + /// MARK: Endpoints + + + case germany(postalCode: String) + + + var baseURL: NSURL { return NSURL (string: "http://api.zippopotam.us")! } + var path: String { + switch self { + case .germany(postalCode: let a): return "/DE/"+a + } + } + var method: Moya.Method { return .GET } + var parameters: [String : AnyObject]? { + return nil + } + var sampleData: NSData {return "{\"post code\": \"90210\",\"country\": \"United States\",\"country abbreviation\": \"US\",\"places\": [\"place name\": \"Beverly Hills\",\"longitude\": \"-118.4065\",\"state\": \"California\",\"state abbreviation\": \"CA\", \"latitude\": \"34.0901\"]}".dataUsingEncoding(NSUTF8StringEncoding)!} + + + +} + +struct PlaceInfo{ + let placeName: String + let placeLongitude: String + let placeLatitude: String + public init(json: JSON) throws { + self.placeName = try json.array("places")[0].string("place name") + self.placeLongitude = try json.array("places")[0].string("longitude") + self.placeLatitude = try json.array("places")[0].string("latitude") + } +} \ No newline at end of file diff --git a/APIClient/View.xib b/APIClient/View.xib new file mode 100644 index 0000000..94c1f20 --- /dev/null +++ b/APIClient/View.xib @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/APIClient/WeatherViewController.swift b/APIClient/WeatherViewController.swift new file mode 100644 index 0000000..3ca36d5 --- /dev/null +++ b/APIClient/WeatherViewController.swift @@ -0,0 +1,35 @@ +// +// WeatherViewController.swift +// APIClient +// +// Created by Florian M. on 13/06/16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import UIKit + +class WeatherViewController: UIViewController { + + override func viewDidLoad() { + super.viewDidLoad() + + // Do any additional setup after loading the view. + } + + override func didReceiveMemoryWarning() { + super.didReceiveMemoryWarning() + // Dispose of any resources that can be recreated. + } + + + /* + // MARK: - Navigation + + // In a storyboard-based application, you will often want to do a little preparation before navigation + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { + // Get the new view controller using segue.destinationViewController. + // Pass the selected object to the new view controller. + } + */ + +} diff --git a/APIClientUITests/ViewController.swift b/APIClientUITests/ViewController.swift new file mode 100644 index 0000000..d05f28f --- /dev/null +++ b/APIClientUITests/ViewController.swift @@ -0,0 +1,35 @@ +// +// ViewController.swift +// APIClient +// +// Created by Florian M. on 13/06/16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import UIKit + +class ViewController: UIViewController { + + override func viewDidLoad() { + super.viewDidLoad() + + // Do any additional setup after loading the view. + } + + override func didReceiveMemoryWarning() { + super.didReceiveMemoryWarning() + // Dispose of any resources that can be recreated. + } + + + /* + // MARK: - Navigation + + // In a storyboard-based application, you will often want to do a little preparation before navigation + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { + // Get the new view controller using segue.destinationViewController. + // Pass the selected object to the new view controller. + } + */ + +}