IPAPI
http://ip-api.com Swift 编写的地理定位 API 客户端。
需求
- iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+
- Xcode 10.2+
- Swift 5.1+
安装
Carthage
Carthage 是一个去中心化的依赖关系管理器,它构建您的依赖关系并提供二进制框架。
您可以使用以下命令通过 Homebrew 安装 Carthage
$ brew update
$ brew install carthage
要使用 Carthage 将 IPAPI 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "arturgrigor/IPAPI" ~> 3.0
运行 carthage update
来构建框架,并将构建的 IPAPI.framework
拖动到您的 Xcode 项目中。
CocoaPods
CocoaPods 是一个用于 Cocoa 项目的依赖管理工具。您可以使用以下命令安装它
$ gem install cocoapods
需要 CocoaPods 1.1.0+。
要使用 CocoaPods 将 IPAPI 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'IPAPI', '~> 3.0'
end
然后,运行以下命令
$ pod install
Swift 包管理器
Swift 包管理器 是一个用于自动化 Swift 代码分发的工具,并集成到 swift
编译器中。
设置好 Swift 包后,将 IPAPI 添加为依赖项就像将其添加到您的 Package.swift
的 dependencies
值一样简单。
dependencies: [
.Package(url: "https://github.com/arturgrigor/IPAPI.git", majorVersion: 3)
]
使用说明
🎯 查找当前 IP 地址
import IPAPI
IPAPI.Service.default.fetch {
if let result = try? $0.get() {
print("Geo IP result \(result).")
}
}
🔍 查找域名
import IPAPI
Service.default.fetch(query: "apple.com") {
if let result = try? $0.get() {
print("Geo IP result \(result).")
}
}
✂️ 只请求某些特定字段
import IPAPI
Service.default.fetch(fields: [.ip, .latitude, .longitude, .organization]) {
if let result = try? $0.get() {
print("Geo IP result \(result).")
}
}
🇷🇴 本地化
import IPAPI
Service.default.fetch(language: "es") {
if let result = try? $0.get() {
print("Geo IP result \(result).")
}
}
请访问此 页面 了解可用的语言。
📦 批量请求
import IPAPI
Service.default.batch([Service.Request(query: "208.80.152.201",
fields: [.countryName, .countryCode, .latitude, .longitude, .organization, .ip]),
Service.Request(query: "91.198.174.192", language: "es")]) {
if let result = try? $0.get() {
print("Geo IP result \(result).")
}
}
🔒 HTTPS 请求
要使用 HTTPS 通过 API,您必须注册 "Pro" 计划 获取 API 密钥。完成此操作后,您可以像这样设置 API 密钥,并且所有请求都将使用 HTTPS。
import IPAPI
IPAPI.Service(pricingPlan: .pro(apiKey: "test-demo-pro")).fetch {
if let result = try? $0.get() {
print("Geo IP result \(result).")
}
}
联系
告诉我您是否在使用或享受这个产品。