![Alamofire: Elegant Networking in Swift](https://raw.githubusercontent.com/Alamofire/Alamofire/assets/alamofire.png)
Alamofire is an HTTP networking library written in Swift.
## Features
- [x] Chainable Request / Response methods
- [x] URL / JSON / plist Parameter Encoding
- [x] Upload File / Data / Stream
- [x] Download using Request or Resume data
- [x] Authentication with NSURLCredential
- [x] HTTP Response Validation
- [x] Progress Closure & NSProgress
- [x] cURL Debug Output
- [x] Comprehensive Unit Test Coverage
- [x] Complete Documentation
## Requirements
- iOS 7.0+ / Mac OS X 10.9+
- Xcode 6.3
## Communication
- If you **need help**, use [Stack Overflow](http://stackoverflow.com/questions/tagged/alamofire). (Tag 'alamofire')
- If you'd like to **ask a general question**, use [Stack Overflow](http://stackoverflow.com/questions/tagged/alamofire).
- If you **found a bug**, open an issue.
- If you **have a feature request**, open an issue.
- If you **want to contribute**, submit a pull request.
## Installation
> **Embedded frameworks require a minimum deployment target of iOS 8 or OS X Mavericks.**
>
> To use Alamofire with a project targeting iOS 7, you must include the `Alamofire.swift` source file directly in your project. See the ['Source File'](#source-file) section for instructions.
### CocoaPods
[CocoaPods](http://cocoapods.org) is a dependency manager for Cocoa projects.
CocoaPods 0.36 adds supports for Swift and embedded frameworks. You can install it with the following command:
```bash
$ gem install cocoapods
```
To integrate Alamofire into your Xcode project using CocoaPods, specify it in your `Podfile`:
```ruby
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'Alamofire', '~> 1.2'
```
Then, run the following command:
```bash
$ pod install
```
### Carthage
Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
You can install Carthage with [Homebrew](http://brew.sh/) using the following command:
```bash
$ brew update
$ brew install carthage
```
To integrate Alamofire into your Xcode project using Carthage, specify it in your `Cartfile`:
```ogdl
github "Alamofire/Alamofire" >= 1.2
```
### Manually
If you prefer not to use either of the aforementioned dependency managers, you can integrate Alamofire into your project manually.
#### Embedded Framework
- Add Alamofire as a [submodule](http://git-scm.com/docs/git-submodule) by opening the Terminal, `cd`-ing into your top-level project directory, and entering the following command:
```bash
$ git submodule add https://github.com/Alamofire/Alamofire.git
```
- Open the new `Alamofire` folder, and drag the `Alamofire.xcodeproj` into the Project Navigator of your application's Xcode project.
> It should appear nested underneath your application's blue project icon. Whether it is above or below all the other Xcode groups does not matter.
- Select the `Alamofire.xcodeproj` in the Project Navigator and verify the deployment target matches that of your application target.
- Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
- In the tab bar at the top of that window, open the "General" panel.
- Click on the `+` button under the "Embedded Binaries" section.
- You will see two different `Alamofire.xcodeproj` folders each with two different versions of the `Alamofire.framework` nested inside a `Products` folder.
> It does not matter which `Products` folder you choose from, but it does matter whether you choose the top or bottom `Alamofire.framework`.
- Select the top `Alamofire.framework` for iOS and the bottom one for OS X.
> You can verify which one you selected by inspecting the build log for your project. The build target for `Alamofire` will be listed as either `Alamofire iOS` or `Alamofire OSX`.
- And that's it!
> The `Alamofire.framework` is automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.
#### Source File
For application targets that do not support embedded frameworks, such as iOS 7, Alamofire can be integrated by adding the `Alamofire.swift` source file directly into your project. Note that you will no longer need to `import Alamofire` since you are not actually loading a framework. Additionally, any of the calling conventions described in the ['Usage'](#usage) section with the `Alamofire` prefix would instead omit it (for example, `Alamofire.request` becomes `request`), since this functionality is incorporated into the top-level namespace.
---
## Usage
### Making a Request
```swift
import Alamofire
Alamofire.request(.GET, "http://httpbin.org/get")
```
### Response Handling
```swift
Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
.response { (request, response, data, error) in
println(request)
println(response)
println(error)
}
```
> Networking in Alamofire is done _asynchronously_. Asynchronous programming may be a source of frustration to programmers unfamiliar with the concept, but there are [very good reasons](https://developer.apple.com/library/ios/qa/qa1693/_index.html) for doing it this way.
> Rather than blocking execution to wait for a response from the server, a [callback](http://en.wikipedia.org/wiki/Callback_%28computer_programming%29) 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.
### Response Serialization
**Built-in Response Methods**
- `response()`
- `responseString(encoding: NSStringEncoding)`
- `responseJSON(options: NSJSONReadingOptions)`
- `responsePropertyList(options: NSPropertyListReadOptions)`
#### Response String Handler
```swift
Alamofire.request(.GET, "http://httpbin.org/get")
.responseString { (_, _, string, _) in
println(string)
}
```
#### Response JSON Handler
```swift
Alamofire.request(.GET, "http://httpbin.org/get")
.responseJSON { (_, _, JSON, _) in
println(JSON)
}
```
#### Chained Response Handlers
Response handlers can even be chained:
```swift
Alamofire.request(.GET, "http://httpbin.org/get")
.responseString { (_, _, string, _) in
println(string)
}
.responseJSON { (_, _, JSON, _) in
println(JSON)
}
```
### HTTP Methods
`Alamofire.Method` lists the HTTP methods defined in [RFC 7231 §4.3](http://tools.ietf.org/html/rfc7231#section-4.3):
```swift
public enum Method: String {
case OPTIONS = "OPTIONS"
case GET = "GET"
case HEAD = "HEAD"
case POST = "POST"
case PUT = "PUT"
case PATCH = "PATCH"
case DELETE = "DELETE"
case TRACE = "TRACE"
case CONNECT = "CONNECT"
}
```
These values can be passed as the first argument of the `Alamofire.request` method:
```swift
Alamofire.request(.POST, "http://httpbin.org/post")
Alamofire.request(.PUT, "http://httpbin.org/put")
Alamofire.request(.DELETE, "http://httpbin.org/delete")
```
### Parameters
#### GET Request With URL-Encoded Parameters
```swift
Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
// http://httpbin.org/get?foo=bar
```
#### POST Request With URL-Encoded Parameters
```swift
let parameters = [
"foo": "bar",
"baz": ["a", 1],
"qux": [
"x": 1,
"y": 2,
"z": 3
]
]
Alamofire.request(.POST, "http://httpbin.org/post", parameters: parameters)
// HTTP body: foo=bar&baz[]=a&baz[]=1&qux[x]=1&qux[y]=2
没有合适的资源?快使用搜索试试~ 我知道了~
基于swift的实战项目
共652个文件
png:103个
h:84个
json:71个
需积分: 5 0 下载量 51 浏览量
2024-05-17
23:40:06
上传
评论
收藏 8.38MB ZIP 举报
温馨提示
项目背景: 为了学习和掌握Swift语言及iOS应用开发技能,开发了一款基于Swift的天气预报应用。该应用通过调用天气API,为用户提供实时天气信息和未来天气预报,帮助用户随时掌握天气动态。 项目目标: 熟悉Swift编程语言和iOS开发环境。 实现一个功能齐全、界面友好的天气预报应用。 提升移动端开发和网络数据处理能力。 主要功能: 实时天气:获取并显示用户当前位置的实时天气信息,包括温度、湿度、风速等。 未来预报:提供未来几天的天气预报,帮助用户进行出行计划。 位置搜索:支持用户搜索其他城市的天气信息。 天气图标:根据天气情况显示相应的天气图标和背景。 数据刷新:提供手动刷新功能,确保天气数据的实时性。 技术实现: 开发环境:使用Xcode作为开发工具,编写Swift代码。 网络请求:利用URLSession库发送网络请求,调用天气API获取数据。 用户界面:使用UIKit框架设计界面,构建视图控制器、标签、按钮等UI组件。 数据解析:使用Codable协议解析JSON格式的天气数据。 位置服务:集成Core Location框架获取用户当前位置。
资源推荐
资源详情
资源评论
收起资源包目录
基于swift的实战项目 (652个子文件)
AFURLSessionManager.h 30KB
AFURLRequestSerialization.h 22KB
AFHTTPRequestOperationManager.h 19KB
AFURLConnectionOperation.h 17KB
AFHTTPSessionManager.h 15KB
AFURLResponseSerialization.h 12KB
UIButton+AFNetworking.h 9KB
UIView+DCAnimationKit.h 8KB
AFNetworkReachabilityManager.h 8KB
UIImageView+AFNetworking.h 7KB
UIWebView+AFNetworking.h 6KB
UIAlertView+AFNetworking.h 5KB
AFSecurityPolicy.h 5KB
AFHTTPRequestOperation.h 4KB
AFNetworkActivityIndicatorManager.h 4KB
UIProgressView+AFNetworking.h 3KB
Reachability.h 3KB
FMMosaicLayout.h 3KB
UIActivityIndicatorView+AFNetworking.h 2KB
UIRefreshControl+AFNetworking.h 2KB
Pods-environment.h 2KB
AFNetworking.h 2KB
UIKit+AFNetworking.h 2KB
ProgressBarView.h 731B
Pods-environment.h 440B
Pods-environment.h 440B
Pods-environment.h 430B
Pods-environment.h 415B
AppDelegate.h 291B
AppDelegate.h 288B
ViewController.h 264B
ViewController.h 229B
Pods-FMMosaicLayout-umbrella.h 177B
DCAnimationKitDemo-Bridging-Header.h 154B
MosaicLayout-Bridging-Header.h 145B
AFNetworkingDemo-Bridging-Header.h 143B
ReachabilityDemo-Bridging-Header.h 143B
Pods-Alamofire-umbrella.h 140B
Pods-umbrella.h 130B
Pods-umbrella.h 130B
UIActivityIndicatorView+AFNetworking.h 79B
UIActivityIndicatorView+AFNetworking.h 79B
AFNetworkActivityIndicatorManager.h 76B
AFNetworkActivityIndicatorManager.h 76B
UIRefreshControl+AFNetworking.h 72B
UIRefreshControl+AFNetworking.h 72B
UIProgressView+AFNetworking.h 70B
UIProgressView+AFNetworking.h 70B
UIAlertView+AFNetworking.h 67B
UIImageView+AFNetworking.h 67B
UIAlertView+AFNetworking.h 67B
UIImageView+AFNetworking.h 67B
AFHTTPRequestOperationManager.h 66B
AFHTTPRequestOperationManager.h 66B
UIWebView+AFNetworking.h 65B
AFNetworkReachabilityManager.h 65B
UIWebView+AFNetworking.h 65B
AFNetworkReachabilityManager.h 65B
UIButton+AFNetworking.h 64B
UIButton+AFNetworking.h 64B
AFURLResponseSerialization.h 63B
AFURLResponseSerialization.h 63B
AFURLRequestSerialization.h 62B
AFURLRequestSerialization.h 62B
UIKit+AFNetworking.h 61B
AFURLConnectionOperation.h 61B
UIKit+AFNetworking.h 61B
AFURLConnectionOperation.h 61B
AFHTTPRequestOperation.h 59B
AFHTTPRequestOperation.h 59B
AFHTTPSessionManager.h 57B
AFHTTPSessionManager.h 57B
AFURLSessionManager.h 56B
AFURLSessionManager.h 56B
AFSecurityPolicy.h 53B
AFSecurityPolicy.h 53B
FMMosaicLayout.h 52B
FMMosaicLayout.h 52B
AFNetworking.h 49B
AFNetworking.h 49B
UIView+DCAnimationKit.h 47B
UIView+DCAnimationKit.h 47B
Reachability.h 36B
Reachability.h 36B
ym3.jpg 316KB
ym10.jpg 211KB
ym9.jpg 119KB
ym2.jpg 102KB
ym5.jpg 73KB
ym1.jpg 65KB
ym7.jpg 48KB
ym6.jpg 48KB
ym4.jpg 42KB
ym11.jpg 40KB
ym8.jpg 34KB
logo.jpg 24KB
detail.jpg 4KB
Contents.json 1KB
Contents.json 1KB
Contents.json 1KB
共 652 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
阿吉的呓语
- 粉丝: 1692
- 资源: 160
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功