Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fehler #2

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions APIClient.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
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 */; };
A0A3FC001CF38D8C00FC6A8C /* PlanetViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0A3FBFF1CF38D8C00FC6A8C /* PlanetViewController.swift */; };
A0A3FC021CF38F6B00FC6A8C /* PlanetAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0A3FC011CF38F6B00FC6A8C /* PlanetAPI.swift */; };
A0A3FC061CF399A400FC6A8C /* Planet.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0A3FC051CF399A400FC6A8C /* Planet.swift */; };
A526FDEE5BF2D1BC3C97B9FA /* Pods_APIClient.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BD8A5B3E1266D5298CABE44A /* Pods_APIClient.framework */; };
/* End PBXBuildFile section */

Expand All @@ -24,6 +27,9 @@
874D06621CEF295E009A494D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
874D06641CEF295E009A494D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
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 = "<group>"; };
A0A3FBFF1CF38D8C00FC6A8C /* PlanetViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlanetViewController.swift; sourceTree = "<group>"; };
A0A3FC011CF38F6B00FC6A8C /* PlanetAPI.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PlanetAPI.swift; sourceTree = "<group>"; };
A0A3FC051CF399A400FC6A8C /* Planet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Planet.swift; sourceTree = "<group>"; };
BD8A5B3E1266D5298CABE44A /* Pods_APIClient.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_APIClient.framework; sourceTree = BUILT_PRODUCTS_DIR; };
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 = "<group>"; };
/* End PBXFileReference section */
Expand All @@ -43,6 +49,7 @@
872A27571CF0D4E200A988C4 /* View Controller */ = {
isa = PBXGroup;
children = (
A0A3FBFF1CF38D8C00FC6A8C /* PlanetViewController.swift */,
);
name = "View Controller";
sourceTree = "<group>";
Expand All @@ -58,6 +65,8 @@
isa = PBXGroup;
children = (
872A275B1CF0D87100A988C4 /* APIResource.swift */,
A0A3FC011CF38F6B00FC6A8C /* PlanetAPI.swift */,
A0A3FC051CF399A400FC6A8C /* Planet.swift */,
);
name = Model;
sourceTree = "<group>";
Expand Down Expand Up @@ -242,8 +251,11 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A0A3FC021CF38F6B00FC6A8C /* PlanetAPI.swift in Sources */,
872A275C1CF0D87100A988C4 /* APIResource.swift in Sources */,
874D06591CEF295E009A494D /* AppDelegate.swift in Sources */,
A0A3FC001CF38D8C00FC6A8C /* PlanetViewController.swift in Sources */,
A0A3FC061CF399A400FC6A8C /* Planet.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
10 changes: 10 additions & 0 deletions APIClient/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@
//

import UIKit
import Moya

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

let planetAPI = MoyaProvider<PlanetAPI>()


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

if let planetViewController = (window?.rootViewController as? UINavigationController)?.topViewController as? PlanetViewController{

planetViewController.planetAPI = planetAPI

}

return true
}

Expand Down
42 changes: 32 additions & 10 deletions APIClient/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,46 @@
</objects>
<point key="canvasLocation" x="-658" y="301"/>
</scene>
<!--View Controller-->
<!--Planet View Controller-->
<scene sceneID="adh-8k-Iuh">
<objects>
<viewController id="rPr-Fp-ALE" sceneMemberID="viewController">
<viewController id="rPr-Fp-ALE" customClass="PlanetViewController" customModule="APIClient" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="pj8-zF-1z6"/>
<viewControllerLayoutGuide type="bottom" id="kt2-9S-3TK"/>
<viewControllerLayoutGuide type="top" id="g8M-74-ZTL"/>
<viewControllerLayoutGuide type="bottom" id="awQ-VP-YWW"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="rEB-Za-Zhx">
<stackView key="view" opaque="NO" contentMode="scaleToFill" axis="vertical" id="WyT-AH-7qH">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
<navigationItem key="navigationItem" id="CZR-l9-xMg"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Planet" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Uoy-yF-OJ2">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="highlightedColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</label>
</subviews>
</stackView>
<navigationItem key="navigationItem" id="CZR-l9-xMg">
<nil key="title"/>
<textField key="titleView" opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" text="Search Planets in Star Wars" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" id="aZK-x1-BeE">
<rect key="frame" x="180" y="7" width="240" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="rPr-Fp-ALE" id="J7k-gc-VuH"/>
</connections>
</textField>
</navigationItem>
<connections>
<outlet property="planetLab" destination="Uoy-yF-OJ2" id="rCZ-Uo-aDa"/>
<outlet property="searchField" destination="aZK-x1-BeE" id="A9N-sa-ivp"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="OaC-W5-AeM" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="65" y="301"/>
<point key="canvasLocation" x="77" y="301"/>
</scene>
</scenes>
</document>
11 changes: 11 additions & 0 deletions APIClient/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,16 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>swapi.co</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
</dict>
</plist>
20 changes: 20 additions & 0 deletions APIClient/Planet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Planet.swift
// APIClient
//
// Created by Kleimaier, Dennis on 23.05.16.
// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved.
//

import Foundation
import Freddy

struct Planet: JSONDecodable {

let name: String

init(json: JSON) throws {
self.name = try json.string("name")
}

}
51 changes: 51 additions & 0 deletions APIClient/PlanetAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// PlanetAPI.swift
// APIClient
//
// Created by Kleimaier, Dennis on 23.05.16.
// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved.
//

import Foundation
import Moya
import Freddy

enum PlanetAPI: Moya.TargetType{
case planet(NamedResource<Planet>)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Du kannst hier auch eine NamedResource<Planet> statt eines name: String als Associated Value verwenden, damit wird die API noch besser modelliert ;)

var baseURL: NSURL { return NSURL(string: "http://swapi.co/api")! }

var path: String {
switch self{
case .planet(let namedResource): return "/planets/(namedResource)"
}
Copy link
Member

@nilsvu nilsvu May 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hier fehlt ein \, so ist das nur ein String, und kein Wert wird eingesetzt ;) Daher kommt momentan immer der Status Code 404 zurück.

        case .planet(let namedResource): return "/planets/\(namedResource.name)"

}

var method: Moya.Method { return .GET }

var parameters: [String : AnyObject]? {
switch self {
default: return nil
}
}

// TODO: Provide sample data for testing
var sampleData: NSData {
switch self {
default: return "".dataUsingEncoding(NSUTF8StringEncoding)!
}
}
}

struct NamedResource<Resource: Freddy.JSONDecodable>: Freddy.JSONDecodable {

let name:String

init(name: String) {
self.name = name
}

init(json: JSON) throws {
self.name = try json.string("name")
}
}
64 changes: 64 additions & 0 deletions APIClient/PlanetViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// PlanetViewController.swift
// APIClient
//
// Created by Kleimaier, Dennis on 23.05.16.
// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved.
//

import Foundation
import Moya
import Freddy

class PlanetViewController: UIViewController {


override func viewDidLoad() {
super.viewDidLoad()
}

var planetAPI: MoyaProvider<PlanetAPI>!
var planet: Planet? {
didSet {
self.planetLab.text = planet?.name
}
}


@IBOutlet weak var searchField: UITextField!
@IBOutlet weak var planetLab: UILabel!


func loadPlanet(planet: NamedResource<Planet>){
planetAPI.request(.planet(planet)){ 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 pokemonSpecies = try Planet(json: json)
// Configure view according to model
self.planet = pokemonSpecies
print(pokemonSpecies)
} catch {
print(error)
}
case .Failure(let error):
print(error)
}
}
}




func textFieldShouldReturn(textField: UITextField) -> Bool {
guard let name = textField.text else {
return true
}
self.loadPlanet(NamedResource(name: name))
return true
}
}