我遇到了對EatStreet API的AlamoFire請求的問題。我有一個API密鑰,並且正確導入了Alamofire。Alamofire API連接和Swift
https://developers.eatstreet.com/endpoint/search
下面是鏈接到的網站,他們將創建API URL字符串爲您服務。
即使這一切,我仍然沒有成功
這是樣本串
curl -X GET \
-H 'X-Access-Token: __API_EXPLORER_AUTH_KEY__' \
'https://api.eatstreet.com/publicapi/v1/restaurant/search?latitude=40.718293&longitude=-74.002276&method=pickup&pickup-radius=2'
下面是我用我的API和Alamofire請求示例代碼。它不斷返回結果值爲false。任何幫助表示讚賞
import UIKit
import Firebase
import FirebaseDatabase
import FirebaseAuth
import Alamofire
import SwiftyJSON
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(LoginViewController.dismissKeyboard))
//Uncomment the line below if you want the tap not not interfere and cancel other interactions.
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
let apiToContact = "\\ -H 'X-Access-Token: f6565a0360167144' \\'https://api.eatstreet.com/publicapi/v1/restaurant/search?latitude=40.718293&longitude=-74.002276&method=pickup&pickup-radius=2'"
Alamofire.request(apiToContact).validate().responseJSON() { response in
switch response.result {
case .success:
if let value = response.result.value {
let json = JSON(value)
// Do what you need to with JSON here!
// The rest is all boiler plate code you'll use for API requests
print(json)
}
case .failure(let error):
print(error)
}
}
}
//Calls this function when the tap is recognized.
func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
啊剛纔讀它謝謝 – HiDungLing
@HiDungLing很大,請標記爲正確答案;) – xmhafiz