我是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中是完全不可接受的,或者我有任何其他方式可以通过此方式获取,请告诉我。
先谢谢你。
EDIT ---------------------------------------------- --------
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
}
}
答案 0 :(得分:1)
根据您的回复,您看起来好像遇到了ATS问题 - 网络代码根本不运行,因为URL是http:// URL而不是https:// one。
如果你有一个https://的安全网址,我建议你使用它。否则,您可以添加ATS异常,详见以下SO线程: Transport security has blocked a cleartext HTTP
答案 1 :(得分:0)
我通过更改我的URLsession函数来解决这个问题。
session.dataTask(with: url!, completionHandler: {
(data, response, error) in
if (error != nil) {
print("in session error", error.debugDescription)
} else {
do {
print("in session")
self.orderData = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! NSDictionary
OperationQueue.main.addOperation {
self.orderView.reloadData()
print("data", self.orderData)
}
} catch let error as NSError {
print(error)
}
}
}).resume()
现在我在"会话"在我的控制台中,这意味着它正确地进行了http调用,但我正面临一个新问题,即Cannot assign value of type 'NSDictionary' to type '[[String: AnyObect]]'
。但我仍然很高兴我的http电话有效,最后我可以开始对不同的问题大开眼界。
谢谢大家的帮助,我希望这个答案也可以帮助其他人。