I am really new to Swift and I'm trying to make a http request to a URL with consumer key and consumer secret and honestly I'm not sure that is even possible to do in Swift.
I've been trying to make this work with authentication method but it only gives me an error.
My code (Hope this helps you to understand what I'm trying to do..)
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()
If this http call with consumer key and consumer secret is totally unacceptable in Swift or if there is any other way that I can get by this, please let me know.
Thank you in advanced.
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
}
}