From d9b47ffb764952e40e2a279df3a2d69776dd66a4 Mon Sep 17 00:00:00 2001 From: lucasmllr Date: Mon, 5 Sep 2016 16:28:58 +0200 Subject: [PATCH 1/2] country api client. Gibt name, hauptstadt und population aus. --- APIClient/CapitalViewController.swift | 46 +++++++++++++++++++ APIClient/country.swift | 9 ++++ APIClient/countryAPI.swift | 9 ++++ tryingOutMoya.playground/Contents.swift | 5 ++ .../contents.xcplayground | 4 ++ 5 files changed, 73 insertions(+) create mode 100644 APIClient/CapitalViewController.swift create mode 100644 APIClient/country.swift create mode 100644 APIClient/countryAPI.swift create mode 100644 tryingOutMoya.playground/Contents.swift create mode 100644 tryingOutMoya.playground/contents.xcplayground diff --git a/APIClient/CapitalViewController.swift b/APIClient/CapitalViewController.swift new file mode 100644 index 0000000..488aa1f --- /dev/null +++ b/APIClient/CapitalViewController.swift @@ -0,0 +1,46 @@ +// +// testViewController.swift +// APIClient +// +// Created by Lucas Moeller on 23.08.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation +import UIKit +import Freddy +import Moya +import Alamofire + +class CapitalViewController: UIViewController { + + var provider: MoyaProvider! + + func getCapitalOf(Country: String) -> (Capital: String?, error: ErrorType?){ + var returnCapital: String? = nil + var returnError: ErrorType? = nil + provider.request(.fullName(name: Country)) {result in + switch result { + case let .Success(moyaResponse): + let data = moyaResponse.data + guard let json = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) else { + break + } + guard let jsonArray = json as? [String: AnyObject] else { + break + } + guard let capital = jsonArray["capital"] as? String else { + break + } + returnCapital = capital + case let .Failure(error): + print("Error") + } + } + return (returnCapital, returnError) + } + + + +} + diff --git a/APIClient/country.swift b/APIClient/country.swift new file mode 100644 index 0000000..002cd6a --- /dev/null +++ b/APIClient/country.swift @@ -0,0 +1,9 @@ +// +// country.swift +// APIClient +// +// Created by Lucas Moeller on 22.08.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation diff --git a/APIClient/countryAPI.swift b/APIClient/countryAPI.swift new file mode 100644 index 0000000..80ed2ab --- /dev/null +++ b/APIClient/countryAPI.swift @@ -0,0 +1,9 @@ +// +// countryAPI.swift +// APIClient +// +// Created by Lucas Moeller on 22.08.16. +// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved. +// + +import Foundation diff --git a/tryingOutMoya.playground/Contents.swift b/tryingOutMoya.playground/Contents.swift new file mode 100644 index 0000000..c9a6742 --- /dev/null +++ b/tryingOutMoya.playground/Contents.swift @@ -0,0 +1,5 @@ +//: Playground - noun: a place where people can play + +import UIKit + +var str = "Hello, playground" diff --git a/tryingOutMoya.playground/contents.xcplayground b/tryingOutMoya.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/tryingOutMoya.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file From 8e92f555cf47f446a672060e38a78aaea8152a8f Mon Sep 17 00:00:00 2001 From: lucasmllr Date: Mon, 5 Sep 2016 16:34:06 +0200 Subject: [PATCH 2/2] added all --- APIClient.xcodeproj/project.pbxproj | 20 +++- APIClient/AppDelegate.swift | 6 + APIClient/Base.lproj/Main.storyboard | 106 ++++++++++++++++-- APIClient/CapitalViewController.swift | 86 +++++++++++--- APIClient/country.swift | 19 ++++ APIClient/countryAPI.swift | 35 ++++++ tryingOutMoya.playground/Contents.swift | 3 +- .../contents.xcworkspacedata | 7 ++ 8 files changed, 255 insertions(+), 27 deletions(-) create mode 100644 tryingOutMoya.playground/playground.xcworkspace/contents.xcworkspacedata diff --git a/APIClient.xcodeproj/project.pbxproj b/APIClient.xcodeproj/project.pbxproj index a549a3b..7298b4d 100644 --- a/APIClient.xcodeproj/project.pbxproj +++ b/APIClient.xcodeproj/project.pbxproj @@ -8,12 +8,15 @@ /* Begin PBXBuildFile section */ 078B558C6C77F3C6DCDC4678 /* Pods_APIClientUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82531BA6D6228998A00AB5B6 /* Pods_APIClientUITests.framework */; }; - 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 */; }; 874D06601CEF295E009A494D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 874D065F1CEF295E009A494D /* Assets.xcassets */; }; 874D06631CEF295E009A494D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 874D06611CEF295E009A494D /* LaunchScreen.storyboard */; }; A526FDEE5BF2D1BC3C97B9FA /* Pods_APIClient.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BD8A5B3E1266D5298CABE44A /* Pods_APIClient.framework */; }; + E905C2CD1D6B8D3200DE0FBC /* countryAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = E905C2CC1D6B8D3200DE0FBC /* countryAPI.swift */; }; + E905C2CF1D6B9D9A00DE0FBC /* country.swift in Sources */ = {isa = PBXBuildFile; fileRef = E905C2CE1D6B9D9A00DE0FBC /* country.swift */; }; + E905C2D11D6CEDD600DE0FBC /* CapitalViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E905C2D01D6CEDD600DE0FBC /* CapitalViewController.swift */; }; + E905C2D31D770DF300DE0FBC /* APIResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = E905C2D21D770DF300DE0FBC /* APIResource.swift */; }; E9294E7F05F2671186BD1359 /* Pods_APIClientTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4FFA0A7AA37697BD5CBC3211 /* Pods_APIClientTests.framework */; }; /* End PBXBuildFile section */ @@ -39,7 +42,6 @@ 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; }; - 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 = ""; }; 874D065D1CEF295E009A494D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; @@ -53,6 +55,10 @@ 93EFAB13AE62F1B6FB2683B2 /* Pods-APIClient.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-APIClient.debug.xcconfig"; path = "Pods/Target Support Files/Pods-APIClient/Pods-APIClient.debug.xcconfig"; sourceTree = ""; }; BD8A5B3E1266D5298CABE44A /* Pods_APIClient.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_APIClient.framework; sourceTree = BUILT_PRODUCTS_DIR; }; C67B3B3E7990BE278400974F /* Pods-APIClientUITests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-APIClientUITests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-APIClientUITests/Pods-APIClientUITests.debug.xcconfig"; sourceTree = ""; }; + E905C2CC1D6B8D3200DE0FBC /* countryAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = countryAPI.swift; sourceTree = ""; }; + E905C2CE1D6B9D9A00DE0FBC /* country.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = country.swift; sourceTree = ""; }; + E905C2D01D6CEDD600DE0FBC /* CapitalViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CapitalViewController.swift; sourceTree = ""; }; + E905C2D21D770DF300DE0FBC /* APIResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = APIResource.swift; sourceTree = ""; }; F8B9811B1068B05E73195990 /* Pods-APIClientTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-APIClientTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-APIClientTests/Pods-APIClientTests.debug.xcconfig"; sourceTree = ""; }; FFE13D543A95D28F27CFBDA4 /* Pods-APIClient.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-APIClient.release.xcconfig"; path = "Pods/Target Support Files/Pods-APIClient/Pods-APIClient.release.xcconfig"; sourceTree = ""; }; /* End PBXFileReference section */ @@ -88,6 +94,7 @@ 872A27571CF0D4E200A988C4 /* View Controller */ = { isa = PBXGroup; children = ( + E905C2D01D6CEDD600DE0FBC /* CapitalViewController.swift */, ); name = "View Controller"; sourceTree = ""; @@ -102,7 +109,9 @@ 872A27591CF0D64A00A988C4 /* Model */ = { isa = PBXGroup; children = ( - 872A275B1CF0D87100A988C4 /* APIResource.swift */, + E905C2D21D770DF300DE0FBC /* APIResource.swift */, + E905C2CC1D6B8D3200DE0FBC /* countryAPI.swift */, + E905C2CE1D6B9D9A00DE0FBC /* country.swift */, ); name = Model; sourceTree = ""; @@ -468,7 +477,10 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */, + E905C2CF1D6B9D9A00DE0FBC /* country.swift in Sources */, + E905C2CD1D6B8D3200DE0FBC /* countryAPI.swift in Sources */, + E905C2D11D6CEDD600DE0FBC /* CapitalViewController.swift in Sources */, + E905C2D31D770DF300DE0FBC /* APIResource.swift in Sources */, 874D06591CEF295E009A494D /* AppDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/APIClient/AppDelegate.swift b/APIClient/AppDelegate.swift index 630c3ed..f777145 100644 --- a/APIClient/AppDelegate.swift +++ b/APIClient/AppDelegate.swift @@ -7,14 +7,20 @@ // import UIKit +import Moya @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? + let provider = MoyaProvider() func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + + if let countryViewController = (window?.rootViewController as? UINavigationController)?.topViewController as? CountryViewController { + countryViewController.provider = provider + } return true } diff --git a/APIClient/Base.lproj/Main.storyboard b/APIClient/Base.lproj/Main.storyboard index 69b2fbf..9d4ac55 100644 --- a/APIClient/Base.lproj/Main.storyboard +++ b/APIClient/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - - + + @@ -19,26 +19,116 @@ - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + diff --git a/APIClient/CapitalViewController.swift b/APIClient/CapitalViewController.swift index 488aa1f..b6c43d9 100644 --- a/APIClient/CapitalViewController.swift +++ b/APIClient/CapitalViewController.swift @@ -12,35 +12,95 @@ import Freddy import Moya import Alamofire -class CapitalViewController: UIViewController { +class CountryViewController: UIViewController { var provider: MoyaProvider! + + var countryToDisplay: Country? + + @IBOutlet var countryLabel: UILabel! + @IBOutlet var CapitalLabel: UILabel! + @IBOutlet var populationLabel: UILabel! - func getCapitalOf(Country: String) -> (Capital: String?, error: ErrorType?){ - var returnCapital: String? = nil - var returnError: ErrorType? = nil + @IBOutlet var showCountryLabel: UILabel! + @IBOutlet var showCapitalLabel: UILabel! + @IBOutlet var showPopulationLabel: UILabel! + + @IBOutlet var searchField: UITextField! + + @IBAction func searchButtonPressed(sender: AnyObject) { + if let countryToFind = searchField.text { + findCountry(countryToFind) + } else { + searchField.text = "please enter a country" + } + } + /* + func getCapitalOf(Country: String) { provider.request(.fullName(name: Country)) {result in switch result { case let .Success(moyaResponse): let data = moyaResponse.data - guard let json = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) else { - break - } - guard let jsonArray = json as? [String: AnyObject] else { - break + var json:AnyObject? + + do + { + json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) + print(json) + } catch { + print(error) } - guard let capital = jsonArray["capital"] as? String else { + + guard let jArray = json as? [NSMutableDictionary] + else { + print("format error") + break + } + + guard let capital = jArray[0]["capital"] as? String else { + print("capital not a String") break } - returnCapital = capital + self.capitalLabel.text = capital + case let .Failure(error): - print("Error") + print(error) } } - return (returnCapital, returnError) } + */ + func findCountry(CountryToFind: String) { + provider.request(.fullName(name: CountryToFind)) {result in + switch result { + case let .Success(moyaResponse): + let data = moyaResponse.data + do { + let json = try JSON(data: data) + let someCountry = try json.array().map(Country.init)[0] + self.countryToDisplay = someCountry + self.displayCountry(self.countryToDisplay) + } + catch { + print(error) + } + + case let .Failure(error): + print(error) + } + } + } + func displayCountry(countryToDisplay: Country?) { + if countryToDisplay != nil { + showCountryLabel.text = countryToDisplay!.name + showCapitalLabel.text = countryToDisplay!.capital + showPopulationLabel.text = String(countryToDisplay!.population) + } else { + searchField.text = "no country to display" + } + } + } diff --git a/APIClient/country.swift b/APIClient/country.swift index 002cd6a..793e6e0 100644 --- a/APIClient/country.swift +++ b/APIClient/country.swift @@ -7,3 +7,22 @@ // import Foundation +import Freddy +import UIKit + +public struct Country { + + let name: String + let languages: [JSON] + let population: Int + let capital: String +} + +extension Country: JSONDecodable { + public init(json: JSON) throws { + name = try json.string("name") + languages = try json.array("languages") + population = try json.int("population") + capital = try json.string("capital") + } +} \ No newline at end of file diff --git a/APIClient/countryAPI.swift b/APIClient/countryAPI.swift index 80ed2ab..ebb10ff 100644 --- a/APIClient/countryAPI.swift +++ b/APIClient/countryAPI.swift @@ -7,3 +7,38 @@ // import Foundation +import Moya +import Freddy + +enum countryAPI: Moya.TargetType, Cacheable { + + case fullName(name: String) + + var baseURL: NSURL {return NSURL(string: "https://restcountries.eu/rest/v1")!} + + var path: String { + switch self { + case .fullName(let countryName): return "/name/\(countryName)" //fullText=true + } + } + + var method: Moya.Method { + return .GET + } + + var parameters: [String : AnyObject]? {return nil} + + var sampleData: NSData { + switch self { + case .fullName(let countryName): return "[{\"name\": \(countryName),\"capital\": \"Paris\",\"altSpellings\": [\"FR\",\"French Republic\",\"République française\"],\"relevance\": \"2.5\",\"region\": \"Europe\",\"subregion\": \"Western Europe\",\"translations\": {\"de\": \"Frankreich\",\"es\": \"Francia\",\"fr\": \"France\",\"ja\": \"フランス\",\"it\": \"Francia\"},\"population\": 66186000,\"latlng\": [46,2],\"demonym\": \"French\",\"area\": 640679,\"gini\": 32.7,\"timezones\": [\"UTC−10:00\",\"UTC−09:30\",\"UTC−09:00\",\"UTC−08:00\",\"UTC−04:00\",\"UTC−03:00\",\"UTC+01:00\",\"UTC+03:00\",\"UTC+04:00\",\"UTC+05:00\",\"UTC+11:00\",\"UTC+12:00\"],\"borders\": [\"AND\",\"BEL\",\"DEU\",\"ITA\",\"LUX\",\"MCO\",\"ESP\",\"CHE\"],\"nativeName\":\"France\",\"callingCodes\": [\"33\"],\"topLevelDomain\": [\".fr\"],\"alpha2Code\": \"FR\",\"alpha3Code\":\"FRA\",\"currencies\": [\"EUR\"],\"languages\": [\"fr\"]}]".dataUsingEncoding(NSUTF8StringEncoding)! + default: return "".dataUsingEncoding(NSUTF8StringEncoding)! + } + } + + var cacheIdentifier: String { + return self.path + } +} + + + diff --git a/tryingOutMoya.playground/Contents.swift b/tryingOutMoya.playground/Contents.swift index c9a6742..0ee12f5 100644 --- a/tryingOutMoya.playground/Contents.swift +++ b/tryingOutMoya.playground/Contents.swift @@ -1,5 +1,4 @@ //: Playground - noun: a place where people can play import UIKit - -var str = "Hello, playground" +import Moya diff --git a/tryingOutMoya.playground/playground.xcworkspace/contents.xcworkspacedata b/tryingOutMoya.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/tryingOutMoya.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + +