Alamofire: Elegant Networking in Swift

Swift Platforms CocoaPods Compatible Carthage Compatible Swift Package Manager Swift Forums

Alamofire 是一个用 Swift 编写的 HTTP 网络库。

特性

快速编写请求!

Alamofire 的简洁语法和广泛的功能集允许用几行代码编写具有强大功能的请求,例如自动重试。

// Automatic String to URL conversion, Swift concurrency support, and automatic retry.
let response = await AF.request("https://httpbin.org/get", interceptor: .retryPolicy)
                       // Automatic HTTP Basic Auth.
                       .authenticate(username: "user", password: "pass")
                       // Caching customization.
                       .cacheResponse(using: .cache)
                       // Redirect customization.
                       .redirect(using: .follow)
                       // Validate response code and Content-Type.
                       .validate()
                       // Produce a cURL command for the request.
                       .cURLDescription { description in
                         print(description)
                       }
                       // Automatic Decodable support with background parsing.
                       .serializingDecodable(DecodableType.self)
                       // Await the full response with metrics and a parsed body.
                       .response
// Detailed response description for easy debugging.
debugPrint(response)

组件库

为了使 Alamofire 专注于核心网络实现,Alamofire Software Foundation 创建了额外的组件库,为 Alamofire 生态系统带来更多功能。

要求

平台 最低 Swift 版本 安装 状态
iOS 10.0+ / macOS 10.12+ / tvOS 10.0+ / watchOS 3.0+ 5.9 / Xcode 15.0 CocoaPods, Carthage, Swift Package Manager, 手动 完全测试
Linux 仅最新版本 Swift Package Manager 构建但不支持
Windows 仅最新版本 Swift Package Manager 构建但不支持
Android 仅最新版本 Swift Package Manager 构建但不支持

Linux 和 Windows 上的已知问题

Alamofire 可以在 Linux、Windows 和 Android 上构建,但在底层 swift-corelibs-foundation 中缺少功能并且存在许多问题,这些问题阻碍了完整的功能并可能导致崩溃。 这些包括

由于这些问题,Alamofire 在 Linux、Windows 和 Android 上不受支持。 请将任何崩溃报告给 Swift 错误报告器

迁移指南

沟通交流

安装

Swift Package Manager

Swift Package Manager 是一种用于自动分发 Swift 代码的工具,并已集成到 swift 编译器中。

设置好 Swift 包后,将 Alamofire 添加为依赖项就像将其添加到 Package.swiftdependencies 值或 Xcode 中的 Package 列表一样简单。

dependencies: [
    .package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.10.0"))
]

通常,您需要依赖 Alamofire 目标

.product(name: "Alamofire", package: "Alamofire")

但是,如果您想强制 Alamofire 进行动态链接(除非您确定需要,否则不要这样做),您可以依赖 AlamofireDynamic 目标

.product(name: "AlamofireDynamic", package: "Alamofire")

CocoaPods

CocoaPods 是 Cocoa 项目的依赖项管理器。 有关使用和安装说明,请访问他们的网站。 要使用 CocoaPods 将 Alamofire 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

pod 'Alamofire'

Carthage

Carthage 是一个分散的依赖项管理器,它构建您的依赖项并为您提供二进制框架。 要使用 Carthage 将 Alamofire 集成到您的 Xcode 项目中,请在您的 Cartfile 中指定它

github "Alamofire/Alamofire"

手动

如果您不想使用上述任何依赖项管理器,您可以手动将 Alamofire 集成到您的项目中。

嵌入式框架

贡献

在贡献 Alamofire 之前,请阅读我们的贡献指南中详细说明的说明。

开放 Radar

以下雷达(Radar,指 Apple 提交的错误报告)对 Alamofire 的当前实现产生了一些影响。

已解决的雷达

以下雷达在针对 Alamofire 项目提交后,随着时间的推移已得到解决。

FAQ

Alamofire 这个名字的由来是什么?

Alamofire 以 阿拉莫火焰花 (Alamo Fire flower) 命名,它是德克萨斯州官方州花蓝帽花的杂交变种。

鸣谢

Alamofire 由 Alamofire 软件基金会 拥有和维护。您可以在 Twitter 上关注他们 @AlamofireSF,以获取项目更新和版本发布信息。

安全披露

如果您认为您发现了 Alamofire 的安全漏洞,您应尽快通过电子邮件报告至 security@alamofire.org。请不要将其发布到公共问题跟踪器。

赞助

ASF 正在筹集资金,以便正式注册为联邦非营利组织。注册将允许基金会成员获得一些法律保护,并允许我们免税地使用捐款。赞助 ASF 将使我们能够:

社区对 ASF 库的采用令人惊叹。 我们对您对这些项目的热情感到非常荣幸,并希望继续尽一切努力来推动事情向前发展。 在您持续的支持下,ASF 将能够扩大其影响力,并为核心成员提供更好的法律保障。 如果您在工作中使用我们的任何库,请询问您的雇主是否有兴趣捐款。 任何您能捐赠的金额,无论是单次还是每月捐赠,都将非常感谢您帮助我们实现目标。

赞助 Alamofire

支持者

MacStadium 为 Alamofire 提供免费的托管 Mac mini。

Powered by MacStadium

许可证

Alamofire 在 MIT 许可证下发布。请参阅 LICENSE 了解详细信息。