jazzy 0.0.14 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +6 -4
- data/README.md +2 -2
- data/jazzy.gemspec +1 -0
- data/lib/jazzy/assets/css/github_syntax_highlighting.css.scss +142 -0
- data/lib/jazzy/assets/css/jazzy.css.scss +321 -0
- data/lib/jazzy/config.rb +1 -0
- data/lib/jazzy/doc.mustache +1 -0
- data/lib/jazzy/doc_builder.rb +30 -22
- data/lib/jazzy/gem_version.rb +1 -1
- data/lib/jazzy/jazzy_markdown.rb +27 -1
- data/lib/jazzy/partials/task.mustache +7 -0
- data/lib/jazzy/readme_generator.rb +35 -0
- data/lib/jazzy/source_declaration.rb +1 -0
- data/lib/jazzy/source_mark.rb +31 -0
- data/lib/jazzy/sourcekitten.rb +7 -0
- data/logo.jpg +0 -0
- data/spec/integration_specs/document_alamofire/after/docs/Classes.html +15 -4
- data/spec/integration_specs/document_alamofire/after/docs/Classes/Manager.html +14 -4
- data/spec/integration_specs/document_alamofire/after/docs/Classes/Manager/init(configuration:).html +4 -4
- data/spec/integration_specs/document_alamofire/after/docs/Classes/Request.html +40 -4
- data/spec/integration_specs/document_alamofire/after/docs/Enums.html +5 -4
- data/spec/integration_specs/document_alamofire/after/docs/Enums/ParameterEncoding.html +4 -4
- data/spec/integration_specs/document_alamofire/after/docs/Extensions.html +112 -4
- data/spec/integration_specs/document_alamofire/after/docs/Extensions/Manager.html +18 -3
- data/spec/integration_specs/document_alamofire/after/docs/Extensions/Request.html +4 -4
- data/spec/integration_specs/document_alamofire/after/docs/Functions.html +66 -3
- data/spec/integration_specs/document_alamofire/after/docs/Global Variables.html +4 -4
- data/spec/integration_specs/document_alamofire/after/docs/Protocols.html +20 -3
- data/spec/integration_specs/document_alamofire/after/docs/Protocols/URLRequestConvertible.html +4 -4
- data/spec/integration_specs/document_alamofire/after/docs/Protocols/URLStringConvertible.html +4 -4
- data/spec/integration_specs/document_alamofire/after/docs/css/github_syntax_highlighting.css +98 -0
- data/spec/integration_specs/document_alamofire/after/docs/css/jazzy.css +250 -0
- data/spec/integration_specs/document_alamofire/after/docs/index.html +572 -1
- data/spec/integration_specs/misc_jazzy_features/after/docs/Classes.html +7 -3
- data/spec/integration_specs/misc_jazzy_features/after/docs/Classes/ImplicitlyInternalTopLevelClass.html +4 -4
- data/spec/integration_specs/misc_jazzy_features/after/docs/Enums.html +7 -3
- data/spec/integration_specs/misc_jazzy_features/after/docs/Enums/DocumentedEnum.html +4 -4
- data/spec/integration_specs/misc_jazzy_features/after/docs/Global Variables.html +6 -3
- data/spec/integration_specs/misc_jazzy_features/after/docs/css/github_syntax_highlighting.css +98 -0
- data/spec/integration_specs/misc_jazzy_features/after/docs/css/jazzy.css +250 -0
- data/spec/integration_specs/misc_jazzy_features/after/docs/index.html +4 -1
- data/spec/integration_specs/misc_jazzy_features/before/docs/css/jazzy.css +48 -0
- metadata +22 -2
@@ -3,6 +3,7 @@
|
|
3
3
|
<head>
|
4
4
|
<title>Alamofire Reference</title>
|
5
5
|
<link rel="stylesheet" type="text/css" href="css/jazzy.css" />
|
6
|
+
<link rel="stylesheet" type="text/css" href="css/github_syntax_highlighting.css" />
|
6
7
|
<meta charset='utf-8'>
|
7
8
|
<script src="js/jquery.min.js" defer></script>
|
8
9
|
<script src="js/jazzy.js" defer></script>
|
@@ -159,7 +160,577 @@
|
|
159
160
|
<h1 class="chapter-name">Alamofire</h1>
|
160
161
|
<section>
|
161
162
|
<section class="section">
|
162
|
-
<p class="para">
|
163
|
+
<div class='readme'><p class="para"><img src="https://raw.githubusercontent.com/Alamofire/Alamofire/assets/alamofire.png" alt="Alamofire: Elegant Networking in Swift"></p><p class="para">Alamofire is an HTTP networking library written in Swift, from the <a href="https://github.com/mattt">creator</a> of <a href="https://github.com/afnetworking/afnetworking">AFNetworking</a>.</p><a href='#features' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h2 id='features'>Features</h2>
|
164
|
+
|
165
|
+
<ul>
|
166
|
+
<li>[x] Chainable Request / Response methods</li>
|
167
|
+
<li>[x] URL / JSON / plist Parameter Encoding</li>
|
168
|
+
<li>[x] Upload File / Data / Stream</li>
|
169
|
+
<li>[x] Download using Request or Resume data</li>
|
170
|
+
<li>[x] Authentication with NSURLCredential</li>
|
171
|
+
<li>[x] HTTP Response Validation</li>
|
172
|
+
<li>[x] Progress Closure & NSProgress</li>
|
173
|
+
<li>[x] cURL Debug Output</li>
|
174
|
+
<li>[x] Comprehensive Unit Test Coverage</li>
|
175
|
+
<li>[x] Complete Documentation</li>
|
176
|
+
</ul>
|
177
|
+
<a href='#requirements' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h2 id='requirements'>Requirements</h2>
|
178
|
+
|
179
|
+
<ul>
|
180
|
+
<li>iOS 7.0+ / Mac OS X 10.9+</li>
|
181
|
+
<li>Xcode 6.1</li>
|
182
|
+
</ul>
|
183
|
+
<a href='#communication' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h2 id='communication'>Communication</h2>
|
184
|
+
|
185
|
+
<ul>
|
186
|
+
<li>If you <strong>need help</strong>, use <a href="http://stackoverflow.com/questions/tagged/alamofire">Stack Overflow</a>. (Tag ‘alamofire’)</li>
|
187
|
+
<li>If you’d like to <strong>ask a general question</strong>, use <a href="http://stackoverflow.com/questions/tagged/alamofire">Stack Overflow</a>.</li>
|
188
|
+
<li>If you <strong>found a bug</strong>, open an issue.</li>
|
189
|
+
<li>If you <strong>have a feature request</strong>, open an issue.</li>
|
190
|
+
<li>If you <strong>want to contribute</strong>, submit a pull request.</li>
|
191
|
+
</ul>
|
192
|
+
<a href='#installation' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h2 id='installation'>Installation</h2>
|
193
|
+
|
194
|
+
<blockquote>
|
195
|
+
<p class="para">For application targets that do not support embedded frameworks, such as iOS 7, Alamofire can be integrated by including the <code>Alamofire.swift</code> source file directly, and making the appropriate changes to calling semantics.</p></blockquote>
|
196
|
+
<p class="para"><em>Due to the current lack of <a href="http://cocoapods.org">proper infrastructure</a> for Swift dependency management, using Alamofire in your project requires the following steps:</em></p>
|
197
|
+
<ol>
|
198
|
+
<li>Add Alamofire as a <a href="http://git-scm.com/docs/git-submodule">submodule</a> by opening the Terminal, <code>cd</code>-ing into your top-level project directory, and entering the command <code>git submodule add https://github.com/Alamofire/Alamofire.git</code></li>
|
199
|
+
<li>Open the <code>Alamofire</code> folder, and drag <code>Alamofire.xcodeproj</code> into the file navigator of your app project.</li>
|
200
|
+
<li>In Xcode, navigate to the target configuration window by clicking on the blue project icon, and selecting the application target under the <q>Targets</q> heading in the sidebar.</li>
|
201
|
+
<li>Ensure that the deployment target of Alamofire.framework matches that of the application target.</li>
|
202
|
+
<li>In the tab bar at the top of that window, open the <q>Build Phases</q> panel.</li>
|
203
|
+
<li>Expand the <q>Target Dependencies</q> group, and add <code>Alamofire.framework</code>.</li>
|
204
|
+
<li>Click on the <code>+</code> button at the top left of the panel and select <q>New Copy Files Phase</q>. Rename this new phase to <q>Copy Frameworks</q>, set the <q>Destination</q> to <q>Frameworks</q>, and add <code>Alamofire.framework</code>.</li>
|
205
|
+
</ol>
|
206
|
+
|
207
|
+
<hr>
|
208
|
+
<a href='#usage' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h2 id='usage'>Usage</h2>
|
209
|
+
<a href='#making_a_request' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='making_a_request'>Making a Request</h3>
|
210
|
+
<pre><code class="highlight swift"><span class="kd">import</span> <span class="kt">Alamofire</span>
|
211
|
+
|
212
|
+
<span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"http://httpbin.org/get"</span><span class="p">)</span>
|
213
|
+
</code></pre>
|
214
|
+
<a href='#response_handling' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='response_handling'>Response Handling</h3>
|
215
|
+
<pre><code class="highlight swift"><span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"http://httpbin.org/get"</span><span class="p">,</span> <span class="nv">parameters</span><span class="p">:</span> <span class="p">[</span><span class="s">"foo"</span><span class="p">:</span> <span class="s">"bar"</span><span class="p">])</span>
|
216
|
+
<span class="o">.</span><span class="n">response</span> <span class="p">{</span> <span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span> <span class="k">in</span>
|
217
|
+
<span class="nf">println</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
|
218
|
+
<span class="nf">println</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
|
219
|
+
<span class="nf">println</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
|
220
|
+
<span class="p">}</span>
|
221
|
+
</code></pre>
|
222
|
+
|
223
|
+
<blockquote>
|
224
|
+
<p class="para">Networking in Alamofire is done <em>asynchronously</em>. Asynchronous programming may be a source of frustration to programmers unfamiliar with the concept, but there are <a href="https://developer.apple.com/library/ios/qa/qa1693/_index.html">very good reasons</a> for doing it this way.</p><p class="para">Rather than blocking execution to wait for a response from the server, a <a href="http://en.wikipedia.org/wiki/Callback_%28computer_programming%29">callback</a> is specified to handle the response once it’s received. The result of a request is only available inside the scope of a response handler. Any execution contingent on the response or data received from the server must be done within a handler.</p></blockquote>
|
225
|
+
<a href='#response_serialization' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='response_serialization'>Response Serialization</h3>
|
226
|
+
<p class="para"><strong>Built-in Response Methods</strong></p>
|
227
|
+
<ul>
|
228
|
+
<li><code>response()</code></li>
|
229
|
+
<li><code>responseString(encoding: NSStringEncoding)</code></li>
|
230
|
+
<li><code>responseJSON(options: NSJSONReadingOptions)</code></li>
|
231
|
+
<li><code>responsePropertyList(options: NSPropertyListReadOptions)</code></li>
|
232
|
+
</ul>
|
233
|
+
<a href='#response_string_handler' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='response_string_handler'>Response String Handler</h4>
|
234
|
+
<pre><code class="highlight swift"><span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"http://httpbin.org/get"</span><span class="p">)</span>
|
235
|
+
<span class="o">.</span><span class="n">responseString</span> <span class="p">{</span> <span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">string</span><span class="p">,</span> <span class="n">_</span><span class="p">)</span> <span class="k">in</span>
|
236
|
+
<span class="nf">println</span><span class="p">(</span><span class="n">string</span><span class="p">)</span>
|
237
|
+
<span class="p">}</span>
|
238
|
+
</code></pre>
|
239
|
+
<a href='#response_json_handler' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='response_json_handler'>Response JSON Handler</h4>
|
240
|
+
<pre><code class="highlight swift"><span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"http://httpbin.org/get"</span><span class="p">)</span>
|
241
|
+
<span class="o">.</span><span class="n">responseJSON</span> <span class="p">{</span> <span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="kt">JSON</span><span class="p">,</span> <span class="n">_</span><span class="p">)</span> <span class="k">in</span>
|
242
|
+
<span class="nf">println</span><span class="p">(</span><span class="kt">JSON</span><span class="p">)</span>
|
243
|
+
<span class="p">}</span>
|
244
|
+
</code></pre>
|
245
|
+
<a href='#chained_response_handlers' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='chained_response_handlers'>Chained Response Handlers</h4>
|
246
|
+
<p class="para">Response handlers can even be chained:</p><pre><code class="highlight swift"><span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"http://httpbin.org/get"</span><span class="p">)</span>
|
247
|
+
<span class="o">.</span><span class="n">responseString</span> <span class="p">{</span> <span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">string</span><span class="p">,</span> <span class="n">_</span><span class="p">)</span> <span class="k">in</span>
|
248
|
+
<span class="nf">println</span><span class="p">(</span><span class="n">string</span><span class="p">)</span>
|
249
|
+
<span class="p">}</span>
|
250
|
+
<span class="o">.</span><span class="n">responseJSON</span> <span class="p">{</span> <span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="kt">JSON</span><span class="p">,</span> <span class="n">_</span><span class="p">)</span> <span class="k">in</span>
|
251
|
+
<span class="nf">println</span><span class="p">(</span><span class="kt">JSON</span><span class="p">)</span>
|
252
|
+
<span class="p">}</span>
|
253
|
+
</code></pre>
|
254
|
+
<a href='#http_methods' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='http_methods'>HTTP Methods</h3>
|
255
|
+
<p class="para"><code>Alamofire.Method</code> lists the HTTP methods defined in <a href="http://tools.ietf.org/html/rfc7231#section-4.3">RFC 7231 §4.3</a>:</p><pre><code class="highlight swift"><span class="kd">public</span> <span class="kd">enum</span> <span class="kt">Method</span><span class="p">:</span> <span class="kt">String</span> <span class="p">{</span>
|
256
|
+
<span class="k">case</span> <span class="kt">OPTIONS</span> <span class="o">=</span> <span class="s">"OPTIONS"</span>
|
257
|
+
<span class="k">case</span> <span class="kt">GET</span> <span class="o">=</span> <span class="s">"GET"</span>
|
258
|
+
<span class="k">case</span> <span class="kt">HEAD</span> <span class="o">=</span> <span class="s">"HEAD"</span>
|
259
|
+
<span class="k">case</span> <span class="kt">POST</span> <span class="o">=</span> <span class="s">"POST"</span>
|
260
|
+
<span class="k">case</span> <span class="kt">PUT</span> <span class="o">=</span> <span class="s">"PUT"</span>
|
261
|
+
<span class="k">case</span> <span class="kt">PATCH</span> <span class="o">=</span> <span class="s">"PATCH"</span>
|
262
|
+
<span class="k">case</span> <span class="kt">DELETE</span> <span class="o">=</span> <span class="s">"DELETE"</span>
|
263
|
+
<span class="k">case</span> <span class="kt">TRACE</span> <span class="o">=</span> <span class="s">"TRACE"</span>
|
264
|
+
<span class="k">case</span> <span class="kt">CONNECT</span> <span class="o">=</span> <span class="s">"CONNECT"</span>
|
265
|
+
<span class="p">}</span>
|
266
|
+
</code></pre>
|
267
|
+
<p class="para">These values can be passed as the first argument of the <code>Alamofire.request</code> method:</p><pre><code class="highlight swift"><span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">POST</span><span class="p">,</span> <span class="s">"http://httpbin.org/post"</span><span class="p">)</span>
|
268
|
+
|
269
|
+
<span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">PUT</span><span class="p">,</span> <span class="s">"http://httpbin.org/put"</span><span class="p">)</span>
|
270
|
+
|
271
|
+
<span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">DELETE</span><span class="p">,</span> <span class="s">"http://httpbin.org/delete"</span><span class="p">)</span>
|
272
|
+
</code></pre>
|
273
|
+
<a href='#parameters' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='parameters'>Parameters</h3>
|
274
|
+
<a href='#get_request_with_url_encoded_parameters' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='get_request_with_url_encoded_parameters'>GET Request With URL-Encoded Parameters</h4>
|
275
|
+
<pre><code class="highlight swift"><span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"http://httpbin.org/get"</span><span class="p">,</span> <span class="nv">parameters</span><span class="p">:</span> <span class="p">[</span><span class="s">"foo"</span><span class="p">:</span> <span class="s">"bar"</span><span class="p">])</span>
|
276
|
+
<span class="c1">// http://httpbin.org/get?foo=bar
|
277
|
+
</span></code></pre>
|
278
|
+
<a href='#post_request_with_url_encoded_parameters' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='post_request_with_url_encoded_parameters'>POST Request With URL-Encoded Parameters</h4>
|
279
|
+
<pre><code class="highlight swift"><span class="k">let</span> <span class="nv">parameters</span> <span class="o">=</span> <span class="p">[</span>
|
280
|
+
<span class="s">"foo"</span><span class="p">:</span> <span class="s">"bar"</span><span class="p">,</span>
|
281
|
+
<span class="s">"baz"</span><span class="p">:</span> <span class="p">[</span><span class="s">"a"</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span>
|
282
|
+
<span class="s">"qux"</span><span class="p">:</span> <span class="p">[</span>
|
283
|
+
<span class="s">"x"</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
|
284
|
+
<span class="s">"y"</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
|
285
|
+
<span class="s">"z"</span><span class="p">:</span> <span class="mi">3</span>
|
286
|
+
<span class="p">]</span>
|
287
|
+
<span class="p">]</span>
|
288
|
+
|
289
|
+
<span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">POST</span><span class="p">,</span> <span class="s">"http://httpbin.org/post"</span><span class="p">,</span> <span class="nv">parameters</span><span class="p">:</span> <span class="n">parameters</span><span class="p">)</span>
|
290
|
+
<span class="c1">// HTTP body: foo=bar&baz[]=a&baz[]=1&qux[x]=1&qux[y]=2&qux[z]=3
|
291
|
+
</span></code></pre>
|
292
|
+
<a href='#parameter_encoding' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='parameter_encoding'>Parameter Encoding</h3>
|
293
|
+
<p class="para">Parameters can also be encoded as JSON, Property List, or any custom format, using the <code>ParameterEncoding</code> enum:</p><pre><code class="highlight swift"><span class="kd">enum</span> <span class="kt">ParameterEncoding</span> <span class="p">{</span>
|
294
|
+
<span class="k">case</span> <span class="kt">URL</span>
|
295
|
+
<span class="k">case</span> <span class="kt">JSON</span>
|
296
|
+
<span class="k">case</span> <span class="kt">PropertyList</span><span class="p">(</span><span class="nv">format</span><span class="p">:</span> <span class="kt">NSPropertyListFormat</span><span class="p">,</span>
|
297
|
+
<span class="nv">options</span><span class="p">:</span> <span class="kt">NSPropertyListWriteOptions</span><span class="p">)</span>
|
298
|
+
|
299
|
+
<span class="kd">func</span> <span class="nf">encode</span><span class="p">(</span><span class="nv">request</span><span class="p">:</span> <span class="kt">NSURLRequest</span><span class="p">,</span>
|
300
|
+
<span class="nv">parameters</span><span class="p">:</span> <span class="p">[</span><span class="kt">String</span><span class="p">:</span> <span class="kt">AnyObject</span><span class="p">]?)</span> <span class="o">-></span>
|
301
|
+
<span class="p">(</span><span class="kt">NSURLRequest</span><span class="p">,</span> <span class="kt">NSError</span><span class="p">?)</span>
|
302
|
+
<span class="p">{</span> <span class="o">...</span> <span class="p">}</span>
|
303
|
+
<span class="p">}</span>
|
304
|
+
</code></pre>
|
305
|
+
|
306
|
+
<ul>
|
307
|
+
<li><code>URL</code>: A query string to be set as or appended to any existing URL query for <code>GET</code>, <code>HEAD</code>, and <code>DELETE</code> requests, or set as the body for requests with any other HTTP method. The <code>Content-Type</code> HTTP header field of an encoded request with HTTP body is set to <code>application/x-www-form-urlencoded</code>. <em>Since there is no published specification for how to encode collection types, the convention of appending <code>[]</code> to the key for array values (<code>foo[]=1&foo[]=2</code>), and appending the key surrounded by square brackets for nested dictionary values (<code>foo[bar]=baz</code>).</em></li>
|
308
|
+
<li><code>JSON</code>: Uses <code>NSJSONSerialization</code> to create a JSON representation of the parameters object, which is set as the body of the request. The <code>Content-Type</code> HTTP header field of an encoded request is set to <code>application/json</code>.</li>
|
309
|
+
<li><code>PropertyList</code>: Uses <code>NSPropertyListSerialization</code> to create a plist representation of the parameters object, according to the associated format and write options values, which is set as the body of the request. The <code>Content-Type</code> HTTP header field of an encoded request is set to <code>application/x-plist</code>.</li>
|
310
|
+
<li><code>Custom</code>: Uses the associated closure value to construct a new request given an existing request and parameters.</li>
|
311
|
+
</ul>
|
312
|
+
<a href='#manual_parameter_encoding_of_an_nsurlrequest' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='manual_parameter_encoding_of_an_nsurlrequest'>Manual Parameter Encoding of an NSURLRequest</h4>
|
313
|
+
<pre><code class="highlight swift"><span class="k">let</span> <span class="nv">URL</span> <span class="o">=</span> <span class="kt">NSURL</span><span class="p">(</span><span class="nv">string</span><span class="p">:</span> <span class="s">"http://httpbin.org/get"</span><span class="p">)</span>
|
314
|
+
<span class="k">var</span> <span class="nv">request</span> <span class="o">=</span> <span class="kt">NSURLRequest</span><span class="p">(</span><span class="kt">URL</span><span class="p">:</span> <span class="kt">URL</span><span class="p">)</span>
|
315
|
+
|
316
|
+
<span class="k">let</span> <span class="nv">parameters</span> <span class="o">=</span> <span class="p">[</span><span class="s">"foo"</span><span class="p">:</span> <span class="s">"bar"</span><span class="p">]</span>
|
317
|
+
<span class="k">let</span> <span class="nv">encoding</span> <span class="o">=</span> <span class="kt">Alamofire</span><span class="o">.</span><span class="kt">ParameterEncoding</span><span class="o">.</span><span class="kt">URL</span>
|
318
|
+
<span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">_</span><span class="p">)</span> <span class="o">=</span> <span class="n">encoding</span><span class="o">.</span><span class="nf">encode</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">parameters</span><span class="p">)</span>
|
319
|
+
</code></pre>
|
320
|
+
<a href='#post_request_with_json_encoded_parameters' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='post_request_with_json_encoded_parameters'>POST Request with JSON-encoded Parameters</h4>
|
321
|
+
<pre><code class="highlight swift"><span class="k">let</span> <span class="nv">parameters</span> <span class="o">=</span> <span class="p">[</span>
|
322
|
+
<span class="s">"foo"</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span>
|
323
|
+
<span class="s">"bar"</span><span class="p">:</span> <span class="p">[</span>
|
324
|
+
<span class="s">"baz"</span><span class="p">:</span> <span class="s">"qux"</span>
|
325
|
+
<span class="p">]</span>
|
326
|
+
<span class="p">]</span>
|
327
|
+
|
328
|
+
<span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">POST</span><span class="p">,</span> <span class="s">"http://httpbin.org/post"</span><span class="p">,</span> <span class="nv">parameters</span><span class="p">:</span> <span class="n">parameters</span><span class="p">,</span> <span class="nv">encoding</span><span class="p">:</span> <span class="o">.</span><span class="kt">JSON</span><span class="p">)</span>
|
329
|
+
<span class="c1">// HTTP body: {"foo": [1, 2, 3], "bar": {"baz": "qux"}}
|
330
|
+
</span></code></pre>
|
331
|
+
<a href='#caching' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='caching'>Caching</h3>
|
332
|
+
<p class="para">Caching is handled on the system framework level by <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLCache_Class/Reference/Reference.html#//apple_ref/occ/cl/NSURLCache"><code>NSURLCache</code></a>.</p><a href='#uploading' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='uploading'>Uploading</h3>
|
333
|
+
<p class="para"><strong>Supported Upload Types</strong></p>
|
334
|
+
<ul>
|
335
|
+
<li>File</li>
|
336
|
+
<li>Data</li>
|
337
|
+
<li>Stream</li>
|
338
|
+
</ul>
|
339
|
+
<a href='#uploading_a_file' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='uploading_a_file'>Uploading a File</h4>
|
340
|
+
<pre><code class="highlight swift"><span class="k">let</span> <span class="nv">fileURL</span> <span class="o">=</span> <span class="kt">NSBundle</span><span class="o">.</span><span class="nf">mainBundle</span><span class="p">()</span>
|
341
|
+
<span class="o">.</span><span class="kt">URLForResource</span><span class="p">(</span><span class="s">"Default"</span><span class="p">,</span>
|
342
|
+
<span class="nv">withExtension</span><span class="p">:</span> <span class="s">"png"</span><span class="p">)</span>
|
343
|
+
|
344
|
+
<span class="kt">Alamofire</span><span class="o">.</span><span class="nf">upload</span><span class="p">(</span><span class="o">.</span><span class="kt">POST</span><span class="p">,</span> <span class="s">"http://httpbin.org/post"</span><span class="p">,</span> <span class="nv">file</span><span class="p">:</span> <span class="n">fileURL</span><span class="p">)</span>
|
345
|
+
</code></pre>
|
346
|
+
<a href='#uploading_w_progress' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='uploading_w_progress'>Uploading w/Progress</h4>
|
347
|
+
<pre><code class="highlight swift"><span class="kt">Alamofire</span><span class="o">.</span><span class="nf">upload</span><span class="p">(</span><span class="o">.</span><span class="kt">POST</span><span class="p">,</span> <span class="s">"http://httpbin.org/post"</span><span class="p">,</span> <span class="nv">file</span><span class="p">:</span> <span class="n">fileURL</span><span class="p">)</span>
|
348
|
+
<span class="o">.</span><span class="n">progress</span> <span class="p">{</span> <span class="p">(</span><span class="n">bytesWritten</span><span class="p">,</span> <span class="n">totalBytesWritten</span><span class="p">,</span> <span class="n">totalBytesExpectedToWrite</span><span class="p">)</span> <span class="k">in</span>
|
349
|
+
<span class="nf">println</span><span class="p">(</span><span class="n">totalBytesWritten</span><span class="p">)</span>
|
350
|
+
<span class="p">}</span>
|
351
|
+
<span class="o">.</span><span class="n">responseJSON</span> <span class="p">{</span> <span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="kt">JSON</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span> <span class="k">in</span>
|
352
|
+
<span class="nf">println</span><span class="p">(</span><span class="kt">JSON</span><span class="p">)</span>
|
353
|
+
<span class="p">}</span>
|
354
|
+
</code></pre>
|
355
|
+
<a href='#downloading' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='downloading'>Downloading</h3>
|
356
|
+
<p class="para"><strong>Supported Download Types</strong></p>
|
357
|
+
<ul>
|
358
|
+
<li>Request</li>
|
359
|
+
<li>Resume Data</li>
|
360
|
+
</ul>
|
361
|
+
<a href='#downloading_a_file' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='downloading_a_file'>Downloading a File</h4>
|
362
|
+
<pre><code class="highlight swift"><span class="kt">Alamofire</span><span class="o">.</span><span class="nf">download</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"http://httpbin.org/stream/100"</span><span class="p">,</span> <span class="nv">destination</span><span class="p">:</span> <span class="p">{</span> <span class="p">(</span><span class="n">temporaryURL</span><span class="p">,</span> <span class="n">response</span><span class="p">)</span> <span class="k">in</span>
|
363
|
+
<span class="k">if</span> <span class="k">let</span> <span class="nv">directoryURL</span> <span class="o">=</span> <span class="kt">NSFileManager</span><span class="o">.</span><span class="nf">defaultManager</span><span class="p">()</span>
|
364
|
+
<span class="o">.</span><span class="kt">URLsForDirectory</span><span class="p">(</span><span class="o">.</span><span class="kt">DocumentDirectory</span><span class="p">,</span>
|
365
|
+
<span class="nv">inDomains</span><span class="p">:</span> <span class="o">.</span><span class="kt">UserDomainMask</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
|
366
|
+
<span class="k">as</span><span class="p">?</span> <span class="kt">NSURL</span> <span class="p">{</span>
|
367
|
+
<span class="k">let</span> <span class="nv">pathComponent</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">suggestedFilename</span>
|
368
|
+
|
369
|
+
<span class="k">return</span> <span class="n">directoryURL</span><span class="o">.</span><span class="kt">URLByAppendingPathComponent</span><span class="p">(</span><span class="n">pathComponent</span><span class="o">!</span><span class="p">)</span>
|
370
|
+
<span class="p">}</span>
|
371
|
+
|
372
|
+
<span class="k">return</span> <span class="n">temporaryURL</span>
|
373
|
+
<span class="p">})</span>
|
374
|
+
</code></pre>
|
375
|
+
<a href='#using_the_default_download_destination' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='using_the_default_download_destination'>Using the Default Download Destination</h4>
|
376
|
+
<pre><code class="highlight swift"><span class="k">let</span> <span class="nv">destination</span> <span class="o">=</span> <span class="kt">Alamofire</span><span class="o">.</span><span class="kt">Request</span><span class="o">.</span><span class="nf">suggestedDownloadDestination</span><span class="p">(</span><span class="nv">directory</span><span class="p">:</span> <span class="o">.</span><span class="kt">DocumentDirectory</span><span class="p">,</span> <span class="nv">domain</span><span class="p">:</span> <span class="o">.</span><span class="kt">UserDomainMask</span><span class="p">)</span>
|
377
|
+
|
378
|
+
<span class="kt">Alamofire</span><span class="o">.</span><span class="nf">download</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"http://httpbin.org/stream/100"</span><span class="p">,</span> <span class="nv">destination</span><span class="p">:</span> <span class="n">destination</span><span class="p">)</span>
|
379
|
+
</code></pre>
|
380
|
+
<a href='#downloading_a_file_w_progress' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='downloading_a_file_w_progress'>Downloading a File w/Progress</h4>
|
381
|
+
<pre><code class="highlight swift"><span class="kt">Alamofire</span><span class="o">.</span><span class="nf">download</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"http://httpbin.org/stream/100"</span><span class="p">,</span> <span class="nv">destination</span><span class="p">:</span> <span class="n">destination</span><span class="p">)</span>
|
382
|
+
<span class="o">.</span><span class="n">progress</span> <span class="p">{</span> <span class="p">(</span><span class="n">bytesRead</span><span class="p">,</span> <span class="n">totalBytesRead</span><span class="p">,</span> <span class="n">totalBytesExpectedToRead</span><span class="p">)</span> <span class="k">in</span>
|
383
|
+
<span class="nf">println</span><span class="p">(</span><span class="n">totalBytesRead</span><span class="p">)</span>
|
384
|
+
<span class="p">}</span>
|
385
|
+
<span class="o">.</span><span class="n">response</span> <span class="p">{</span> <span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span> <span class="k">in</span>
|
386
|
+
<span class="nf">println</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
|
387
|
+
<span class="p">}</span>
|
388
|
+
</code></pre>
|
389
|
+
<a href='#authentication' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='authentication'>Authentication</h3>
|
390
|
+
<p class="para">Authentication is handled on the system framework level by <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLAuthenticationChallenge_Class/Reference/Reference.html"><code>NSURLCredential</code> and <code>NSURLAuthenticationChallenge</code></a>.</p><p class="para"><strong>Supported Authentication Schemes</strong></p>
|
391
|
+
<ul>
|
392
|
+
<li><a href="http://en.wikipedia.org/wiki/Basic_access_authentication">HTTP Basic</a></li>
|
393
|
+
<li><a href="http://en.wikipedia.org/wiki/Digest_access_authentication">HTTP Digest</a></li>
|
394
|
+
<li><a href="http://en.wikipedia.org/wiki/Kerberos_%28protocol%29">Kerberos</a></li>
|
395
|
+
<li><a href="http://en.wikipedia.org/wiki/NT_LAN_Manager">NTLM</a></li>
|
396
|
+
</ul>
|
397
|
+
<a href='#http_basic_authentication' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='http_basic_authentication'>HTTP Basic Authentication</h4>
|
398
|
+
<pre><code class="highlight swift"><span class="k">let</span> <span class="nv">user</span> <span class="o">=</span> <span class="s">"user"</span>
|
399
|
+
<span class="k">let</span> <span class="nv">password</span> <span class="o">=</span> <span class="s">"password"</span>
|
400
|
+
|
401
|
+
<span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"https://httpbin.org/basic-auth/</span><span class="se">\(</span><span class="n">user</span><span class="se">)</span><span class="s">/</span><span class="se">\(</span><span class="n">password</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
|
402
|
+
<span class="o">.</span><span class="nf">authenticate</span><span class="p">(</span><span class="nv">user</span><span class="p">:</span> <span class="n">user</span><span class="p">,</span> <span class="nv">password</span><span class="p">:</span> <span class="n">password</span><span class="p">)</span>
|
403
|
+
<span class="o">.</span><span class="n">response</span> <span class="p">{(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span> <span class="k">in</span>
|
404
|
+
<span class="nf">println</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
|
405
|
+
<span class="p">}</span>
|
406
|
+
</code></pre>
|
407
|
+
<a href='#authentication_with_nsurlcredential' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='authentication_with_nsurlcredential'>Authentication with NSURLCredential</h4>
|
408
|
+
<pre><code class="highlight swift"><span class="k">let</span> <span class="nv">user</span> <span class="o">=</span> <span class="s">"user"</span>
|
409
|
+
<span class="k">let</span> <span class="nv">password</span> <span class="o">=</span> <span class="s">"password"</span>
|
410
|
+
|
411
|
+
<span class="k">let</span> <span class="nv">credential</span> <span class="o">=</span> <span class="kt">NSURLCredential</span><span class="p">(</span><span class="nv">user</span><span class="p">:</span> <span class="n">user</span><span class="p">,</span> <span class="nv">password</span><span class="p">:</span> <span class="n">password</span><span class="p">,</span> <span class="nv">persistence</span><span class="p">:</span> <span class="o">.</span><span class="kt">ForSession</span><span class="p">)</span>
|
412
|
+
|
413
|
+
<span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"https://httpbin.org/basic-auth/</span><span class="se">\(</span><span class="n">user</span><span class="se">)</span><span class="s">/</span><span class="se">\(</span><span class="n">password</span><span class="se">)</span><span class="s">"</span><span class="p">)</span>
|
414
|
+
<span class="o">.</span><span class="nf">authenticate</span><span class="p">(</span><span class="nv">usingCredential</span><span class="p">:</span> <span class="n">credential</span><span class="p">)</span>
|
415
|
+
<span class="o">.</span><span class="n">response</span> <span class="p">{(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span> <span class="k">in</span>
|
416
|
+
<span class="nf">println</span><span class="p">(</span><span class="n">response</span><span class="p">)</span>
|
417
|
+
<span class="p">}</span>
|
418
|
+
</code></pre>
|
419
|
+
<a href='#validation' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='validation'>Validation</h3>
|
420
|
+
<p class="para">By default, Alamofire treats any completed request to be successful, regardless of the content of the response. Calling <code>validate</code> before a response handler causes an error to be generated if the response had an unacceptable status code or MIME type.</p><a href='#manual_validation' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='manual_validation'>Manual Validation</h4>
|
421
|
+
<pre><code class="highlight swift"><span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"http://httpbin.org/get"</span><span class="p">,</span> <span class="nv">parameters</span><span class="p">:</span> <span class="p">[</span><span class="s">"foo"</span><span class="p">:</span> <span class="s">"bar"</span><span class="p">])</span>
|
422
|
+
<span class="o">.</span><span class="nf">validate</span><span class="p">(</span><span class="nv">statusCode</span><span class="p">:</span> <span class="mi">200</span><span class="o">..<</span><span class="mi">300</span><span class="p">)</span>
|
423
|
+
<span class="o">.</span><span class="nf">validate</span><span class="p">(</span><span class="nv">contentType</span><span class="p">:</span> <span class="p">[</span><span class="s">"application/json"</span><span class="p">])</span>
|
424
|
+
<span class="o">.</span><span class="n">response</span> <span class="p">{</span> <span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span> <span class="k">in</span>
|
425
|
+
<span class="nf">println</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
|
426
|
+
<span class="p">}</span>
|
427
|
+
</code></pre>
|
428
|
+
<a href='#automatic_validation' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='automatic_validation'>Automatic Validation</h4>
|
429
|
+
<p class="para">Automatically validates status code within <code>200...299</code> range, and that the <code>Content-Type</code> header of the response matches the <code>Accept</code> header of the request, if one is provided.</p><pre><code class="highlight swift"><span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"http://httpbin.org/get"</span><span class="p">,</span> <span class="nv">parameters</span><span class="p">:</span> <span class="p">[</span><span class="s">"foo"</span><span class="p">:</span> <span class="s">"bar"</span><span class="p">])</span>
|
430
|
+
<span class="o">.</span><span class="nf">validate</span><span class="p">()</span>
|
431
|
+
<span class="o">.</span><span class="n">response</span> <span class="p">{</span> <span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span> <span class="k">in</span>
|
432
|
+
<span class="nf">println</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
|
433
|
+
<span class="p">}</span>
|
434
|
+
</code></pre>
|
435
|
+
<a href='#printable' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='printable'>Printable</h3>
|
436
|
+
<pre><code class="highlight swift"><span class="k">let</span> <span class="nv">request</span> <span class="o">=</span> <span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"http://httpbin.org/ip"</span><span class="p">)</span>
|
437
|
+
|
438
|
+
<span class="nf">println</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
|
439
|
+
<span class="c1">// GET http://httpbin.org/ip (200)
|
440
|
+
</span></code></pre>
|
441
|
+
<a href='#debugprintable' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='debugprintable'>DebugPrintable</h3>
|
442
|
+
<pre><code class="highlight swift"><span class="k">let</span> <span class="nv">request</span> <span class="o">=</span> <span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"http://httpbin.org/get"</span><span class="p">,</span> <span class="nv">parameters</span><span class="p">:</span> <span class="p">[</span><span class="s">"foo"</span><span class="p">:</span> <span class="s">"bar"</span><span class="p">])</span>
|
443
|
+
|
444
|
+
<span class="nf">debugPrintln</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
|
445
|
+
</code></pre>
|
446
|
+
<a href='#output_curl' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='output_curl'>Output (cURL)</h4>
|
447
|
+
<pre><code class="highlight plaintext">$ curl -i \
|
448
|
+
-H "User-Agent: Alamofire" \
|
449
|
+
-H "Accept-Encoding: Accept-Encoding: gzip;q=1.0,compress;q=0.5" \
|
450
|
+
-H "Accept-Language: en;q=1.0,fr;q=0.9,de;q=0.8,zh-Hans;q=0.7,zh-Hant;q=0.6,ja;q=0.5" \
|
451
|
+
"http://httpbin.org/get?foo=bar"
|
452
|
+
</code></pre>
|
453
|
+
|
454
|
+
<hr>
|
455
|
+
<a href='#advanced_usage' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h2 id='advanced_usage'>Advanced Usage</h2>
|
456
|
+
|
457
|
+
<blockquote>
|
458
|
+
<p class="para">Alamofire is built on <code>NSURLSession</code> and the Foundation URL Loading System. To make the most of
|
459
|
+
this framework, it is recommended that you be familiar with the concepts and capabilities of the underlying networking stack.</p></blockquote>
|
460
|
+
<p class="para"><strong>Recommended Reading</strong></p>
|
461
|
+
<ul>
|
462
|
+
<li><a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html">URL Loading System Programming Guide</a></li>
|
463
|
+
<li><a href="https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSession_class/Introduction/Introduction.html#//apple_ref/occ/cl/NSURLSession">NSURLSession Class Reference</a></li>
|
464
|
+
<li><a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLCache_Class/Reference/Reference.html#//apple_ref/occ/cl/NSURLCache">NSURLCache Class Reference</a></li>
|
465
|
+
<li><a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLAuthenticationChallenge_Class/Reference/Reference.html">NSURLAuthenticationChallenge Class Reference</a></li>
|
466
|
+
</ul>
|
467
|
+
<a href='#manager' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='manager'>Manager</h3>
|
468
|
+
<p class="para">Top-level convenience methods like <code>Alamofire.request</code> use a shared instance of <code>Alamofire.Manager</code>, which is configured with the default <code>NSURLSessionConfiguration</code>.</p><p class="para">As such, the following two statements are equivalent:</p><pre><code class="highlight swift"><span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"http://httpbin.org/get"</span><span class="p">)</span>
|
469
|
+
</code></pre>
|
470
|
+
<pre><code class="highlight swift"><span class="k">let</span> <span class="nv">manager</span> <span class="o">=</span> <span class="kt">Alamofire</span><span class="o">.</span><span class="kt">Manager</span><span class="o">.</span><span class="n">sharedInstance</span>
|
471
|
+
<span class="n">manager</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="kt">NSURLRequest</span><span class="p">(</span><span class="kt">URL</span><span class="p">:</span> <span class="kt">NSURL</span><span class="p">(</span><span class="nv">string</span><span class="p">:</span> <span class="s">"http://httpbin.org/get"</span><span class="p">)))</span>
|
472
|
+
</code></pre>
|
473
|
+
<p class="para">Applications can create managers for background and ephemeral sessions, as well as new managers that customize the default session configuration, such as for default headers (<code>HTTPAdditionalHeaders</code>) or timeout interval (<code>timeoutIntervalForRequest</code>).</p><a href='#creating_a_manager_with_default_configuration' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='creating_a_manager_with_default_configuration'>Creating a Manager with Default Configuration</h4>
|
474
|
+
<pre><code class="highlight swift"><span class="k">let</span> <span class="nv">configuration</span> <span class="o">=</span> <span class="kt">NSURLSessionConfiguration</span><span class="o">.</span><span class="nf">defaultSessionConfiguration</span><span class="p">()</span>
|
475
|
+
<span class="k">let</span> <span class="nv">manager</span> <span class="o">=</span> <span class="kt">Alamofire</span><span class="o">.</span><span class="kt">Manager</span><span class="p">(</span><span class="nv">configuration</span><span class="p">:</span> <span class="n">configuration</span><span class="p">)</span>
|
476
|
+
</code></pre>
|
477
|
+
<a href='#creating_a_manager_with_background_configuration' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='creating_a_manager_with_background_configuration'>Creating a Manager with Background Configuration</h4>
|
478
|
+
<pre><code class="highlight swift"><span class="k">let</span> <span class="nv">configuration</span> <span class="o">=</span> <span class="kt">NSURLSessionConfiguration</span><span class="o">.</span><span class="nf">backgroundSessionConfiguration</span><span class="p">(</span><span class="s">"com.example.app.background"</span><span class="p">)</span>
|
479
|
+
<span class="k">let</span> <span class="nv">manager</span> <span class="o">=</span> <span class="kt">Alamofire</span><span class="o">.</span><span class="kt">Manager</span><span class="p">(</span><span class="nv">configuration</span><span class="p">:</span> <span class="n">configuration</span><span class="p">)</span>
|
480
|
+
</code></pre>
|
481
|
+
<a href='#creating_a_manager_with_ephemeral_configuration' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='creating_a_manager_with_ephemeral_configuration'>Creating a Manager with Ephemeral Configuration</h4>
|
482
|
+
<pre><code class="highlight swift"><span class="k">let</span> <span class="nv">configuration</span> <span class="o">=</span> <span class="kt">NSURLSessionConfiguration</span><span class="o">.</span><span class="nf">ephemeralSessionConfiguration</span><span class="p">()</span>
|
483
|
+
<span class="k">let</span> <span class="nv">manager</span> <span class="o">=</span> <span class="kt">Alamofire</span><span class="o">.</span><span class="kt">Manager</span><span class="p">(</span><span class="nv">configuration</span><span class="p">:</span> <span class="n">configuration</span><span class="p">)</span>
|
484
|
+
</code></pre>
|
485
|
+
<a href='#modifying_session_configuration' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='modifying_session_configuration'>Modifying Session Configuration</h4>
|
486
|
+
<pre><code class="highlight swift"><span class="k">var</span> <span class="nv">defaultHeaders</span> <span class="o">=</span> <span class="kt">Alamofire</span><span class="o">.</span><span class="kt">Manager</span><span class="o">.</span><span class="n">sharedInstance</span><span class="o">.</span><span class="n">session</span><span class="o">.</span><span class="n">configuration</span><span class="o">.</span><span class="kt">HTTPAdditionalHeaders</span> <span class="p">??</span> <span class="p">[:]</span>
|
487
|
+
<span class="n">defaultHeaders</span><span class="p">[</span><span class="s">"DNT"</span><span class="p">]</span> <span class="o">=</span> <span class="s">"1 (Do Not Track Enabled)"</span>
|
488
|
+
|
489
|
+
<span class="k">let</span> <span class="nv">configuration</span> <span class="o">=</span> <span class="kt">NSURLSessionConfiguration</span><span class="o">.</span><span class="nf">defaultSessionConfiguration</span><span class="p">()</span>
|
490
|
+
<span class="n">configuration</span><span class="o">.</span><span class="kt">HTTPAdditionalHeaders</span> <span class="o">=</span> <span class="n">defaultHeaders</span>
|
491
|
+
|
492
|
+
<span class="k">let</span> <span class="nv">manager</span> <span class="o">=</span> <span class="kt">Alamofire</span><span class="o">.</span><span class="kt">Manager</span><span class="p">(</span><span class="nv">configuration</span><span class="p">:</span> <span class="n">configuration</span><span class="p">)</span>
|
493
|
+
</code></pre>
|
494
|
+
|
495
|
+
<blockquote>
|
496
|
+
<p class="para">This is <strong>not</strong> recommended for <code>Authorization</code> or <code>Content-Type</code> headers. Instead, use <code>URLRequestConvertible</code> and <code>ParameterEncoding</code>, respectively.</p></blockquote>
|
497
|
+
<a href='#request' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='request'>Request</h3>
|
498
|
+
<p class="para">The result of a <code>request</code>, <code>upload</code>, or <code>download</code> method is an instance of <code>Alamofire.Request</code>. A request is always created using a constructor method from an owning manager, and never initialized directly.</p><p class="para">Methods like <code>authenticate</code>, <code>validate</code>, and <code>response</code> return the caller in order to facilitate chaining.</p><p class="para">Requests can be suspended, resumed, and cancelled:</p>
|
499
|
+
<ul>
|
500
|
+
<li><code>suspend()</code>: Suspends the underlying task and dispatch queue</li>
|
501
|
+
<li><code>resume()</code>: Resumes the underlying task and dispatch queue. If the owning manager does not have <code>startRequestsImmediately</code> set to <code>true</code>, the request must call <code>resume()</code> in order to start.</li>
|
502
|
+
<li><code>cancel()</code>: Cancels the underlying task, producing an error that is passed to any registered response handlers.</li>
|
503
|
+
</ul>
|
504
|
+
<a href='#response_serialization' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='response_serialization'>Response Serialization</h3>
|
505
|
+
<a href='#creating_a_custom_response_serializer' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='creating_a_custom_response_serializer'>Creating a Custom Response Serializer</h4>
|
506
|
+
<p class="para">Alamofire provides built-in response serialization for strings, JSON, and property lists, but others can be added in extensions on <code>Alamofire.Request</code>.</p><p class="para">For example, here’s how a response handler using <a href="https://github.com/mattt/Ono">Ono</a> might be implemented:</p><pre><code class="highlight swift"><span class="kd">extension</span> <span class="kt">Request</span> <span class="p">{</span>
|
507
|
+
<span class="kd">class</span> <span class="kd">func</span> <span class="kt">XMLResponseSerializer</span><span class="p">()</span> <span class="o">-></span> <span class="kt">Serializer</span> <span class="p">{</span>
|
508
|
+
<span class="k">return</span> <span class="p">{</span> <span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span> <span class="k">in</span>
|
509
|
+
<span class="k">if</span> <span class="n">data</span> <span class="o">==</span> <span class="kc">nil</span> <span class="p">{</span>
|
510
|
+
<span class="nf">return</span> <span class="p">(</span><span class="kc">nil</span><span class="p">,</span> <span class="kc">nil</span><span class="p">)</span>
|
511
|
+
<span class="p">}</span>
|
512
|
+
|
513
|
+
<span class="k">var</span> <span class="nv">XMLSerializationError</span><span class="p">:</span> <span class="kt">NSError</span><span class="p">?</span>
|
514
|
+
<span class="k">let</span> <span class="nv">XML</span> <span class="o">=</span> <span class="kt">ONOXMLDocument</span><span class="o">.</span><span class="kt">XMLDocumentWithData</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="o">&</span><span class="kt">XMLSerializationError</span><span class="p">)</span>
|
515
|
+
|
516
|
+
<span class="nf">return</span> <span class="p">(</span><span class="kt">XML</span><span class="p">,</span> <span class="kt">XMLSerializationError</span><span class="p">)</span>
|
517
|
+
<span class="p">}</span>
|
518
|
+
<span class="p">}</span>
|
519
|
+
|
520
|
+
<span class="kd">func</span> <span class="nf">responseXMLDocument</span><span class="p">(</span><span class="nv">completionHandler</span><span class="p">:</span> <span class="p">(</span><span class="kt">NSURLRequest</span><span class="p">,</span> <span class="kt">NSHTTPURLResponse</span><span class="p">?,</span> <span class="kt">OnoXMLDocument</span><span class="p">?,</span> <span class="kt">NSError</span><span class="p">?)</span> <span class="o">-></span> <span class="kt">Void</span><span class="p">)</span> <span class="o">-></span> <span class="k">Self</span> <span class="p">{</span>
|
521
|
+
<span class="k">return</span> <span class="nf">response</span><span class="p">(</span><span class="nv">serializer</span><span class="p">:</span> <span class="kt">Request</span><span class="o">.</span><span class="kt">XMLResponseSerializer</span><span class="p">(),</span> <span class="nv">completionHandler</span><span class="p">:</span> <span class="p">{</span> <span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="kt">XML</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span> <span class="k">in</span>
|
522
|
+
<span class="nf">completionHandler</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="kt">XML</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span>
|
523
|
+
<span class="p">})</span>
|
524
|
+
<span class="p">}</span>
|
525
|
+
<span class="p">}</span>
|
526
|
+
</code></pre>
|
527
|
+
<a href='#generic_response_object_serialization' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='generic_response_object_serialization'>Generic Response Object Serialization</h4>
|
528
|
+
<p class="para">Generics can be used to provide automatic, type-safe response object serialization.</p><pre><code class="highlight swift"><span class="k">@objc</span> <span class="kd">public</span> <span class="kd">protocol</span> <span class="kt">ResponseObjectSerializable</span> <span class="p">{</span>
|
529
|
+
<span class="nf">init</span><span class="p">(</span><span class="nv">response</span><span class="p">:</span> <span class="kt">NSHTTPURLResponse</span><span class="p">,</span> <span class="nv">representation</span><span class="p">:</span> <span class="kt">AnyObject</span><span class="p">)</span>
|
530
|
+
<span class="p">}</span>
|
531
|
+
|
532
|
+
<span class="kd">extension</span> <span class="kt">Alamofire</span><span class="o">.</span><span class="kt">Request</span> <span class="p">{</span>
|
533
|
+
<span class="kd">public</span> <span class="kd">func</span> <span class="n">responseObject</span><span class="o"><</span><span class="kt">T</span><span class="p">:</span> <span class="kt">ResponseObjectSerializable</span><span class="o">></span><span class="p">(</span><span class="nv">completionHandler</span><span class="p">:</span> <span class="p">(</span><span class="kt">NSURLRequest</span><span class="p">,</span> <span class="kt">NSHTTPURLResponse</span><span class="p">?,</span> <span class="kt">T</span><span class="p">?,</span> <span class="kt">NSError</span><span class="p">?)</span> <span class="o">-></span> <span class="kt">Void</span><span class="p">)</span> <span class="o">-></span> <span class="k">Self</span> <span class="p">{</span>
|
534
|
+
<span class="k">let</span> <span class="nv">serializer</span><span class="p">:</span> <span class="kt">Serializer</span> <span class="o">=</span> <span class="p">{</span> <span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span> <span class="k">in</span>
|
535
|
+
<span class="k">let</span> <span class="nv">JSONSerializer</span> <span class="o">=</span> <span class="kt">Request</span><span class="o">.</span><span class="kt">JSONResponseSerializer</span><span class="p">(</span><span class="nv">options</span><span class="p">:</span> <span class="o">.</span><span class="kt">AllowFragments</span><span class="p">)</span>
|
536
|
+
<span class="nf">let</span> <span class="p">(</span><span class="kt">JSON</span><span class="p">:</span> <span class="kt">AnyObject</span><span class="p">?,</span> <span class="n">serializationError</span><span class="p">)</span> <span class="o">=</span> <span class="kt">JSONSerializer</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
|
537
|
+
<span class="k">if</span> <span class="n">response</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="o">&&</span> <span class="kt">JSON</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
|
538
|
+
<span class="nf">return</span> <span class="p">(</span><span class="kt">T</span><span class="p">(</span><span class="nv">response</span><span class="p">:</span> <span class="n">response</span><span class="o">!</span><span class="p">,</span> <span class="nv">representation</span><span class="p">:</span> <span class="kt">JSON</span><span class="o">!</span><span class="p">),</span> <span class="kc">nil</span><span class="p">)</span>
|
539
|
+
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
|
540
|
+
<span class="nf">return</span> <span class="p">(</span><span class="kc">nil</span><span class="p">,</span> <span class="n">serializationError</span><span class="p">)</span>
|
541
|
+
<span class="p">}</span>
|
542
|
+
<span class="p">}</span>
|
543
|
+
|
544
|
+
<span class="k">return</span> <span class="nf">response</span><span class="p">(</span><span class="nv">serializer</span><span class="p">:</span> <span class="n">serializer</span><span class="p">,</span> <span class="nv">completionHandler</span><span class="p">:</span> <span class="p">{</span> <span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">object</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span> <span class="k">in</span>
|
545
|
+
<span class="nf">completionHandler</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">object</span> <span class="k">as</span><span class="p">?</span> <span class="kt">T</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span>
|
546
|
+
<span class="p">})</span>
|
547
|
+
<span class="p">}</span>
|
548
|
+
<span class="p">}</span>
|
549
|
+
</code></pre>
|
550
|
+
<pre><code class="highlight swift"><span class="kd">final</span> <span class="kd">class</span> <span class="kt">User</span><span class="p">:</span> <span class="kt">ResponseObjectSerializable</span> <span class="p">{</span>
|
551
|
+
<span class="k">let</span> <span class="nv">username</span><span class="p">:</span> <span class="kt">String</span>
|
552
|
+
<span class="k">let</span> <span class="nv">name</span><span class="p">:</span> <span class="kt">String</span>
|
553
|
+
|
554
|
+
<span class="kd">required</span> <span class="nf">init</span><span class="p">(</span><span class="nv">response</span><span class="p">:</span> <span class="kt">NSHTTPURLResponse</span><span class="p">,</span> <span class="nv">representation</span><span class="p">:</span> <span class="kt">AnyObject</span><span class="p">)</span> <span class="p">{</span>
|
555
|
+
<span class="k">self</span><span class="o">.</span><span class="n">username</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="kt">URL</span><span class="o">!.</span><span class="n">lastPathComponent</span>
|
556
|
+
<span class="k">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">representation</span><span class="o">.</span><span class="nf">valueForKeyPath</span><span class="p">(</span><span class="s">"name"</span><span class="p">)</span> <span class="k">as</span> <span class="kt">String</span>
|
557
|
+
<span class="p">}</span>
|
558
|
+
<span class="p">}</span>
|
559
|
+
</code></pre>
|
560
|
+
<pre><code class="highlight swift"><span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="s">"http://example.com/users/mattt"</span><span class="p">)</span>
|
561
|
+
<span class="o">.</span><span class="n">responseObject</span> <span class="p">{</span> <span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="nv">user</span><span class="p">:</span> <span class="kt">User</span><span class="p">?,</span> <span class="n">_</span><span class="p">)</span> <span class="k">in</span>
|
562
|
+
<span class="nf">println</span><span class="p">(</span><span class="n">user</span><span class="p">)</span>
|
563
|
+
<span class="p">}</span>
|
564
|
+
</code></pre>
|
565
|
+
<p class="para">The same approach can also be used to handle endpoints that return a representation of a collection of objects:</p><pre><code class="highlight swift"><span class="k">@objc</span> <span class="kd">public</span> <span class="kd">protocol</span> <span class="kt">ResponseCollectionSerializable</span> <span class="p">{</span>
|
566
|
+
<span class="kd">class</span> <span class="kd">func</span> <span class="nf">collection</span><span class="p">(</span><span class="nv">#response</span><span class="p">:</span> <span class="kt">NSHTTPURLResponse</span><span class="p">,</span> <span class="nv">representation</span><span class="p">:</span> <span class="kt">AnyObject</span><span class="p">)</span> <span class="o">-></span> <span class="p">[</span><span class="k">Self</span><span class="p">]</span>
|
567
|
+
<span class="p">}</span>
|
568
|
+
|
569
|
+
<span class="kd">extension</span> <span class="kt">Alamofire</span><span class="o">.</span><span class="kt">Request</span> <span class="p">{</span>
|
570
|
+
<span class="kd">public</span> <span class="kd">func</span> <span class="n">responseCollection</span><span class="o"><</span><span class="kt">T</span><span class="p">:</span> <span class="kt">ResponseCollectionSerializable</span><span class="o">></span><span class="p">(</span><span class="nv">completionHandler</span><span class="p">:</span> <span class="p">(</span><span class="kt">NSURLRequest</span><span class="p">,</span> <span class="kt">NSHTTPURLResponse</span><span class="p">?,</span> <span class="p">[</span><span class="kt">T</span><span class="p">]?,</span> <span class="kt">NSError</span><span class="p">?)</span> <span class="o">-></span> <span class="kt">Void</span><span class="p">)</span> <span class="o">-></span> <span class="k">Self</span> <span class="p">{</span>
|
571
|
+
<span class="k">let</span> <span class="nv">serializer</span><span class="p">:</span> <span class="kt">Serializer</span> <span class="o">=</span> <span class="p">{</span> <span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span> <span class="k">in</span>
|
572
|
+
<span class="k">let</span> <span class="nv">JSONSerializer</span> <span class="o">=</span> <span class="kt">Request</span><span class="o">.</span><span class="kt">JSONResponseSerializer</span><span class="p">(</span><span class="nv">options</span><span class="p">:</span> <span class="o">.</span><span class="kt">AllowFragments</span><span class="p">)</span>
|
573
|
+
<span class="nf">let</span> <span class="p">(</span><span class="kt">JSON</span><span class="p">:</span> <span class="kt">AnyObject</span><span class="p">?,</span> <span class="n">serializationError</span><span class="p">)</span> <span class="o">=</span> <span class="kt">JSONSerializer</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>
|
574
|
+
<span class="k">if</span> <span class="n">response</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="o">&&</span> <span class="kt">JSON</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
|
575
|
+
<span class="nf">return</span> <span class="p">(</span><span class="kt">T</span><span class="o">.</span><span class="nf">collection</span><span class="p">(</span><span class="nv">response</span><span class="p">:</span> <span class="n">response</span><span class="o">!</span><span class="p">,</span> <span class="nv">representation</span><span class="p">:</span> <span class="kt">JSON</span><span class="o">!</span><span class="p">),</span> <span class="kc">nil</span><span class="p">)</span>
|
576
|
+
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
|
577
|
+
<span class="nf">return</span> <span class="p">(</span><span class="kc">nil</span><span class="p">,</span> <span class="n">serializationError</span><span class="p">)</span>
|
578
|
+
<span class="p">}</span>
|
579
|
+
<span class="p">}</span>
|
580
|
+
|
581
|
+
<span class="k">return</span> <span class="nf">response</span><span class="p">(</span><span class="nv">serializer</span><span class="p">:</span> <span class="n">serializer</span><span class="p">,</span> <span class="nv">completionHandler</span><span class="p">:</span> <span class="p">{</span> <span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">object</span><span class="p">,</span> <span class="n">error</span><span class="p">)</span> <span class="k">in</span>
|
582
|
+
<span class="nf">completionHandler</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">object</span> <span class="k">as</span><span class="p">?</span> <span class="p">[</span><span class="kt">T</span><span class="p">],</span> <span class="n">error</span><span class="p">)</span>
|
583
|
+
<span class="p">})</span>
|
584
|
+
<span class="p">}</span>
|
585
|
+
<span class="p">}</span>
|
586
|
+
</code></pre>
|
587
|
+
<a href='#urlstringconvertible' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='urlstringconvertible'>URLStringConvertible</h3>
|
588
|
+
<p class="para">Types adopting the <code>URLStringConvertible</code> protocol can be used to construct URL strings, which are then used to construct URL requests. Top-level convenience methods taking a <code>URLStringConvertible</code> argument are provided to allow for type-safe routing behavior.</p><p class="para">Applications interacting with web applications in a significant manner are encouraged to adopt either <code>URLStringConvertible</code> or <code>URLRequestConvertible</code> as a way to ensure consistency of requested endpoints.</p><a href='#type_safe_routing' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='type_safe_routing'>Type-Safe Routing</h4>
|
589
|
+
<pre><code class="highlight swift"><span class="kd">enum</span> <span class="kt">Router</span><span class="p">:</span> <span class="kt">URLStringConvertible</span> <span class="p">{</span>
|
590
|
+
<span class="kd">static</span> <span class="k">let</span> <span class="nv">baseURLString</span> <span class="o">=</span> <span class="s">"http://example.com"</span>
|
591
|
+
|
592
|
+
<span class="k">case</span> <span class="kt">Root</span>
|
593
|
+
<span class="k">case</span> <span class="kt">User</span><span class="p">(</span><span class="kt">String</span><span class="p">)</span>
|
594
|
+
<span class="k">case</span> <span class="kt">Post</span><span class="p">(</span><span class="kt">Int</span><span class="p">,</span> <span class="kt">Int</span><span class="p">,</span> <span class="kt">String</span><span class="p">)</span>
|
595
|
+
|
596
|
+
<span class="c1">// MARK: URLStringConvertible
|
597
|
+
</span>
|
598
|
+
<span class="k">var</span> <span class="nv">URLString</span><span class="p">:</span> <span class="kt">String</span> <span class="p">{</span>
|
599
|
+
<span class="k">let</span> <span class="nv">path</span><span class="p">:</span> <span class="kt">String</span> <span class="o">=</span> <span class="p">{</span>
|
600
|
+
<span class="k">switch</span> <span class="k">self</span> <span class="p">{</span>
|
601
|
+
<span class="k">case</span> <span class="o">.</span><span class="kt">Root</span><span class="p">:</span>
|
602
|
+
<span class="k">return</span> <span class="s">"/"</span>
|
603
|
+
<span class="k">case</span> <span class="o">.</span><span class="kt">User</span><span class="p">(</span><span class="k">let</span> <span class="nv">username</span><span class="p">):</span>
|
604
|
+
<span class="k">return</span> <span class="s">"/users/</span><span class="se">\(</span><span class="n">username</span><span class="se">)</span><span class="s">"</span>
|
605
|
+
<span class="k">case</span> <span class="o">.</span><span class="kt">Post</span><span class="p">(</span><span class="k">let</span> <span class="nv">year</span><span class="p">,</span> <span class="k">let</span> <span class="nv">month</span><span class="p">,</span> <span class="k">let</span> <span class="nv">title</span><span class="p">):</span>
|
606
|
+
<span class="k">let</span> <span class="nv">slug</span> <span class="o">=</span> <span class="n">title</span><span class="o">.</span><span class="nf">stringByReplacingOccurrencesOfString</span><span class="p">(</span><span class="s">" "</span><span class="p">,</span> <span class="nv">withString</span><span class="p">:</span> <span class="s">"-"</span><span class="p">)</span><span class="o">.</span><span class="n">lowercaseString</span>
|
607
|
+
<span class="k">return</span> <span class="s">"/</span><span class="se">\(</span><span class="n">year</span><span class="se">)</span><span class="s">/</span><span class="se">\(</span><span class="n">month</span><span class="se">)</span><span class="s">/</span><span class="se">\(</span><span class="n">slug</span><span class="se">)</span><span class="s">"</span>
|
608
|
+
<span class="p">}</span>
|
609
|
+
<span class="p">}()</span>
|
610
|
+
|
611
|
+
<span class="k">return</span> <span class="kt">Router</span><span class="o">.</span><span class="n">baseURLString</span> <span class="o">+</span> <span class="n">path</span>
|
612
|
+
<span class="p">}</span>
|
613
|
+
<span class="p">}</span>
|
614
|
+
</code></pre>
|
615
|
+
<pre><code class="highlight swift"><span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="o">.</span><span class="kt">GET</span><span class="p">,</span> <span class="kt">Router</span><span class="o">.</span><span class="kt">User</span><span class="p">(</span><span class="s">"mattt"</span><span class="p">))</span>
|
616
|
+
</code></pre>
|
617
|
+
<a href='#urlrequestconvertible' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='urlrequestconvertible'>URLRequestConvertible</h3>
|
618
|
+
<p class="para">Types adopting the <code>URLRequestConvertible</code> protocol can be used to construct URL requests. Like <code>URLStringConvertible</code>, this is recommended for applications with any significant interactions between client and server.</p><p class="para">Top-level and instance methods on <code>Manager</code> taking <code>URLRequestConvertible</code> arguments are provided as a way to provide type-safe routing. Such an approach can be used to abstract away server-side inconsistencies, as well as manage authentication credentials and other state.</p><a href='#api_parameter_abstraction' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='api_parameter_abstraction'>API Parameter Abstraction</h4>
|
619
|
+
<pre><code class="highlight swift"><span class="kd">enum</span> <span class="kt">Router</span><span class="p">:</span> <span class="kt">URLRequestConvertible</span> <span class="p">{</span>
|
620
|
+
<span class="kd">static</span> <span class="k">let</span> <span class="nv">baseURLString</span> <span class="o">=</span> <span class="s">"http://example.com"</span>
|
621
|
+
<span class="kd">static</span> <span class="k">let</span> <span class="nv">perPage</span> <span class="o">=</span> <span class="mi">50</span>
|
622
|
+
|
623
|
+
<span class="k">case</span> <span class="kt">Search</span><span class="p">(</span><span class="nv">query</span><span class="p">:</span> <span class="kt">String</span><span class="p">,</span> <span class="nv">page</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span>
|
624
|
+
|
625
|
+
<span class="c1">// MARK: URLRequestConvertible
|
626
|
+
</span>
|
627
|
+
<span class="k">var</span> <span class="nv">URLRequest</span><span class="p">:</span> <span class="kt">NSURLRequest</span> <span class="p">{</span>
|
628
|
+
<span class="nf">let</span> <span class="p">(</span><span class="nv">path</span><span class="p">:</span> <span class="kt">String</span><span class="p">,</span> <span class="nv">parameters</span><span class="p">:</span> <span class="p">[</span><span class="kt">String</span><span class="p">:</span> <span class="kt">AnyObject</span><span class="p">]?)</span> <span class="o">=</span> <span class="p">{</span>
|
629
|
+
<span class="k">switch</span> <span class="k">self</span> <span class="p">{</span>
|
630
|
+
<span class="k">case</span> <span class="o">.</span><span class="kt">Search</span><span class="p">(</span><span class="k">let</span> <span class="nv">query</span><span class="p">,</span> <span class="k">let</span> <span class="nv">page</span><span class="p">)</span> <span class="k">where</span> <span class="n">page</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span>
|
631
|
+
<span class="nf">return</span> <span class="p">(</span><span class="s">"/search"</span><span class="p">,</span> <span class="p">[</span><span class="s">"q"</span><span class="p">:</span> <span class="n">query</span><span class="p">,</span> <span class="s">"offset"</span><span class="p">:</span> <span class="kt">Router</span><span class="o">.</span><span class="n">perPage</span> <span class="o">*</span> <span class="n">page</span><span class="p">])</span>
|
632
|
+
<span class="k">case</span> <span class="o">.</span><span class="kt">Search</span><span class="p">(</span><span class="k">let</span> <span class="nv">query</span><span class="p">,</span> <span class="n">_</span><span class="p">):</span>
|
633
|
+
<span class="nf">return</span> <span class="p">(</span><span class="s">"/search"</span><span class="p">,</span> <span class="p">[</span><span class="s">"q"</span><span class="p">:</span> <span class="n">query</span><span class="p">])</span>
|
634
|
+
<span class="p">}</span>
|
635
|
+
<span class="p">}()</span>
|
636
|
+
|
637
|
+
<span class="k">let</span> <span class="nv">URL</span> <span class="o">=</span> <span class="kt">NSURL</span><span class="p">(</span><span class="nv">string</span><span class="p">:</span> <span class="kt">Router</span><span class="o">.</span><span class="n">baseURLString</span><span class="p">)</span>
|
638
|
+
<span class="k">let</span> <span class="nv">URLRequest</span> <span class="o">=</span> <span class="kt">NSURLRequest</span><span class="p">(</span><span class="kt">URL</span><span class="p">:</span> <span class="kt">URL</span><span class="o">.</span><span class="kt">URLByAppendingPathComponent</span><span class="p">(</span><span class="n">path</span><span class="p">))</span>
|
639
|
+
<span class="k">let</span> <span class="nv">encoding</span> <span class="o">=</span> <span class="kt">Alamofire</span><span class="o">.</span><span class="kt">ParameterEncoding</span><span class="o">.</span><span class="kt">URL</span>
|
640
|
+
|
641
|
+
<span class="k">return</span> <span class="n">encoding</span><span class="o">.</span><span class="nf">encode</span><span class="p">(</span><span class="kt">URLRequest</span><span class="p">,</span> <span class="nv">parameters</span><span class="p">:</span> <span class="n">parameters</span><span class="p">)</span><span class="o">.</span><span class="mi">0</span>
|
642
|
+
<span class="p">}</span>
|
643
|
+
<span class="p">}</span>
|
644
|
+
</code></pre>
|
645
|
+
<pre><code class="highlight swift"><span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="kt">Router</span><span class="o">.</span><span class="kt">Search</span><span class="p">(</span><span class="nv">query</span><span class="p">:</span> <span class="s">"foo bar"</span><span class="p">,</span> <span class="nv">page</span><span class="p">:</span> <span class="mi">1</span><span class="p">))</span> <span class="c1">// ?q=foo+bar&offset=50
|
646
|
+
</span></code></pre>
|
647
|
+
<a href='#crud_amp_authorization' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h4 id='crud_amp_authorization'>CRUD & Authorization</h4>
|
648
|
+
<pre><code class="highlight swift"><span class="kd">enum</span> <span class="kt">Router</span><span class="p">:</span> <span class="kt">URLRequestConvertible</span> <span class="p">{</span>
|
649
|
+
<span class="kd">static</span> <span class="k">let</span> <span class="nv">baseURLString</span> <span class="o">=</span> <span class="s">"http://example.com"</span>
|
650
|
+
<span class="kd">static</span> <span class="k">var</span> <span class="nv">OAuthToken</span><span class="p">:</span> <span class="kt">String</span><span class="p">?</span>
|
651
|
+
|
652
|
+
<span class="k">case</span> <span class="kt">CreateUser</span><span class="p">([</span><span class="kt">String</span><span class="p">:</span> <span class="kt">AnyObject</span><span class="p">])</span>
|
653
|
+
<span class="k">case</span> <span class="kt">ReadUser</span><span class="p">(</span><span class="kt">String</span><span class="p">)</span>
|
654
|
+
<span class="k">case</span> <span class="kt">UpdateUser</span><span class="p">(</span><span class="kt">String</span><span class="p">,</span> <span class="p">[</span><span class="kt">String</span><span class="p">:</span> <span class="kt">AnyObject</span><span class="p">])</span>
|
655
|
+
<span class="k">case</span> <span class="kt">DestroyUser</span><span class="p">(</span><span class="kt">String</span><span class="p">)</span>
|
656
|
+
|
657
|
+
<span class="k">var</span> <span class="nv">method</span><span class="p">:</span> <span class="kt">Alamofire</span><span class="o">.</span><span class="kt">Method</span> <span class="p">{</span>
|
658
|
+
<span class="k">switch</span> <span class="k">self</span> <span class="p">{</span>
|
659
|
+
<span class="k">case</span> <span class="o">.</span><span class="kt">CreateUser</span><span class="p">:</span>
|
660
|
+
<span class="k">return</span> <span class="o">.</span><span class="kt">POST</span>
|
661
|
+
<span class="k">case</span> <span class="o">.</span><span class="kt">ReadUser</span><span class="p">:</span>
|
662
|
+
<span class="k">return</span> <span class="o">.</span><span class="kt">GET</span>
|
663
|
+
<span class="k">case</span> <span class="o">.</span><span class="kt">UpdateUser</span><span class="p">:</span>
|
664
|
+
<span class="k">return</span> <span class="o">.</span><span class="kt">PUT</span>
|
665
|
+
<span class="k">case</span> <span class="o">.</span><span class="kt">DestroyUser</span><span class="p">:</span>
|
666
|
+
<span class="k">return</span> <span class="o">.</span><span class="kt">DELETE</span>
|
667
|
+
<span class="p">}</span>
|
668
|
+
<span class="p">}</span>
|
669
|
+
|
670
|
+
<span class="k">var</span> <span class="nv">path</span><span class="p">:</span> <span class="kt">String</span> <span class="p">{</span>
|
671
|
+
<span class="k">switch</span> <span class="k">self</span> <span class="p">{</span>
|
672
|
+
<span class="k">case</span> <span class="o">.</span><span class="kt">CreateUser</span><span class="p">:</span>
|
673
|
+
<span class="k">return</span> <span class="s">"/users"</span>
|
674
|
+
<span class="k">case</span> <span class="o">.</span><span class="kt">ReadUser</span><span class="p">(</span><span class="k">let</span> <span class="nv">username</span><span class="p">):</span>
|
675
|
+
<span class="k">return</span> <span class="s">"/users/</span><span class="se">\(</span><span class="n">username</span><span class="se">)</span><span class="s">"</span>
|
676
|
+
<span class="k">case</span> <span class="o">.</span><span class="kt">UpdateUser</span><span class="p">(</span><span class="k">let</span> <span class="nv">username</span><span class="p">,</span> <span class="n">_</span><span class="p">):</span>
|
677
|
+
<span class="k">return</span> <span class="s">"/users/</span><span class="se">\(</span><span class="n">username</span><span class="se">)</span><span class="s">"</span>
|
678
|
+
<span class="k">case</span> <span class="o">.</span><span class="kt">DestroyUser</span><span class="p">(</span><span class="k">let</span> <span class="nv">username</span><span class="p">):</span>
|
679
|
+
<span class="k">return</span> <span class="s">"/users/</span><span class="se">\(</span><span class="n">username</span><span class="se">)</span><span class="s">"</span>
|
680
|
+
<span class="p">}</span>
|
681
|
+
<span class="p">}</span>
|
682
|
+
|
683
|
+
<span class="c1">// MARK: URLRequestConvertible
|
684
|
+
</span>
|
685
|
+
<span class="k">var</span> <span class="nv">URLRequest</span><span class="p">:</span> <span class="kt">NSURLRequest</span> <span class="p">{</span>
|
686
|
+
<span class="k">let</span> <span class="nv">URL</span> <span class="o">=</span> <span class="kt">NSURL</span><span class="p">(</span><span class="nv">string</span><span class="p">:</span> <span class="kt">Router</span><span class="o">.</span><span class="n">baseURLString</span><span class="p">)</span>
|
687
|
+
<span class="k">let</span> <span class="nv">mutableURLRequest</span> <span class="o">=</span> <span class="kt">NSMutableURLRequest</span><span class="p">(</span><span class="kt">URL</span><span class="p">:</span> <span class="kt">URL</span><span class="o">.</span><span class="kt">URLByAppendingPathComponent</span><span class="p">(</span><span class="n">path</span><span class="p">))</span>
|
688
|
+
<span class="n">mutableURLRequest</span><span class="o">.</span><span class="kt">HTTPMethod</span> <span class="o">=</span> <span class="n">method</span><span class="o">.</span><span class="nf">toRaw</span><span class="p">()</span>
|
689
|
+
|
690
|
+
<span class="k">if</span> <span class="k">let</span> <span class="nv">token</span> <span class="o">=</span> <span class="kt">Router</span><span class="o">.</span><span class="kt">OAuthToken</span> <span class="p">{</span>
|
691
|
+
<span class="n">mutableURLRequest</span><span class="o">.</span><span class="nf">setValue</span><span class="p">(</span><span class="s">"Bearer </span><span class="se">\(</span><span class="n">token</span><span class="se">)</span><span class="s">"</span><span class="p">,</span> <span class="nv">forHTTPHeaderField</span><span class="p">:</span> <span class="s">"Authorization"</span><span class="p">)</span>
|
692
|
+
<span class="p">}</span>
|
693
|
+
|
694
|
+
<span class="k">switch</span> <span class="k">self</span> <span class="p">{</span>
|
695
|
+
<span class="k">case</span> <span class="o">.</span><span class="kt">CreateUser</span><span class="p">(</span><span class="k">let</span> <span class="nv">parameters</span><span class="p">):</span>
|
696
|
+
<span class="k">return</span> <span class="kt">Alamofire</span><span class="o">.</span><span class="kt">ParameterEncoding</span><span class="o">.</span><span class="kt">JSON</span><span class="o">.</span><span class="nf">encode</span><span class="p">(</span><span class="n">mutableURLRequest</span><span class="p">,</span> <span class="nv">parameters</span><span class="p">:</span> <span class="n">parameters</span><span class="p">)</span><span class="o">.</span><span class="mi">0</span>
|
697
|
+
<span class="k">case</span> <span class="o">.</span><span class="kt">UpdateUser</span><span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="k">let</span> <span class="nv">parameters</span><span class="p">):</span>
|
698
|
+
<span class="k">return</span> <span class="kt">Alamofire</span><span class="o">.</span><span class="kt">ParameterEncoding</span><span class="o">.</span><span class="kt">URL</span><span class="o">.</span><span class="nf">encode</span><span class="p">(</span><span class="n">mutableURLRequest</span><span class="p">,</span> <span class="nv">parameters</span><span class="p">:</span> <span class="n">parameters</span><span class="p">)</span><span class="o">.</span><span class="mi">0</span>
|
699
|
+
<span class="k">default</span><span class="p">:</span>
|
700
|
+
<span class="k">return</span> <span class="n">mutableURLRequest</span>
|
701
|
+
<span class="p">}</span>
|
702
|
+
<span class="p">}</span>
|
703
|
+
<span class="p">}</span>
|
704
|
+
</code></pre>
|
705
|
+
<pre><code class="highlight swift"><span class="kt">Alamofire</span><span class="o">.</span><span class="nf">request</span><span class="p">(</span><span class="kt">Router</span><span class="o">.</span><span class="kt">ReadUser</span><span class="p">(</span><span class="s">"mattt"</span><span class="p">))</span> <span class="c1">// GET /users/mattt
|
706
|
+
</span></code></pre>
|
707
|
+
|
708
|
+
<hr>
|
709
|
+
<a href='#faq' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h2 id='faq'>FAQ</h2>
|
710
|
+
<a href='#when_should_i_use_alamofire' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='when_should_i_use_alamofire'>When should I use Alamofire?</h3>
|
711
|
+
<p class="para">If you’re starting a new project in Swift, and want to take full advantage of its conventions and language features, Alamofire is a great choice. Although not as fully-featured as AFNetworking, Alamofire is much nicer to work with, and should satisfy the vast majority of networking use cases.</p>
|
712
|
+
<blockquote>
|
713
|
+
<p class="para">It’s important to note that two libraries aren’t mutually exclusive: AFNetworking and Alamofire can peacefully exist in the same code base.</p></blockquote>
|
714
|
+
<a href='#when_should_i_use_afnetworking' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='when_should_i_use_afnetworking'>When should I use AFNetworking?</h3>
|
715
|
+
<p class="para">AFNetworking remains the premiere networking library available for OS X and iOS, and can easily be used in Swift, just like any other Objective-C code. AFNetworking is stable and reliable, and isn’t going anywhere.</p><p class="para">Use AFNetworking for any of the following:</p>
|
716
|
+
<ul>
|
717
|
+
<li>UIKit extensions, such as asynchronously loading images to <code>UIImageView</code></li>
|
718
|
+
<li>TLS verification, using <code>AFSecurityManager</code></li>
|
719
|
+
<li>Situations requiring <code>NSOperation</code> or <code>NSURLConnection</code>, using <code>AFURLConnectionOperation</code></li>
|
720
|
+
<li>Network reachability monitoring, using <code>AFNetworkReachabilityManager</code></li>
|
721
|
+
<li>Multipart HTTP request construction, using <code>AFHTTPRequestSerializer</code></li>
|
722
|
+
</ul>
|
723
|
+
<a href='#what_39_s_the_origin_of_the_name_alamofire' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='what_39_s_the_origin_of_the_name_alamofire'>What’s the origin of the name Alamofire?</h3>
|
724
|
+
<p class="para">Alamofire is named after the <a href="https://aggie-horticulture.tamu.edu/wildseed/alamofire.html">Alamo Fire flower</a>, a hybrid variant of the Bluebonnet, the official state flower of Texas.</p>
|
725
|
+
<hr>
|
726
|
+
<a href='#contact' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h2 id='contact'>Contact</h2>
|
727
|
+
<p class="para">Follow AFNetworking on Twitter (<a href="https://twitter.com/AFNetworking">@AFNetworking</a>)</p><a href='#creator' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h3 id='creator'>Creator</h3>
|
728
|
+
|
729
|
+
<ul>
|
730
|
+
<li><a href="http://github.com/mattt">Mattt Thompson</a> (<a href="https://twitter.com/mattt">@mattt</a>)</li>
|
731
|
+
</ul>
|
732
|
+
<a href='#license' class='anchor' aria-hidden=true><span class="header-anchor"></span></a><h2 id='license'>License</h2>
|
733
|
+
<p class="para">Alamofire is released under the MIT license. See LICENSE for details.</p></div>
|
163
734
|
</section>
|
164
735
|
</section>
|
165
736
|
<section id="footer">
|