如何在 Swift 中创建 QuickBlox 会话 [关闭]
Posted
技术标签:
【中文标题】如何在 Swift 中创建 QuickBlox 会话 [关闭]【英文标题】:How to create QuickBlox session in Swift [closed] 【发布时间】:2017-02-09 15:56:00 【问题描述】:没有手动创建 Quickblox 会话的示例。有人可以快速分享此代码吗?
Create session REST API
【问题讨论】:
【参考方案1】:1) 只需确保将 #import <CommonCrypto/CommonHMAC.h>
添加到桥接 Objective-C 标头即可。
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 中创建 QuickBlox 会话 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 WEB SDK 在 QuickBlox 中创建房间