SwiftPM compatible Carthage compatible Build Status CocoaPods Compatible Platform Twitter

IPAPI

http://ip-api.com 使用 Swift 编写的地理位置 API 客户端。

要求

安装

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 Package Manager

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

一旦你设置了 Swift 包,添加 IPAPI 作为依赖项就像将其添加到你的 Package.swiftdependencies 值一样简单。

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).")
    }
}

联系方式

如果你正在使用或喜欢这个产品,请告诉我。