GoBus! 是一个库,帮助开发者使用 Swift 与 Inthegra API 进行交互。我们提供了一种简单的方式来获取数据和一些在 API 上未实现的新功能。
按照以下三个步骤运行示例项目:克隆 GoBus 仓库,打开示例工作区并运行 示例 项目。
设置认证令牌很简单,就像这样
import UIKit
import GoBus
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
GoBus().setupWithApiKey(apiKey: String, email: String, password: String, url: String)
return true
}
}
在这个例子中,我们只是设置了您的应用程序密钥、电子邮件和密码。有了这些,您将自动登录,访问令牌会自动获取和更新。
现在您可以使用 GoBus 函数了。
我们可以通过调用以下三个 GoBus
函数来获取总线值
GoBus().getBus(completion: (([Bus]?, [Line]?, NSError?) -> Void))
GoBus().getBus() { (buss, lines, error) in
if error != nil {
print(error?.code)
print(error?.domain)
}else {
if lines != nil {
for line in lines! {
print(line.description)
}
}
if buss != nil {
for bus in buss! {
print(bus.description)
}
}
}
}
将返回包含总线对象、包含线路对象的数组和 NSError
GoBus().getBus(inLine: String?, completion: (([Bus]?, [Line]?, NSError?) -> Void))
GoBus().getBus(inLine: "0408") { (buss, line, error) in
if error != nil {
print(error?.code)
print(error?.domain)
}else {
if line != nil {
//We received only one Line, which correspond to the line researched
print(line![0].description)
}
// - buss - correspond to all Bus in the line researched
if buss != nil {
for bus in buss! {
print(bus.description)
}
}
}
}
传递您希望返回的线路编号,并将收到包含此线路总线对象的数组、包含线路的数组和 NSError
//** repeatAfter works in Seconds
GoBus().getBus(inLine: String?, repeatAfter: Double?, completion: (([Bus]?, [Line]?, NSError?) -> Void))
//Usage:
// 1: GoBus().getBus(inLine: "0408", repeatAfter: 30, completion: (([Bus]?, [Line]?, NSError?) -> Void)) with Search
// 2: GoBus().getBus(repeatAfter: 30, completion: (([Bus]?, [Line]?, NSError?) -> Void)) without Search
// 3: In this case, you can use either the first way as the second
// - First, add GoBusDelegate in the class
let go = GoBus()
//Returns both the bus as the line now. From now on, every five seconds the function implemented by the protocol will return updated values.
let cancel = go.getBus(inLine: "0408", repeatAfter: 5) { (buss, line, error) in }
//Using this way (3), now you can cancel repeat After and the call protocol
go.cancel(cancel)
奥兰多·阿莫里姆,[email protected]
GoBus可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。