소스 검색

Added download request sample to iOS example app.

kengo Yokoyama 10 년 전
부모
커밋
e0dd9e8441
3개의 변경된 파일59개의 추가작업 그리고 15개의 파일을 삭제
  1. 15 13
      Example/Base.lproj/Main.storyboard
  2. 36 2
      Example/DetailViewController.swift
  3. 8 0
      Example/MasterViewController.swift

+ 15 - 13
Example/Base.lproj/Main.storyboard

@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6245" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="H1p-Uh-vWS">
+<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7706" systemVersion="14E46" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="H1p-Uh-vWS">
     <dependencies>
     <dependencies>
-        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6238"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
     </dependencies>
     </dependencies>
     <scenes>
     <scenes>
         <!--Master-->
         <!--Master-->
@@ -40,7 +40,7 @@
                     <tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="static" style="grouped" separatorStyle="default" rowHeight="44" sectionHeaderHeight="10" sectionFooterHeight="10" id="r7i-6Z-zg0">
                     <tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="static" style="grouped" separatorStyle="default" rowHeight="44" sectionHeaderHeight="10" sectionFooterHeight="10" id="r7i-6Z-zg0">
                         <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
                         <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
-                        <color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
+                        <color key="backgroundColor" red="0.93725490196078431" green="0.93725490196078431" blue="0.95686274509803926" alpha="1" colorSpace="calibratedRGB"/>
                         <sections>
                         <sections>
                             <tableViewSection headerTitle="Data" id="8cQ-ii-Dz7">
                             <tableViewSection headerTitle="Data" id="8cQ-ii-Dz7">
                                 <cells>
                                 <cells>
@@ -55,7 +55,7 @@
                                                     <rect key="frame" x="15" y="0.0" width="290" height="43"/>
                                                     <rect key="frame" x="15" y="0.0" width="290" height="43"/>
                                                     <autoresizingMask key="autoresizingMask"/>
                                                     <autoresizingMask key="autoresizingMask"/>
                                                     <fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
                                                     <fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
-                                                    <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
+                                                    <color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
                                                     <color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                                     <color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                                 </label>
                                                 </label>
                                             </subviews>
                                             </subviews>
@@ -76,7 +76,7 @@
                                                     <rect key="frame" x="15" y="0.0" width="290" height="43"/>
                                                     <rect key="frame" x="15" y="0.0" width="290" height="43"/>
                                                     <autoresizingMask key="autoresizingMask"/>
                                                     <autoresizingMask key="autoresizingMask"/>
                                                     <fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
                                                     <fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
-                                                    <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
+                                                    <color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
                                                     <color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                                     <color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                                 </label>
                                                 </label>
                                             </subviews>
                                             </subviews>
@@ -97,7 +97,7 @@
                                                     <rect key="frame" x="15" y="0.0" width="290" height="43"/>
                                                     <rect key="frame" x="15" y="0.0" width="290" height="43"/>
                                                     <autoresizingMask key="autoresizingMask"/>
                                                     <autoresizingMask key="autoresizingMask"/>
                                                     <fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
                                                     <fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
-                                                    <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
+                                                    <color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
                                                     <color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                                     <color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                                 </label>
                                                 </label>
                                             </subviews>
                                             </subviews>
@@ -118,7 +118,7 @@
                                                     <rect key="frame" x="15" y="0.0" width="290" height="43"/>
                                                     <rect key="frame" x="15" y="0.0" width="290" height="43"/>
                                                     <autoresizingMask key="autoresizingMask"/>
                                                     <autoresizingMask key="autoresizingMask"/>
                                                     <fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
                                                     <fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
-                                                    <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
+                                                    <color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
                                                     <color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                                     <color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                                 </label>
                                                 </label>
                                             </subviews>
                                             </subviews>
@@ -190,7 +190,7 @@
                             </tableViewSection>
                             </tableViewSection>
                             <tableViewSection headerTitle="Download" id="7nc-cQ-nUY">
                             <tableViewSection headerTitle="Download" id="7nc-cQ-nUY">
                                 <cells>
                                 <cells>
-                                    <tableViewCell contentMode="scaleToFill" selectionStyle="none" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="Cell" textLabel="Gr3-i1-tdE" style="IBUITableViewCellStyleDefault" id="khw-Sk-LOc">
+                                    <tableViewCell contentMode="scaleToFill" selectionStyle="blue" accessoryType="disclosureIndicator" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="Cell" textLabel="Gr3-i1-tdE" style="IBUITableViewCellStyleDefault" id="khw-Sk-LOc">
                                         <rect key="frame" x="0.0" y="86" width="320" height="44"/>
                                         <rect key="frame" x="0.0" y="86" width="320" height="44"/>
                                         <autoresizingMask key="autoresizingMask"/>
                                         <autoresizingMask key="autoresizingMask"/>
                                         <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="khw-Sk-LOc" id="HO7-NM-pbP">
                                         <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="khw-Sk-LOc" id="HO7-NM-pbP">
@@ -201,12 +201,14 @@
                                                     <rect key="frame" x="15" y="0.0" width="290" height="43"/>
                                                     <rect key="frame" x="15" y="0.0" width="290" height="43"/>
                                                     <autoresizingMask key="autoresizingMask"/>
                                                     <autoresizingMask key="autoresizingMask"/>
                                                     <fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
                                                     <fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
-                                                    <color key="textColor" white="0.66666666666666663" alpha="1" colorSpace="calibratedWhite"/>
                                                     <color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                                     <color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
                                                 </label>
                                                 </label>
                                             </subviews>
                                             </subviews>
                                         </tableViewCellContentView>
                                         </tableViewCellContentView>
                                         <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                                         <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
+                                        <connections>
+                                            <segue destination="vC3-pB-5Vb" kind="showDetail" identifier="DOWNLOAD" id="R3Y-Dw-Jtc"/>
+                                        </connections>
                                     </tableViewCell>
                                     </tableViewCell>
                                     <tableViewCell contentMode="scaleToFill" selectionStyle="none" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="Cell" textLabel="fmT-qD-bf2" style="IBUITableViewCellStyleDefault" id="8hK-B8-VMy">
                                     <tableViewCell contentMode="scaleToFill" selectionStyle="none" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" reuseIdentifier="Cell" textLabel="fmT-qD-bf2" style="IBUITableViewCellStyleDefault" id="8hK-B8-VMy">
                                         <rect key="frame" x="0.0" y="86" width="320" height="44"/>
                                         <rect key="frame" x="0.0" y="86" width="320" height="44"/>
@@ -269,7 +271,7 @@
                     <tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="grouped" separatorStyle="default" allowsSelection="NO" rowHeight="44" sectionHeaderHeight="10" sectionFooterHeight="10" id="uo9-Sd-Gpr">
                     <tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="grouped" separatorStyle="default" allowsSelection="NO" rowHeight="44" sectionHeaderHeight="10" sectionFooterHeight="10" id="uo9-Sd-Gpr">
                         <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
                         <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                         <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
-                        <color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
+                        <color key="backgroundColor" red="0.93725490196078431" green="0.93725490196078431" blue="0.95686274509803926" alpha="1" colorSpace="calibratedRGB"/>
                         <prototypes>
                         <prototypes>
                             <tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Header" textLabel="CVi-D2-cds" detailTextLabel="Umi-gS-7r0" style="IBUITableViewCellStyleValue1" id="tsM-dO-McZ">
                             <tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="Header" textLabel="CVi-D2-cds" detailTextLabel="Umi-gS-7r0" style="IBUITableViewCellStyleValue1" id="tsM-dO-McZ">
                                 <autoresizingMask key="autoresizingMask"/>
                                 <autoresizingMask key="autoresizingMask"/>
@@ -279,7 +281,7 @@
                                         <label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Accept" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="CVi-D2-cds">
                                         <label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="Accept" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="CVi-D2-cds">
                                             <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                                             <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                                             <fontDescription key="fontDescription" type="system" pointSize="16"/>
                                             <fontDescription key="fontDescription" type="system" pointSize="16"/>
-                                            <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
+                                            <color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
                                             <nil key="highlightedColor"/>
                                             <nil key="highlightedColor"/>
                                         </label>
                                         </label>
                                         <label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="&quot;text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8&quot;" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="Umi-gS-7r0">
                                         <label opaque="NO" multipleTouchEnabled="YES" contentMode="left" text="&quot;text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8&quot;" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="Umi-gS-7r0">
@@ -300,7 +302,7 @@
                                             <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                                             <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                                             <string key="text">Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.</string>
                                             <string key="text">Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.</string>
                                             <fontDescription key="fontDescription" name="Courier" family="Courier" pointSize="12"/>
                                             <fontDescription key="fontDescription" name="Courier" family="Courier" pointSize="12"/>
-                                            <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
+                                            <color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
                                             <nil key="highlightedColor"/>
                                             <nil key="highlightedColor"/>
                                         </label>
                                         </label>
                                     </subviews>
                                     </subviews>
@@ -326,7 +328,7 @@
         <image name="Logo" width="250" height="40"/>
         <image name="Logo" width="250" height="40"/>
     </resources>
     </resources>
     <inferredMetricsTieBreakers>
     <inferredMetricsTieBreakers>
-        <segue reference="FoU-a6-nga"/>
+        <segue reference="R3Y-Dw-Jtc"/>
     </inferredMetricsTieBreakers>
     </inferredMetricsTieBreakers>
     <color key="tintColor" red="0.84313732385635376" green="0.25882354378700256" blue="0.11372549831867218" alpha="1" colorSpace="deviceRGB"/>
     <color key="tintColor" red="0.84313732385635376" green="0.25882354378700256" blue="0.11372549831867218" alpha="1" colorSpace="deviceRGB"/>
 </document>
 </document>

+ 36 - 2
Example/DetailViewController.swift

@@ -43,7 +43,8 @@ class DetailViewController: UITableViewController {
     var headers: [String: String] = [:]
     var headers: [String: String] = [:]
     var body: String?
     var body: String?
     var elapsedTime: NSTimeInterval?
     var elapsedTime: NSTimeInterval?
-
+    var segueIdentifier: String?
+    
     override func awakeFromNib() {
     override func awakeFromNib() {
         super.awakeFromNib()
         super.awakeFromNib()
         self.refreshControl?.addTarget(self, action: "refresh", forControlEvents: .ValueChanged)
         self.refreshControl?.addTarget(self, action: "refresh", forControlEvents: .ValueChanged)
@@ -76,13 +77,46 @@ class DetailViewController: UITableViewController {
                 self.headers["\(field)"] = "\(value)"
                 self.headers["\(field)"] = "\(value)"
             }
             }
 
 
-            self.body = body
+            if let segueIdentifier = self.segueIdentifier {
+                switch segueIdentifier {
+                case "GET", "POST", "PUT", "DELETE":
+                    self.body = body
+                case "DOWNLOAD":
+                    self.body = self.downloadedBodyString()
+                default:
+                    break
+                }
+            }
 
 
             self.tableView.reloadData()
             self.tableView.reloadData()
             self.refreshControl?.endRefreshing()
             self.refreshControl?.endRefreshing()
         }
         }
     }
     }
 
 
+    private func downloadedBodyString() -> String {
+        let fileManager = NSFileManager.defaultManager()
+        let cachesDirectory = fileManager.URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask)[0] as! NSURL
+
+        if let
+            contents = fileManager.contentsOfDirectoryAtURL(
+                cachesDirectory,
+                includingPropertiesForKeys: nil,
+                options: .SkipsHiddenFiles,
+                error: nil
+            ),
+            fileURL = contents.first as? NSURL,
+            data = NSData(contentsOfURL: fileURL),
+            json: AnyObject = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil),
+            prettyData = NSJSONSerialization.dataWithJSONObject(json, options: .PrettyPrinted, error: nil),
+            prettyString = NSString(data: prettyData, encoding: NSUTF8StringEncoding) as? String
+        {
+            fileManager.removeItemAtURL(fileURL, error: nil)
+            return prettyString
+        }
+
+        return ""
+    }
+
     // MARK: UITableViewDataSource
     // MARK: UITableViewDataSource
 
 
     override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

+ 8 - 0
Example/MasterViewController.swift

@@ -54,13 +54,21 @@ class MasterViewController: UITableViewController {
             func requestForSegue(segue: UIStoryboardSegue) -> Request? {
             func requestForSegue(segue: UIStoryboardSegue) -> Request? {
                 switch segue.identifier as String! {
                 switch segue.identifier as String! {
                     case "GET":
                     case "GET":
+                        detailViewController.segueIdentifier = "GET"
                         return Alamofire.request(.GET, "http://httpbin.org/get")
                         return Alamofire.request(.GET, "http://httpbin.org/get")
                     case "POST":
                     case "POST":
+                        detailViewController.segueIdentifier = "POST"
                         return Alamofire.request(.POST, "http://httpbin.org/post")
                         return Alamofire.request(.POST, "http://httpbin.org/post")
                     case "PUT":
                     case "PUT":
+                        detailViewController.segueIdentifier = "PUT"
                         return Alamofire.request(.PUT, "http://httpbin.org/put")
                         return Alamofire.request(.PUT, "http://httpbin.org/put")
                     case "DELETE":
                     case "DELETE":
+                        detailViewController.segueIdentifier = "DELETE"
                         return Alamofire.request(.DELETE, "http://httpbin.org/delete")
                         return Alamofire.request(.DELETE, "http://httpbin.org/delete")
+                    case "DOWNLOAD":
+                        detailViewController.segueIdentifier = "DOWNLOAD"
+                        let destination = Alamofire.Request.suggestedDownloadDestination(directory: .CachesDirectory, domain: .UserDomainMask)
+                        return Alamofire.download(.GET, "http://httpbin.org/stream/1", destination)                    
                     default:
                     default:
                         return nil
                         return nil
                 }
                 }