Ios 在Swift 3中阅读和传递JWT
我需要传递JWT令牌才能访问数据。我该怎么做? 我在控制台中看到了令牌,但tableView中没有任何内容。 我真正想要的是: -对用户进行身份验证 -获取JWT令牌 -展示相关课程Ios 在Swift 3中阅读和传递JWT,ios,swift,Ios,Swift,我需要传递JWT令牌才能访问数据。我该怎么做? 我在控制台中看到了令牌,但tableView中没有任何内容。 我真正想要的是: -对用户进行身份验证 -获取JWT令牌 -展示相关课程 import UIKit import Alamofire class AvailableCoursesTableViewController: UITableViewController { let url = "https://api.sis.kemoke.net/au
import UIKit
import Alamofire
class AvailableCoursesTableViewController: UITableViewController {
let url = "https://api.sis.kemoke.net/auth/login"
var parameters = ["email": "kemoke@hotmail.com", "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()
}
}
}
要下载课程,您必须按以下方式放置令牌:
// Where otherParameter is any other parameter you want to pass
func downloadData(token : String, otherParameter : String)
{
let url: String = "https://api.sis.kemoke.net/student/course"
var request = URLRequest(url: NSURL(string: url) as! URL)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let values: [String: Any] = ["otherParameter": otherParameter,"token": token]
request.httpBody = try! JSONSerialization.data(withJSONObject: values)
Alamofire.request(request)
.response { response in
let jsonData = JSON(data: response.data!)
print(jsonData)
print(response)
//Error check
//check if error is returned
if (response.error == nil) {
// Write your required functionality here
}
}
要下载课程,您必须按以下方式放置令牌:
// Where otherParameter is any other parameter you want to pass
func downloadData(token : String, otherParameter : String)
{
let url: String = "https://api.sis.kemoke.net/student/course"
var request = URLRequest(url: NSURL(string: url) as! URL)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let values: [String: Any] = ["otherParameter": otherParameter,"token": token]
request.httpBody = try! JSONSerialization.data(withJSONObject: values)
Alamofire.request(request)
.response { response in
let jsonData = JSON(data: response.data!)
print(jsonData)
print(response)
//Error check
//check if error is returned
if (response.error == nil) {
// Write your required functionality here
}
}
这不是关于JWT令牌的问题,这是关于特定API需要某些参数的问题,请更新问题这不是关于JWT令牌的问题,这是关于特定API需要某些参数的问题,请更新问题