From fa5a5503b8b332f44323156f17c277a7c2ad8772 Mon Sep 17 00:00:00 2001 From: ChrisBtt Date: Tue, 24 May 2016 09:38:06 +0200 Subject: [PATCH 1/4] Leider schon das erste Problem --- APIClient.xcodeproj/project.pbxproj | 4 ++++ APIClient/APIResource.swift | 23 +++++++++++++++++++ APIClient/AppDelegate.swift | 6 +++++ APIClient/Base.lproj/Main.storyboard | 2 +- APIClient/StarWarsViewController.swift | 20 ++++++++++++++++ .../contents.xcworkspacedata | 7 ++++++ 6 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 APIClient/StarWarsViewController.swift create mode 100644 Pods/Pods.xcodeproj/project.xcworkspace/contents.xcworkspacedata diff --git a/APIClient.xcodeproj/project.pbxproj b/APIClient.xcodeproj/project.pbxproj index a549a3b..a5a0b19 100644 --- a/APIClient.xcodeproj/project.pbxproj +++ b/APIClient.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 078B558C6C77F3C6DCDC4678 /* Pods_APIClientUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82531BA6D6228998A00AB5B6 /* Pods_APIClientUITests.framework */; }; + 830DA5871CF437F100203B41 /* StarWarsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 830DA5861CF437F100203B41 /* StarWarsViewController.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 */; }; @@ -39,6 +40,7 @@ 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 = ""; }; 82531BA6D6228998A00AB5B6 /* Pods_APIClientUITests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_APIClientUITests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 830DA5861CF437F100203B41 /* StarWarsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StarWarsViewController.swift; sourceTree = ""; }; 872A275B1CF0D87100A988C4 /* APIResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIResource.swift; sourceTree = ""; }; 874D06551CEF295E009A494D /* APIClient.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = APIClient.app; sourceTree = BUILT_PRODUCTS_DIR; }; 874D06581CEF295E009A494D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -88,6 +90,7 @@ 872A27571CF0D4E200A988C4 /* View Controller */ = { isa = PBXGroup; children = ( + 830DA5861CF437F100203B41 /* StarWarsViewController.swift */, ); name = "View Controller"; sourceTree = ""; @@ -468,6 +471,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 830DA5871CF437F100203B41 /* StarWarsViewController.swift in Sources */, 872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */, 874D06591CEF295E009A494D /* AppDelegate.swift in Sources */, ); diff --git a/APIClient/APIResource.swift b/APIClient/APIResource.swift index 6bb455f..00c9608 100644 --- a/APIClient/APIResource.swift +++ b/APIClient/APIResource.swift @@ -83,6 +83,29 @@ extension MoyaProvider where Target: Cacheable { } +enum YourAPI: Moya.TargetType { + case firstEndpoint (someParameter: String, anotherParameter: Int) + var baseURL:NSURL {return NSURL(string: "http://swapi.co/api/")! } + + var path : String { + switch self { + case .firstEndpoint(someParameter: let a, anotherParameter: _): return "/firstEndpoint/\(a)" + } + } + + var method: Moya.Method {return .GET} + + var parameters: [String : AnyObject]? { + switch self { + default: return nil + } + } + + var sampleData: NSData { + return "".dataUsingEncoding(NSUTF8StringEncoding)! + } +} + /// A persistent cache for resources private let resourceCache = try! Cache(name: "APIResource") diff --git a/APIClient/AppDelegate.swift b/APIClient/AppDelegate.swift index 630c3ed..cad0205 100644 --- a/APIClient/AppDelegate.swift +++ b/APIClient/AppDelegate.swift @@ -13,8 +13,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? + let starWAPI = MoyaProvider() func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + + if let starWarsViewController = (window?.rootViewController as? UINavigationController)?.topViewController as? StarWarsViewController { + + } + return true } diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index 69b2fbf..baa0cf9 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + diff --git a/APIClient/StarWarsViewController.swift b/APIClient/StarWarsViewController.swift new file mode 100644 index 0000000..1b87ef6 --- /dev/null +++ b/APIClient/StarWarsViewController.swift @@ -0,0 +1,20 @@ +// +// StarWarsViewController.swift +// APIClient +// +// Created by Christoph Blattgerste on 24.05.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import UIKit +import Freddy +import Moya +import AlamofireImage + +class StarWarsViewController: UIViewController { + + var starWAPI : MoyaProvider! + + +} \ No newline at end of file diff --git a/Pods/Pods.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Pods/Pods.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/Pods/Pods.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + From 759b72a0367e9a067bc1b2eaf3756cfa2c99a8dd Mon Sep 17 00:00:00 2001 From: ChrisBtt Date: Wed, 25 May 2016 21:26:19 +0200 Subject: [PATCH 2/4] import Moya, Freddy, ... klappt nicht --- APIClient.xcodeproj/project.pbxproj | 4 ++ APIClient/APIResource.swift | 23 -------- APIClient/AppDelegate.swift | 4 +- APIClient/Base.lproj/Main.storyboard | 73 +++++++++++++++++++++++--- APIClient/SWAPI.swift | 43 +++++++++++++++ APIClient/StarWarsViewController.swift | 7 ++- 6 files changed, 121 insertions(+), 33 deletions(-) create mode 100644 APIClient/SWAPI.swift diff --git a/APIClient.xcodeproj/project.pbxproj b/APIClient.xcodeproj/project.pbxproj index a5a0b19..508e552 100644 --- a/APIClient.xcodeproj/project.pbxproj +++ b/APIClient.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 078B558C6C77F3C6DCDC4678 /* Pods_APIClientUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82531BA6D6228998A00AB5B6 /* Pods_APIClientUITests.framework */; }; 830DA5871CF437F100203B41 /* StarWarsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 830DA5861CF437F100203B41 /* StarWarsViewController.swift */; }; + 830DA5891CF57DEE00203B41 /* SWAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 830DA5881CF57DEE00203B41 /* SWAPI.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 */; }; @@ -41,6 +42,7 @@ 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 = ""; }; 82531BA6D6228998A00AB5B6 /* Pods_APIClientUITests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_APIClientUITests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 830DA5861CF437F100203B41 /* StarWarsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StarWarsViewController.swift; sourceTree = ""; }; + 830DA5881CF57DEE00203B41 /* SWAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SWAPI.swift; sourceTree = ""; }; 872A275B1CF0D87100A988C4 /* APIResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIResource.swift; sourceTree = ""; }; 874D06551CEF295E009A494D /* APIClient.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = APIClient.app; sourceTree = BUILT_PRODUCTS_DIR; }; 874D06581CEF295E009A494D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -106,6 +108,7 @@ isa = PBXGroup; children = ( 872A275B1CF0D87100A988C4 /* APIResource.swift */, + 830DA5881CF57DEE00203B41 /* SWAPI.swift */, ); name = Model; sourceTree = ""; @@ -473,6 +476,7 @@ files = ( 830DA5871CF437F100203B41 /* StarWarsViewController.swift in Sources */, 872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */, + 830DA5891CF57DEE00203B41 /* SWAPI.swift in Sources */, 874D06591CEF295E009A494D /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/APIClient/APIResource.swift b/APIClient/APIResource.swift index 00c9608..6bb455f 100644 --- a/APIClient/APIResource.swift +++ b/APIClient/APIResource.swift @@ -83,29 +83,6 @@ extension MoyaProvider where Target: Cacheable { } -enum YourAPI: Moya.TargetType { - case firstEndpoint (someParameter: String, anotherParameter: Int) - var baseURL:NSURL {return NSURL(string: "http://swapi.co/api/")! } - - var path : String { - switch self { - case .firstEndpoint(someParameter: let a, anotherParameter: _): return "/firstEndpoint/\(a)" - } - } - - var method: Moya.Method {return .GET} - - var parameters: [String : AnyObject]? { - switch self { - default: return nil - } - } - - var sampleData: NSData { - return "".dataUsingEncoding(NSUTF8StringEncoding)! - } -} - /// A persistent cache for resources private let resourceCache = try! Cache(name: "APIResource") diff --git a/APIClient/AppDelegate.swift b/APIClient/AppDelegate.swift index cad0205..dae3899 100644 --- a/APIClient/AppDelegate.swift +++ b/APIClient/AppDelegate.swift @@ -7,6 +7,7 @@ // import UIKit +import Moya @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { @@ -18,7 +19,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { if let starWarsViewController = (window?.rootViewController as? UINavigationController)?.topViewController as? StarWarsViewController { - +// API to ViewController + starWarsViewController.starWAPI = starWAPI } return true diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index baa0cf9..a559e2a 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -3,6 +3,7 @@ + @@ -21,24 +22,80 @@ - + - + - - + + - + - + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + - + diff --git a/APIClient/SWAPI.swift b/APIClient/SWAPI.swift new file mode 100644 index 0000000..02bc845 --- /dev/null +++ b/APIClient/SWAPI.swift @@ -0,0 +1,43 @@ +// +// SWAPI.swift +// APIClient +// +// Created by Christoph Blattgerste on 25.05.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import Moya +import Freddy + + +//adress of the used API: ("http://swapi.co/api/") + +enum SWAPI: Moya.TargetType { + + case starship (NamedResource) + case planet (NamedResource) + case species (NamedResource) + + var baseURL: NSURL {return NSURL(string: "http://swapi.co/api/")! } + + var path : String { + switch self { + case .starship(let namedResource): return "/starships/\(namedResource.name)" + case .planet(let namedResource): return "/planets/\(namedResource.name)" + case .species(let namedResource): return "/species/\(namedResource.name)" + } + } + + var method: Moya.Method {return .GET} + + var parameters: [String : AnyObject]? { + switch self { + default: return nil + } + } + + var sampleData: NSData { + return "".dataUsingEncoding(NSUTF8StringEncoding)! + } +} \ No newline at end of file diff --git a/APIClient/StarWarsViewController.swift b/APIClient/StarWarsViewController.swift index 1b87ef6..2ac2fee 100644 --- a/APIClient/StarWarsViewController.swift +++ b/APIClient/StarWarsViewController.swift @@ -6,7 +6,6 @@ // Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. // -import Foundation import UIKit import Freddy import Moya @@ -17,4 +16,10 @@ class StarWarsViewController: UIViewController { var starWAPI : MoyaProvider! + + + @IBOutlet weak var searchTxtField: UITextField! + @IBOutlet weak var starShipLoading: UIActivityIndicatorView! + @IBOutlet weak var starShipLabel: UILabel! + @IBOutlet weak var starShipImage: UIImageView! } \ No newline at end of file From 2e032de6b7f05de39096e6ba60396da7ee7827cd Mon Sep 17 00:00:00 2001 From: ChrisBtt Date: Thu, 2 Jun 2016 18:19:15 +0200 Subject: [PATCH 3/4] =?UTF-8?q?Die=20App=20l=C3=A4uft=20ohne=20Error=20Mel?= =?UTF-8?q?dungen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- APIClient.xcodeproj/project.pbxproj | 4 + APIClient/Base.lproj/Main.storyboard | 150 +++++++++++++++-------- APIClient/SWAPI.swift | 31 ++++- APIClient/StarWarsViewController.swift | 162 ++++++++++++++++++++++++- APIClient/Structs.swift | 53 ++++++++ 5 files changed, 336 insertions(+), 64 deletions(-) create mode 100644 APIClient/Structs.swift diff --git a/APIClient.xcodeproj/project.pbxproj b/APIClient.xcodeproj/project.pbxproj index 508e552..8828983 100644 --- a/APIClient.xcodeproj/project.pbxproj +++ b/APIClient.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 078B558C6C77F3C6DCDC4678 /* Pods_APIClientUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82531BA6D6228998A00AB5B6 /* Pods_APIClientUITests.framework */; }; 830DA5871CF437F100203B41 /* StarWarsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 830DA5861CF437F100203B41 /* StarWarsViewController.swift */; }; 830DA5891CF57DEE00203B41 /* SWAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 830DA5881CF57DEE00203B41 /* SWAPI.swift */; }; + 83229B671CFC842800FF94B7 /* Structs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83229B661CFC842800FF94B7 /* Structs.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 */; }; @@ -43,6 +44,7 @@ 82531BA6D6228998A00AB5B6 /* Pods_APIClientUITests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_APIClientUITests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 830DA5861CF437F100203B41 /* StarWarsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StarWarsViewController.swift; sourceTree = ""; }; 830DA5881CF57DEE00203B41 /* SWAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SWAPI.swift; sourceTree = ""; }; + 83229B661CFC842800FF94B7 /* Structs.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Structs.swift; sourceTree = ""; }; 872A275B1CF0D87100A988C4 /* APIResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIResource.swift; sourceTree = ""; }; 874D06551CEF295E009A494D /* APIClient.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = APIClient.app; sourceTree = BUILT_PRODUCTS_DIR; }; 874D06581CEF295E009A494D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -109,6 +111,7 @@ children = ( 872A275B1CF0D87100A988C4 /* APIResource.swift */, 830DA5881CF57DEE00203B41 /* SWAPI.swift */, + 83229B661CFC842800FF94B7 /* Structs.swift */, ); name = Model; sourceTree = ""; @@ -476,6 +479,7 @@ files = ( 830DA5871CF437F100203B41 /* StarWarsViewController.swift in Sources */, 872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */, + 83229B671CFC842800FF94B7 /* Structs.swift in Sources */, 830DA5891CF57DEE00203B41 /* SWAPI.swift in Sources */, 874D06591CEF295E009A494D /* AppDelegate.swift in Sources */, ); diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index a559e2a..0eafd92 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -3,7 +3,6 @@ - @@ -22,80 +21,127 @@ - + - + - - + + - + - - + + + + - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - + - - - - - - - - - - - + + + + + + + + + + - + diff --git a/APIClient/SWAPI.swift b/APIClient/SWAPI.swift index 02bc845..4cbed61 100644 --- a/APIClient/SWAPI.swift +++ b/APIClient/SWAPI.swift @@ -13,18 +13,18 @@ import Freddy //adress of the used API: ("http://swapi.co/api/") -enum SWAPI: Moya.TargetType { +enum SWAPI: Moya.TargetType, Cacheable { - case starship (NamedResource) - case planet (NamedResource) - case species (NamedResource) + case starships (NamedResource) + case planets (NamedResource) + case species (NamedResource) var baseURL: NSURL {return NSURL(string: "http://swapi.co/api/")! } var path : String { switch self { - case .starship(let namedResource): return "/starships/\(namedResource.name)" - case .planet(let namedResource): return "/planets/\(namedResource.name)" + case .starships(let namedResource): return "/starships/\(namedResource.name)" + case .planets(let namedResource): return "/planets/\(namedResource.name)" case .species(let namedResource): return "/species/\(namedResource.name)" } } @@ -40,4 +40,23 @@ enum SWAPI: Moya.TargetType { var sampleData: NSData { 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") + } + } \ No newline at end of file diff --git a/APIClient/StarWarsViewController.swift b/APIClient/StarWarsViewController.swift index 2ac2fee..c5adf46 100644 --- a/APIClient/StarWarsViewController.swift +++ b/APIClient/StarWarsViewController.swift @@ -11,15 +11,165 @@ import Freddy import Moya import AlamofireImage -class StarWarsViewController: UIViewController { +class StarWarsViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { var starWAPI : MoyaProvider! + var entrieDataSS: Starships? { + didSet { + outputOne.text = entrieDataSS?.starshipClass + outputTwo.text = entrieDataSS?.costInCredits + outputZero.text = entrieDataSS?.name + labelOne.text = "Its class:" + labelTwo.text = "Its costs[credits]:" + labelZero.text = "Starships name:" + } + } + var entrieDataPl: Planets? { + didSet { + outputOne.text = entrieDataPl?.terrain + outputTwo.text = entrieDataPl?.population + outputZero.text = entrieDataPl?.name + labelOne.text = "Its terrain:" + labelTwo.text = "Its population:" + labelZero.text = "Planets name:" + + } + } + var entrieDataSp: Species? { + didSet { + outputZero.text = entrieDataSp?.name + outputOne.text = entrieDataSp?.homeworld + outputTwo.text = entrieDataSp?.language + labelZero.text = "Species name:" + labelOne.text = "Its homeworld:" + labelTwo.text = "Its language:" + } + } + + + @IBOutlet weak var sWPickerView: UIPickerView! + @IBOutlet weak var pickerLabel: UILabel! + @IBOutlet weak var labelOne: UILabel! + @IBOutlet weak var labelTwo: UILabel! + @IBOutlet weak var labelZero: UILabel! + @IBOutlet weak var outputOne: UILabel! + @IBOutlet weak var outputTwo: UILabel! + @IBOutlet weak var outputZero: UILabel! + @IBOutlet weak var numberSelection: UITextField! + @IBOutlet weak var loadingIndicator: UIActivityIndicatorView! + + @IBAction func actNumberSelection(sender: AnyObject) { + sender.resignFirstResponder() + switch selectedEndpoint { + case "Starship": + loadStarship(NamedResource(name: String(numberSelection))) + case "Planet": + loadPlanet(NamedResource(name: String(numberSelection))) + case "Specie": + loadSpecies(NamedResource(name: String(numberSelection))) + default: + break + } + } + + let pickerData = ["Starship","Planet","Specie"] + + private var selectedEndpoint = "" + override func viewDidLoad() { + super.viewDidLoad() + self.sWPickerView.dataSource = self + self.sWPickerView.delegate = self + self.entrieDataSS = nil + self.entrieDataSS = nil + self.entrieDataSp = nil + } - @IBOutlet weak var searchTxtField: UITextField! - @IBOutlet weak var starShipLoading: UIActivityIndicatorView! - @IBOutlet weak var starShipLabel: UILabel! - @IBOutlet weak var starShipImage: UIImageView! -} \ No newline at end of file +// Functions to initialize UIPickerView + func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { + return 1 + } + func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { + return pickerData.count + } + func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { + return pickerData[row] + } + func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { + pickerLabel.text = "Select a \(pickerData[row]):" + selectedEndpoint = pickerData[row] + } + +// load selected Endpoint from API URL +// depending on PickerWheel selection + func loadStarship(starship: NamedResource) { + loadingIndicator.startAnimating() + starWAPI.request(.starships(starship)) { result in + self.loadingIndicator.stopAnimating() + 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 star_ship = try Starships(json: json) + // Configure view according to model + self.entrieDataSS = star_ship + } catch { + print(error) + } + case .Failure(let error): + print(error) + } + } + } + func loadPlanet(planet: NamedResource) { + loadingIndicator.startAnimating() + starWAPI.request(.planets(planet)) { result in + self.loadingIndicator.stopAnimating() + 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 pla_net = try Planets(json: json) + // Configure view according to model + self.entrieDataPl = pla_net + } catch { + print(error) + } + case .Failure(let error): + print(error) + } + } + } + + func loadSpecies(specie: NamedResource) { + loadingIndicator.startAnimating() + starWAPI.request(.species(specie)) { result in + self.loadingIndicator.stopAnimating() + 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 spe_cies = try Species(json: json) + // Configure view according to model + self.entrieDataSp = spe_cies + } catch { + print(error) + } + case .Failure(let error): + print(error) + } + } + } + +} diff --git a/APIClient/Structs.swift b/APIClient/Structs.swift new file mode 100644 index 0000000..8af2d22 --- /dev/null +++ b/APIClient/Structs.swift @@ -0,0 +1,53 @@ +// +// Structs.swift +// APIClient +// +// Created by Christoph Blattgerste on 30.05.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import Freddy +import UIKit + +struct Starships: JSONDecodable { + + let name : String + + let starshipClass : String + + let costInCredits : String + + init (json: JSON) throws { + self.name = try json.string("name") + self.costInCredits = try json.string("cost_in_credits") + self.starshipClass = try json.string("starship_class") + } +} + +struct Planets: JSONDecodable { + + let name : String + let population : String + let terrain : String + + init(json: JSON) throws { + self.name = try json.string("name") + self.population = try json.string("population") + self.terrain = try json.string("terrain") + } +} + +struct Species: JSONDecodable { + + let name : String + let homeworld : String + let language : String + + init(json: JSON) throws { + self.name = try json.string("name") + self.language = try json.string("language") + self.homeworld = try json.string("homeworld") + } + +} From 53d1de5c4355c65f7e7136c17ef2ec7c55bf1bed Mon Sep 17 00:00:00 2001 From: ChrisBtt Date: Mon, 6 Jun 2016 18:59:32 +0200 Subject: [PATCH 4/4] Geht --- APIClient.xcodeproj/project.pbxproj | 7 + .../Datatest.xcdatamodel/contents | 7 + APIClient/Base.lproj/Main.storyboard | 162 ++++++++++-------- APIClient/Info.plist | 11 ++ APIClient/SWAPI.swift | 32 +++- APIClient/StarWarsViewController.swift | 37 ++-- APIClientTests/MyAPITests.swift | 32 ++++ 7 files changed, 200 insertions(+), 88 deletions(-) create mode 100644 APIClient/.xcdatamodeld/Datatest.xcdatamodel/contents create mode 100644 APIClientTests/MyAPITests.swift diff --git a/APIClient.xcodeproj/project.pbxproj b/APIClient.xcodeproj/project.pbxproj index 8828983..16d0b2e 100644 --- a/APIClient.xcodeproj/project.pbxproj +++ b/APIClient.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 830DA5871CF437F100203B41 /* StarWarsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 830DA5861CF437F100203B41 /* StarWarsViewController.swift */; }; 830DA5891CF57DEE00203B41 /* SWAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 830DA5881CF57DEE00203B41 /* SWAPI.swift */; }; 83229B671CFC842800FF94B7 /* Structs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83229B661CFC842800FF94B7 /* Structs.swift */; }; + 835E5DB91D0440A300FD7CC3 /* MyAPITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 835E5DB81D0440A300FD7CC3 /* MyAPITests.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 */; }; @@ -45,6 +46,7 @@ 830DA5861CF437F100203B41 /* StarWarsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StarWarsViewController.swift; sourceTree = ""; }; 830DA5881CF57DEE00203B41 /* SWAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SWAPI.swift; sourceTree = ""; }; 83229B661CFC842800FF94B7 /* Structs.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Structs.swift; sourceTree = ""; }; + 835E5DB81D0440A300FD7CC3 /* MyAPITests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MyAPITests.swift; sourceTree = ""; }; 872A275B1CF0D87100A988C4 /* APIResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIResource.swift; sourceTree = ""; }; 874D06551CEF295E009A494D /* APIClient.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = APIClient.app; sourceTree = BUILT_PRODUCTS_DIR; }; 874D06581CEF295E009A494D /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; @@ -165,6 +167,7 @@ isa = PBXGroup; children = ( 87EC71531CFA380F00FBDDDE /* Info.plist */, + 835E5DB81D0440A300FD7CC3 /* MyAPITests.swift */, ); path = APIClientTests; sourceTree = ""; @@ -489,6 +492,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 835E5DB91D0440A300FD7CC3 /* MyAPITests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -648,10 +652,12 @@ baseConfigurationReference = F8B9811B1068B05E73195990 /* Pods-APIClientTests.debug.xcconfig */; buildSettings = { BUNDLE_LOADER = "$(TEST_HOST)"; + CLANG_ENABLE_MODULES = YES; INFOPLIST_FILE = APIClientTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "de.uni-heidelberg.ios-dev-kurs.APIClientTests"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/APIClient.app/APIClient"; }; name = Debug; @@ -661,6 +667,7 @@ baseConfigurationReference = 65C820A0F52E3181871F5ECA /* Pods-APIClientTests.release.xcconfig */; buildSettings = { BUNDLE_LOADER = "$(TEST_HOST)"; + CLANG_ENABLE_MODULES = YES; INFOPLIST_FILE = APIClientTests/Info.plist; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "de.uni-heidelberg.ios-dev-kurs.APIClientTests"; diff --git a/APIClient/.xcdatamodeld/Datatest.xcdatamodel/contents b/APIClient/.xcdatamodeld/Datatest.xcdatamodel/contents new file mode 100644 index 0000000..ff64d29 --- /dev/null +++ b/APIClient/.xcdatamodeld/Datatest.xcdatamodel/contents @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index 0eafd92..1eb6f98 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -26,117 +26,139 @@ - - + + - + - - - - + + - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - + + - - - + + - - - + + - - + + + + + + + + + + + + - - - - - - - - - - + + + + + + + + + + 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/SWAPI.swift b/APIClient/SWAPI.swift index 4cbed61..c4315fb 100644 --- a/APIClient/SWAPI.swift +++ b/APIClient/SWAPI.swift @@ -19,7 +19,7 @@ enum SWAPI: Moya.TargetType, Cacheable { case planets (NamedResource) case species (NamedResource) - var baseURL: NSURL {return NSURL(string: "http://swapi.co/api/")! } + var baseURL: NSURL {return NSURL(string: "http://swapi.co/api")! } var path : String { switch self { @@ -38,7 +38,35 @@ enum SWAPI: Moya.TargetType, Cacheable { } var sampleData: NSData { - return "".dataUsingEncoding(NSUTF8StringEncoding)! + switch self { +// case .starships(let namedResource): +// return +// "name": "CR90 corvette", +// "model": "CR90 corvette", +// "manufacturer": "Corellian Engineering Corporation", +// "cost_in_credits": "3500000", +// "length": "150", +// "max_atmosphering_speed": "950", +// "crew": "165", +// "passengers": "600", +// "cargo_capacity": "3000000", +// "consumables": "1 year", +// "hyperdrive_rating": "2.0", +// "MGLT": "60", +// "starship_class": "corvette", +// "pilots": [], +// "films": [ +// "http://swapi.co/api/films/6/", +// "http://swapi.co/api/films/3/", +// "http://swapi.co/api/films/1/" +// ], +// "created": "2014-12-10T14:20:33.369000Z", +// "edited": "2014-12-22T17:35:45.408368Z", +// "url": "http://swapi.co/api/starships/2/" +// .dataUsingEncoding(NSUTF8StringEncoding)! + default: + return "".dataUsingEncoding(NSUTF8StringEncoding)! + } } var cacheIdentifier: String { diff --git a/APIClient/StarWarsViewController.swift b/APIClient/StarWarsViewController.swift index c5adf46..2642bbb 100644 --- a/APIClient/StarWarsViewController.swift +++ b/APIClient/StarWarsViewController.swift @@ -11,7 +11,18 @@ import Freddy import Moya import AlamofireImage -class StarWarsViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { +class StarWarsViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate { + + override func viewDidLoad() { + super.viewDidLoad() + self.sWPickerView.dataSource = self + self.sWPickerView.delegate = self + self.entrieDataSS = nil + self.entrieDataSS = nil + self.entrieDataSp = nil + pickerLabel.text = "Select a \(selectedEndpoint):" + numberSelection.delegate = self + } var starWAPI : MoyaProvider! @@ -63,29 +74,22 @@ class StarWarsViewController: UIViewController, UIPickerViewDataSource, UIPicker sender.resignFirstResponder() switch selectedEndpoint { case "Starship": - loadStarship(NamedResource(name: String(numberSelection))) + loadStarship(NamedResource(name: numberSelection.text ?? "")) case "Planet": - loadPlanet(NamedResource(name: String(numberSelection))) - case "Specie": - loadSpecies(NamedResource(name: String(numberSelection))) + loadPlanet(NamedResource(name: numberSelection.text ?? "")) + case "Species": + loadSpecies(NamedResource(name: numberSelection.text ?? "")) default: break } } - let pickerData = ["Starship","Planet","Specie"] + let pickerData = ["Starship","Planet","Species"] - private var selectedEndpoint = "" + private var selectedEndpoint = "Starship" + - override func viewDidLoad() { - super.viewDidLoad() - self.sWPickerView.dataSource = self - self.sWPickerView.delegate = self - self.entrieDataSS = nil - self.entrieDataSS = nil - self.entrieDataSp = nil - } // Functions to initialize UIPickerView func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { @@ -98,8 +102,9 @@ class StarWarsViewController: UIViewController, UIPickerViewDataSource, UIPicker return pickerData[row] } func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { - pickerLabel.text = "Select a \(pickerData[row]):" +// pickerLabel.text = "Select a \(pickerData[row]):" selectedEndpoint = pickerData[row] + pickerLabel.text = "Select a \(selectedEndpoint):" } // load selected Endpoint from API URL diff --git a/APIClientTests/MyAPITests.swift b/APIClientTests/MyAPITests.swift new file mode 100644 index 0000000..f531e69 --- /dev/null +++ b/APIClientTests/MyAPITests.swift @@ -0,0 +1,32 @@ +// +// MyAPITests.swift +// APIClient +// +// Created by Christoph Blattgerste on 05.06.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation + +import XCTest + +@testable import APIClient + +import Nimblek0 + +class MyAPITests : XCTestCase { + override func setUp() { + super.setUp() + } + override func tearDown() { + super.tearDown() + } +//eigene Test Funktionen + func testFirstTry() { + expect(1+1) == 2 + expect( + } + +} + +