From 9ba9a4e103aa722c0d8d3dcb3449ffd458d2a907 Mon Sep 17 00:00:00 2001 From: XXXXX43 Date: Mon, 6 Jun 2016 21:08:00 +0200 Subject: [PATCH 1/5] 1. Step --- APIClient.xcodeproj/project.pbxproj | 16 +++++ APIClient/AppDelegate.swift | 3 + APIClient/Base.lproj/Main.storyboard | 82 ++++++++++++++++++++++- APIClient/InformationViewController.swift | 20 ++++++ APIClient/Peoples.swift | 33 +++++++++ APIClient/SWAPI.swift | 59 ++++++++++++++++ APIClient/SearchViewController.swift | 77 +++++++++++++++++++++ 7 files changed, 287 insertions(+), 3 deletions(-) create mode 100644 APIClient/InformationViewController.swift create mode 100644 APIClient/Peoples.swift create mode 100644 APIClient/SWAPI.swift create mode 100644 APIClient/SearchViewController.swift diff --git a/APIClient.xcodeproj/project.pbxproj b/APIClient.xcodeproj/project.pbxproj index a549a3b..4730aa5 100644 --- a/APIClient.xcodeproj/project.pbxproj +++ b/APIClient.xcodeproj/project.pbxproj @@ -8,6 +8,10 @@ /* Begin PBXBuildFile section */ 078B558C6C77F3C6DCDC4678 /* Pods_APIClientUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82531BA6D6228998A00AB5B6 /* Pods_APIClientUITests.framework */; }; + 1652B9AB1D05E48F00443CF1 /* SearchViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1652B9AA1D05E48F00443CF1 /* SearchViewController.swift */; }; + 1652B9AD1D05E67F00443CF1 /* InformationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1652B9AC1D05E67F00443CF1 /* InformationViewController.swift */; }; + 1652B9AF1D05E81F00443CF1 /* SWAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1652B9AE1D05E81F00443CF1 /* SWAPI.swift */; }; + 1652B9B11D05EBDF00443CF1 /* Peoples.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1652B9B01D05EBDF00443CF1 /* Peoples.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 +39,10 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 1652B9AA1D05E48F00443CF1 /* SearchViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchViewController.swift; sourceTree = ""; }; + 1652B9AC1D05E67F00443CF1 /* InformationViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InformationViewController.swift; sourceTree = ""; }; + 1652B9AE1D05E81F00443CF1 /* SWAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SWAPI.swift; sourceTree = ""; }; + 1652B9B01D05EBDF00443CF1 /* Peoples.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Peoples.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 +96,8 @@ 872A27571CF0D4E200A988C4 /* View Controller */ = { isa = PBXGroup; children = ( + 1652B9AA1D05E48F00443CF1 /* SearchViewController.swift */, + 1652B9AC1D05E67F00443CF1 /* InformationViewController.swift */, ); name = "View Controller"; sourceTree = ""; @@ -103,6 +113,8 @@ isa = PBXGroup; children = ( 872A275B1CF0D87100A988C4 /* APIResource.swift */, + 1652B9B01D05EBDF00443CF1 /* Peoples.swift */, + 1652B9AE1D05E81F00443CF1 /* SWAPI.swift */, ); name = Model; sourceTree = ""; @@ -468,8 +480,12 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 1652B9AB1D05E48F00443CF1 /* SearchViewController.swift in Sources */, 872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */, 874D06591CEF295E009A494D /* AppDelegate.swift in Sources */, + 1652B9AF1D05E81F00443CF1 /* SWAPI.swift in Sources */, + 1652B9B11D05EBDF00443CF1 /* Peoples.swift in Sources */, + 1652B9AD1D05E67F00443CF1 /* InformationViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/APIClient/AppDelegate.swift b/APIClient/AppDelegate.swift index 630c3ed..67e6023 100644 --- a/APIClient/AppDelegate.swift +++ b/APIClient/AppDelegate.swift @@ -7,11 +7,14 @@ // import UIKit +import Moya @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? + + let swAPI = MoyaProvider() func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index 69b2fbf..611ac54 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -21,10 +21,10 @@ - + - + @@ -32,13 +32,89 @@ + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/APIClient/InformationViewController.swift b/APIClient/InformationViewController.swift new file mode 100644 index 0000000..1a13687 --- /dev/null +++ b/APIClient/InformationViewController.swift @@ -0,0 +1,20 @@ +// +// InformationViewController.swift +// APIClient +// +// Created by logosal mac on 06.06.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + + +import Foundation +import UIKit +import Freddy +import Moya +import AlamofireImage + + +class InformationViewController: UITableViewController { + + +} \ No newline at end of file diff --git a/APIClient/Peoples.swift b/APIClient/Peoples.swift new file mode 100644 index 0000000..5a15faa --- /dev/null +++ b/APIClient/Peoples.swift @@ -0,0 +1,33 @@ +// +// Peoples.swift +// APIClient +// +// Created by logosal mac on 06.06.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import Freddy +import UIKit + +// Informationen über Figur + +struct SWPeoples: JSONDecodable { + + let name: String + let gender: String + let height: String + let mass: String + + + // Durchsucht Datei von API nach Schlagwörtern + + init(json: JSON) throws { + self.name = try json.string("name") + self.gender = try json.string("gender") + self.height = try json.string("height") + self.mass = try json.string("mass") + + } + +} diff --git a/APIClient/SWAPI.swift b/APIClient/SWAPI.swift new file mode 100644 index 0000000..a53b637 --- /dev/null +++ b/APIClient/SWAPI.swift @@ -0,0 +1,59 @@ +// +// SWAPI.swift +// APIClient +// +// Created by logosal mac on 06.06.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import Moya +import Freddy + + +enum SWAPI: Moya.TargetType, Cacheable { + + case people(id: Int) + + var baseURL: NSURL { return NSURL(string: "http://swapi.co/api")! } + + var path: String { + switch self { + case .people(id: let id): return "/people/\(id)" + } + } + + var method: Moya.Method { return .GET } + + var parameters: [String : AnyObject]? { + switch self { + default: return nil + } + } + + var sampleData: NSData { + switch self { + default: return "".dataUsingEncoding(NSUTF8StringEncoding)! + } + } + + var cacheIdentifier: String { + return self.path + } + + +} + +struct NamedResource: Freddy.JSONDecodable { + + let name: String + + init(name: String) { + self.name = name + } + + init(json: JSON) throws { + self.name = try json.string("name") + } + +} diff --git a/APIClient/SearchViewController.swift b/APIClient/SearchViewController.swift new file mode 100644 index 0000000..34d6af8 --- /dev/null +++ b/APIClient/SearchViewController.swift @@ -0,0 +1,77 @@ +// +// SearchViewController.swift +// APIClient +// +// Created by logosal mac on 06.06.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import UIKit +import Freddy +import Moya +import AlamofireImage + + +class SearchViewController: UIViewController { + + var starWarsAPI : MoyaProvider! + + var peoples: SWPeoples? { + didSet { + + self.searchTextfield.text = peoples?.name + + } + } + + @IBOutlet weak var searchTextfield: UITextField! + @IBAction func buttonLoad(sender: AnyObject) { + loadPeoples(NamedResource(name: searchTextfield.text ?? "")) + } + + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { + if segue.identifier == "showInformation" { + + guard let InformationViewController = (segue.destinationViewController as? UINavigationController)?.topViewController as? InformationViewController else { + return + } + guard let peoplesInfo = self.peoples else { + return + } + +// TODO: Übergabe + + + } + + } + + func loadPeoples(peoples: Int) { + starWarsAPI.request(.people(id: peoples)) { result in + switch result { + 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 people = try SWPeoples(json: json) + // Configure view according to model + self.peoples = people + } catch { + print(error) + } + case .Failure(let error): + print(error) + } + } + } + +} + + + + + + From 29d48cbeaa6c94e5597622bf19ae6807bfb6b226 Mon Sep 17 00:00:00 2001 From: XXXXX43 Date: Wed, 8 Jun 2016 17:28:02 +0200 Subject: [PATCH 2/5] Input Error --- APIClient/Base.lproj/Main.storyboard | 2 +- APIClient/InformationViewController.swift | 17 +++++++++++++++++ APIClient/SWAPI.swift | 2 +- APIClient/SearchViewController.swift | 7 +++---- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index 611ac54..fd9ae21 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -80,7 +80,7 @@ - + diff --git a/APIClient/InformationViewController.swift b/APIClient/InformationViewController.swift index 1a13687..f551291 100644 --- a/APIClient/InformationViewController.swift +++ b/APIClient/InformationViewController.swift @@ -16,5 +16,22 @@ import AlamofireImage class InformationViewController: UITableViewController { + var peoples : SWPeoples? + + override func numberOfSectionsInTableView(tableView: UITableView) -> Int { + return 1 + } + + override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return 4 + } + + override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { + let cell = tableView.dequeueReusableCellWithIdentifier("personInformation", forIndexPath: indexPath) as! UITableViewCell + + + return cell + } + } \ No newline at end of file diff --git a/APIClient/SWAPI.swift b/APIClient/SWAPI.swift index a53b637..eb5e4cc 100644 --- a/APIClient/SWAPI.swift +++ b/APIClient/SWAPI.swift @@ -19,7 +19,7 @@ enum SWAPI: Moya.TargetType, Cacheable { var path: String { switch self { - case .people(id: let id): return "/people/\(id)" + case .people(let id): return "/people/:\(id)/" } } diff --git a/APIClient/SearchViewController.swift b/APIClient/SearchViewController.swift index 34d6af8..be7bb0a 100644 --- a/APIClient/SearchViewController.swift +++ b/APIClient/SearchViewController.swift @@ -21,19 +21,18 @@ class SearchViewController: UIViewController { didSet { self.searchTextfield.text = peoples?.name - } } @IBOutlet weak var searchTextfield: UITextField! @IBAction func buttonLoad(sender: AnyObject) { - loadPeoples(NamedResource(name: searchTextfield.text ?? "")) + loadPeoples(Int(searchTextfield.text!) ?? 0) } override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == "showInformation" { - guard let InformationViewController = (segue.destinationViewController as? UINavigationController)?.topViewController as? InformationViewController else { + guard let informationViewController = (segue.destinationViewController as? UINavigationController)?.topViewController as? InformationViewController else { return } guard let peoplesInfo = self.peoples else { @@ -41,7 +40,7 @@ class SearchViewController: UIViewController { } // TODO: Übergabe - + informationViewController.peoples = peoplesInfo } From 141fcbb05629b368861e2dc425ccdd3e8df1e8b0 Mon Sep 17 00:00:00 2001 From: XXXXX43 Date: Mon, 13 Jun 2016 19:18:48 +0200 Subject: [PATCH 3/5] still 404 error --- APIClient/AppDelegate.swift | 8 ++ APIClient/Base.lproj/Main.storyboard | 92 +++++++++++++---------- APIClient/Info.plist | 11 +++ APIClient/InformationViewController.swift | 31 ++++---- APIClient/SWAPI.swift | 2 +- APIClient/SearchViewController.swift | 1 + 6 files changed, 92 insertions(+), 53 deletions(-) diff --git a/APIClient/AppDelegate.swift b/APIClient/AppDelegate.swift index 67e6023..0b3ce5e 100644 --- a/APIClient/AppDelegate.swift +++ b/APIClient/AppDelegate.swift @@ -18,8 +18,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + + if let SearchViewController = (window?.rootViewController as? UINavigationController)?.topViewController as? SearchViewController { + + SearchViewController.starWarsAPI = swAPI + + } + return true } + } diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index fd9ae21..0331664 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -44,7 +44,6 @@ - @@ -65,56 +64,71 @@ + - + - - + + - - + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + - + + + + - - + + - + diff --git a/APIClient/Info.plist b/APIClient/Info.plist index 40c6215..8c7d69f 100644 --- a/APIClient/Info.plist +++ b/APIClient/Info.plist @@ -22,6 +22,17 @@ 1 LSRequiresIPhoneOS + NSAppTransportSecurity + + NSExceptionDomains + + swapi.co + + NSExceptionAllowsInsecureHTTPLoads + + + + UILaunchStoryboardName LaunchScreen UIMainStoryboardFile diff --git a/APIClient/InformationViewController.swift b/APIClient/InformationViewController.swift index f551291..22357ed 100644 --- a/APIClient/InformationViewController.swift +++ b/APIClient/InformationViewController.swift @@ -14,24 +14,29 @@ import Moya import AlamofireImage -class InformationViewController: UITableViewController { +class InformationViewController: UIViewController { var peoples : SWPeoples? - override func numberOfSectionsInTableView(tableView: UITableView) -> Int { - return 1 - } + + @IBOutlet weak var name: UILabel! - override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return 4 - } + @IBOutlet weak var gender: UILabel! - override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { - let cell = tableView.dequeueReusableCellWithIdentifier("personInformation", forIndexPath: indexPath) as! UITableViewCell - + @IBOutlet weak var height: UILabel! + + @IBOutlet weak var mass: UILabel! + + override func viewDidLoad() { + super.viewDidLoad() + name.text = peoples?.name + gender.text = peoples?.gender + height.text = peoples?.height + mass.text = peoples?.mass - return cell - } + } + + +} -} \ No newline at end of file diff --git a/APIClient/SWAPI.swift b/APIClient/SWAPI.swift index eb5e4cc..ae9d455 100644 --- a/APIClient/SWAPI.swift +++ b/APIClient/SWAPI.swift @@ -19,7 +19,7 @@ enum SWAPI: Moya.TargetType, Cacheable { var path: String { switch self { - case .people(let id): return "/people/:\(id)/" + case .people(let id): return "/people/\(id)/" } } diff --git a/APIClient/SearchViewController.swift b/APIClient/SearchViewController.swift index be7bb0a..23b0cee 100644 --- a/APIClient/SearchViewController.swift +++ b/APIClient/SearchViewController.swift @@ -58,6 +58,7 @@ class SearchViewController: UIViewController { let people = try SWPeoples(json: json) // Configure view according to model self.peoples = people + self.performSegueWithIdentifier("showInformation", sender: nil) } catch { print(error) } From b697391bb70daa6760b988805023d061d9d38b78 Mon Sep 17 00:00:00 2001 From: XXXXX43 Date: Mon, 20 Jun 2016 17:14:57 +0200 Subject: [PATCH 4/5] finally working :) --- APIClient/Base.lproj/Main.storyboard | 38 ++++++++++++++++------------ APIClient/SearchViewController.swift | 2 +- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index 0331664..4ee327e 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -33,12 +33,6 @@ -