【发布时间】:2017-08-16 19:05:07
【问题描述】:
我对 Swift 很陌生,我正在尝试使用使用者密钥和使用者秘密向 URL 发出 http 请求,老实说,我不确定这在 Swift 中是否可行。
我一直在尝试使用身份验证方法进行这项工作,但它只会给我一个错误。
我的代码(希望这可以帮助您理解我正在尝试做的事情..)
let baseUrl = "my url"
let consumer_key = "consumer_key"
let consumer_secret = "consumer_secret"
let loginString = NSString(format:"%@:%@", consumer_key, consumer_secret)
let loginData = loginString.data(using: String.Encoding.utf8.rawValue)!
let base64LoginString = loginData.base64EncodedString()
let url = URL(string: baseUrl)
var request = URLRequest(url: url!)
request.httpMethod = "GET"
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
let config = URLSessionConfiguration.default
let authString = "Basic \(base64LoginString)"
config.httpAdditionalHeaders = ["Authorization" : authString]
let session = URLSession(configuration: config)
session.dataTask(with: url!) {
(data, response, error) in
if (response as? HTTPURLResponse) != nil {
print("in session")
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("data please...",dataString!)
}
}.resume()
如果这个带有消费者密钥和消费者秘密的 http 调用在 Swift 中是完全不可接受的,或者如果我有任何其他方法可以通过这个,请告诉我。
先谢谢你了。
编辑------------------------------ --------
import UIKit
class OrdersViewController: UIViewController {
@IBOutlet weak var orderView: UITableView!
var orderData = [[String: AnyObject]]()
var selectedIndex = -1
override func viewDidLoad() {
super.viewDidLoad()
print("in")
// Do any additional setup after loading the view.
let baseUrl = "my url"
let consumer_key = "consumer_key"
let consumer_secret = "consumer_secret"
let loginString = NSString(format:"%@:%@", consumer_key, consumer_secret)
let loginData = loginString.data(using: String.Encoding.utf8.rawValue)!
let base64LoginString = loginData.base64EncodedString()
let url = URL(string: baseUrl)
var request = URLRequest(url: url!)
request.httpMethod = "GET"
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
let config = URLSessionConfiguration.default
let authString = "Basic \(base64LoginString)"
config.httpAdditionalHeaders = ["Authorization" : authString]
let session = URLSession(configuration: config)
session.dataTask(with: url!) {
(data, response, error) in
if (response as? HTTPURLResponse) != nil {
print("in session")
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("data please...",dataString!)
}
}.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return orderData.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if (selectedIndex == indexPath.row) {
return 100
} else {
return 40
}
}
func tableView(tableView: UITableView, cellforRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! orderCell
let obj = orderData[indexPath.row]
print("obj", obj)
return cell
}
}
【问题讨论】:
-
就代码而言,您的代码看起来不错。身份验证机制本身可能取决于您正在访问的服务器。但是您是否看到任何错误,或者您只是想验证您的代码是否遵循可接受的编码准则?
-
我不认为它不会运行 session.dataTask 因为我在我的窗口中没有看到我的“会话中”和“请数据...”。我看到一些错误,例如“无法识别的选择器发送到实例 0x7fecb1800490”和“应用程序传输安全已阻止明文 HTTP (http://) 资源加载,因为它不安全。可以通过应用程序的 Info.plist 文件配置临时异常。”最后“以 NSException 类型的未捕获异常终止”
标签: swift api http authentication