swift - 如何在 Swift 中创建 QuickBlox session

标签 swift session quickblox

关闭。这个问题需要details or clarity .它目前不接受答案。












想改善这个问题吗?通过 editing this post 添加详细信息并澄清问题.

4年前关闭。




Improve this question




没有手动创建 Quickblox session 的示例。有人可以快速共享此代码吗?

Create session REST API

最佳答案

1) 只要确保添加 #import <CommonCrypto/CommonHMAC.h>到桥接的 Objective-C header 。

import Foundation

extension Data {

    func hexEncodedString() -> String {
        return map { String(format: "%02hhx", $0) }.joined()
    }
}

func digestHMac(signature:String, secret: String) -> String! {

    let secretData : Data = secret.data(using: .utf8)!
    let signatureData : Data = signature.data(using: .utf8)!

    let digest = UnsafeMutablePointer<UInt8>.allocate(capacity:Int(CC_SHA1_DIGEST_LENGTH))

    var hmacContext = CCHmacContext()
    CCHmacInit(&hmacContext, CCHmacAlgorithm(kCCHmacAlgSHA1), [UInt8](secretData), secretData.count)
    CCHmacUpdate(&hmacContext, [UInt8](signatureData), [UInt8](signatureData).count)
    CCHmacFinal(&hmacContext, digest)

    let cryptData = Data(bytes: digest, count: Int(CC_SHA1_DIGEST_LENGTH))

    return cryptData.hexEncodedString()
}
//Your 'API Application Identifier'
let applicaitonID = ""
//Your 'Authentication Key'
let authKey = ""
//Your 'Authentication Secret'
let authSecret = ""
//Your Quickblox API Endpoint
let apiEndpoint = "https://api.quickblox.com/session.json"
//Unix Timestamp It shouldn't be differ from time provided by NTP more than 60 minutes. We suggest you synchronize time on your devices with NTP service.
let timestamp = Int(Date().timeIntervalSince1970)
//Unique Random Value. Requests with the same timestamp and same value for nonce parameter can not be send twice.
let nonce = arc4random_uniform(1000000) + 1;

var payload = [

    "application_id" : applicaitonID,
    "auth_key": authKey,
    "nonce" : String(nonce),
    "timestamp" : String(timestamp)
]

//Request body is formed as the sorted (sorting alphabetically, as symbols, not as bytes) by increase the string array 'parameter=value', separated with the symbol "&".
let sortedKeys = Array(payload.keys).sorted(by: <)

var parametersString = ""
for key in sortedKeys {
    parametersString = parametersString + key + "=" + payload[key]! + "&"
}

parametersString = parametersString.substring(to: parametersString.index(before: parametersString.endIndex))
let parameters = parametersString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!

let hmac = digestHMac(signature: parameters, secret: authSecret)
payload["signature"] = hmac

//Session Request
var request:NSMutableURLRequest = NSMutableURLRequest(url: URL(string: apiEndpoint)!)

request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField:"Content-Type")
request.httpBody = try! JSONSerialization.data(withJSONObject: payload, options: [])
print(payload)

let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in

    let json = try! JSONSerialization.jsonObject(with: data!)
    /* Response
    {
        session =     {
            "_id" = 589c94c0a28f9a15d7000037;
            "application_id" = 39854;
            "created_at" = "2017-02-09T16:11:44Z";
            "device_id" = 0;
            id = 61804;
            nonce = 243190;
            token = 419e64c861594b63b558b259f8b6e4fd4c009bae;
            ts = 1486656703;
            "updated_at" = "2017-02-09T16:11:44Z";
            "user_id" = 0;
        };
    }

    */
}

task.resume()

sleep(30)

关于swift - 如何在 Swift 中创建 QuickBlox session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42141235/

相关文章:

ios - Alamofire .responseJSON 不适用于 JSON 响应

ios - 将 UIView 添加到 UICollectionViewController 的底部

php - 更改 SESSION 变量值

ios - 如何在 Quickblox iOS SDK 中检查 session 是否有效或过期?

ios - UITableView 弄乱了行内容

javascript - 简单节点/Express 应用程序无法识别 session 存储

java - 服务来自不同服务器的用户

ios - 已发送 Quickblox 无法识别的选择器

java - Quickblox 公共(public)群聊天记录

ios - 字符串比较不适用于 Swift