FoursquareAPIClient
FoursquareAPIClient
是一个非常简单的用于 Foursquare API v2 的 Swift 网络库。
安装
CocoaPods
首先,将以下行添加到您的 Podfile 中
pod 'FoursquareAPIClient'
其次,将 FoursquareAPIClient
安装到您的项目中
pod install
Carthage
- 将其添加到您的 Cartfile 中
github "koogawa/FoursquareAPIClient"
- 运行
carthage update --platform iOS
- 将 'FoursquareAPIClient.framework' 添加到您的项目中的 '链接框架和库'
- 将
/usr/local/bin/carthage copy-frameworks
添加到 '新的运行脚本阶段' - 将
$(SRCROOT)/Carthage/Build/iOS/FoursquareAPIClient.framework
添加到 '输入文件'
手动
将 FoursquareAPIClient 文件夹中的所有文件复制到您的项目中。
- FoursquareAPIClient.swift
- FoursquareAuthClient.swift(可选)
- FoursquareAuthViewController.swift(可选)
用法
导入
import FoursquareAPIClient
设置会话
let client = FoursquareAPIClient(accessToken: "YOUR_ACCESS_TOKEN")
或者
let client = FoursquareAPIClient(clientId: "YOUR_CLIENT_ID", clientSecret: "YOUR_CLIENT_SECRET")
版本控制
// Set v=YYYYMMDD param
let client = FoursquareAPIClient(accessToken: "YOUR_ACCESS_TOKEN", version: "20140723")
或者
let client = FoursquareAPIClient(clientId: "YOUR_CLIENT_ID", clientSecret: "YOUR_CLIENT_SECRET”,
version: "20140723”)
搜索场所
let parameter: [String: String] = [
"ll": "35.702069,139.7753269",
"limit": "10",
];
client.request(path: "venues/search", parameter: parameter) { result in
switch result {
case let .success(data):
// parse the JSON data with NSJSONSerialization or Lib like SwiftyJson
// e.g. {"meta":{"code":200},"notifications":[{"...
let json = try! JSONSerialization.jsonObject(with: data, options: [])
case let .failure(error):
// Error handling
switch error {
case let .connectionError(connectionError):
print(connectionError)
case let .responseParseError(responseParseError):
print(responseParseError) // e.g. JSON text did not start with array or object and option to allow fragments not set.
case let .apiError(apiError):
print(apiError.errorType) // e.g. endpoint_error
print(apiError.errorDetail) // e.g. The requested path does not exist.
}
}
}
签到场所
let parameter: [String: String] = [
"venueId": "55b731a9498eecdfb"3854a9”,
"ll": "37.33262674912818,-122.030451055438",
"alt": "10”,
];
client.request(path: "checkins/add", method: .post, parameter: parameter) { result in
switch result {
case let .success(data):
// parse the JSON data with NSJSONSerialization or Lib like SwiftyJson
// e.g. {"meta":{"code":200},"notifications":[{"...
let json = try! JSONSerialization.jsonObject(with: data, options: [])
case let .failure(error):
// Error handling
switch error {
case let .connectionError(connectionError):
print(connectionError)
case let .responseParseError(responseParseError):
print(responseParseError) // e.g. JSON text did not start with array or object and option to allow fragments not set.
case let .apiError(apiError):
print(apiError.errorType) // e.g. endpoint_error
print(apiError.errorDetail) // e.g. The requested path does not exist.
}
}
}
添加照片
let parameter: [String: String] = [
"checkinId": "IHR8THISVNU",
"broadcast": "twitter,facebook",
"postText": "Awesome!",
];
let yourImage = UIImage(named: "photo")
let imageData = UIImageJPEGRepresentation(yourImage!, 1)
client.upload(path: "photos/add", parameter: parameter, imageData: imageData!) {
result in
switch result {
case let .success(data):
// Upload success
case let .failure(error):
// Upload error
}
}
授权
设置
let client = FoursquareAuthClient(clientId: "YOUR_CLIENT_ID",
callback: "YOUR_CALLBACK_URL",
delegate: self)
委托
func foursquareAuthClientDidSucceed(accessToken: String) {
print(accessToken)
}
func foursquareAuthClientDidFail(error: NSError) {
print(error.description)
}
要求
Swift 5.0 / iOS 8.0+
创建者
许可证
MIT许可证。详细信息请参阅License.txt。