1
データにアクセスするにはJWTトークンを渡す必要があります。どうすればいいのですか? コンソールにトークンが表示されていますが、tableViewには何もありません。 私が実際に欲しいのです: - あなたは次のようにトークンを配置する必要がコースをダウンロードするために、ディスプレイ関連のコースSwift 3でJWTを読んで渡す
import UIKit
import Alamofire
class AvailableCoursesTableViewController: UITableViewController {
let url = "https://api.sis.kemoke.net/auth/login"
var parameters = ["email": "[email protected]", "password": "passwd"]
var token : HTTPHeaders = ["X-Auth-Token": ""]
//Custom struct for the data
struct Courses {
let course : String
init(dictionary: [String:String]) {
self.course = dictionary["course"] ?? ""
}
}
//Array which holds the courses
var courseData = [Courses]()
func authenticate() {
Alamofire.request(url, method: .post, parameters: parameters, encoding: URLEncoding.httpBody, headers: nil).responseJSON {
(response) in
self.token["X-Auth-Token"] = response.description
print(self.token)
}
}
// Download the courses
func downloadData() {
Alamofire.request("https://api.sis.kemoke.net/student/course", headers: token).responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
//Optional binding to handle exceptions
self.courseData.removeAll() // clean the data source array
if let json = response.result.value as? [[String:String]] {
for course in json {
self.courseData.append(Courses(dictionary: course))
}
self.tableView.reloadData()
}
}
}