Skip to content

Commit

Permalink
Web API へのリクエスト処理を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
Kazuya Tateishi committed Mar 25, 2015
1 parent 26eb3a8 commit 5b67d41
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
4 changes: 4 additions & 0 deletions APIDemo/APIDemo.xcodeproj/project.pbxproj
Expand Up @@ -15,6 +15,7 @@
8593AE031AC2E3ED00305B8B /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 8593AE011AC2E3ED00305B8B /* LaunchScreen.xib */; };
8593AE0F1AC2E3ED00305B8B /* APIDemoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8593AE0E1AC2E3ED00305B8B /* APIDemoTests.swift */; };
8593AE1A1AC2E57A00305B8B /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8593AE191AC2E57A00305B8B /* User.swift */; };
8593AE1C1AC2E63E00305B8B /* Router.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8593AE1B1AC2E63E00305B8B /* Router.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -42,6 +43,7 @@
8593AE0D1AC2E3ED00305B8B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8593AE0E1AC2E3ED00305B8B /* APIDemoTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIDemoTests.swift; sourceTree = "<group>"; };
8593AE191AC2E57A00305B8B /* User.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = User.swift; sourceTree = "<group>"; };
8593AE1B1AC2E63E00305B8B /* Router.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Router.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -110,6 +112,7 @@
8593ADFF1AC2E3ED00305B8B /* Images.xcassets */,
8593AE011AC2E3ED00305B8B /* LaunchScreen.xib */,
8593ADF61AC2E3ED00305B8B /* Supporting Files */,
8593AE1B1AC2E63E00305B8B /* Router.swift */,
);
path = APIDemo;
sourceTree = "<group>";
Expand Down Expand Up @@ -298,6 +301,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
8593AE1C1AC2E63E00305B8B /* Router.swift in Sources */,
8593ADFB1AC2E3ED00305B8B /* ViewController.swift in Sources */,
8593ADF91AC2E3ED00305B8B /* AppDelegate.swift in Sources */,
8593AE1A1AC2E57A00305B8B /* User.swift in Sources */,
Expand Down
31 changes: 31 additions & 0 deletions APIDemo/APIDemo/Router.swift
@@ -0,0 +1,31 @@
//
// Router.swift
// APIDemo
//
// Created by Kazuya Tateishi on 2015/03/25.
// Copyright (c) 2015年 kzy52. All rights reserved.
//

import Foundation

import Alamofire

enum Router: URLRequestConvertible {
static let baseURLString = "http://localhost:3000"

case GetUsers()

var URLRequest: NSURLRequest {
let (method: Alamofire.Method, path: String, parameters: [String: AnyObject]?) = {
switch self {
case .GetUsers: return (.GET, "/api/v1/users", nil)
}
}()

let URL = NSURL(string: Router.baseURLString)!
let URLRequest = NSURLRequest(URL: URL.URLByAppendingPathComponent(path))
let encoding = Alamofire.ParameterEncoding.URL

return encoding.encode(URLRequest, parameters: parameters).0
}
}
27 changes: 22 additions & 5 deletions APIDemo/APIDemo/ViewController.swift
Expand Up @@ -7,6 +7,7 @@
//

import UIKit
import Alamofire

// UITableViewを使用する際はUITableViewDataSourceプロトコルとUITableViewDelegateプロトコルを実装する必要がある
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
Expand Down Expand Up @@ -39,6 +40,10 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
self.view.addSubview(self.tableView!)
}

override func viewWillAppear(animated: Bool) {
self.request()
}

// セルの総数を返す(表示するテーブルの行数)
// UITableViewDataSource を使う場合は 必須
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
Expand All @@ -58,11 +63,23 @@ class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSour
return cell
}

// 行が選択された際の処理
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let user = self.users[indexPath.row] as User
// 行選択された際にログにメールアドレスを表示してみる
println(user.email);
// Web API をコールする
func request() {
Alamofire.request(Router.GetUsers()).responseJSON { (request, response, json, error) -> Void in
if let json = json as? Array<Dictionary<String,AnyObject>> {
for j in json {
var user: User = User(
name: j["name"] as NSString,
email: j["email"] as NSString
)
self.users.set(user)
}

dispatch_async(dispatch_get_main_queue(), {
self.tableView!.reloadData()
})
}
}
}

override func didReceiveMemoryWarning() {
Expand Down

0 comments on commit 5b67d41

Please sign in to comment.