tru-sdk-ios
SDK 的唯一目的是在调用公共 URL 之前强制数据蜂窝连接,并将返回如下 JSON 响应
- 成功 数据连接已建立并且已从 URL 端点接收到响应时
{
"http_status": string, // HTTP status related to the url
"response_body" : { // optional depending on the HTTP status
... // the response body of the opened url
... // see API doc for /device_ip and /redirect
},
"debug" : {
"device_info": string,
"url_trace" : string
}
}
- 错误 当数据连接不可用且/或发生 SDK 内部错误时
{
"error" : string,
"error_description": string,
"debug" : {
"device_info": string,
"url_trace" : string
}
}
可能的错误代码:sdk_no_data_connectivity
,sdk_connection_error
,sdk_redirect_error
,sdk_error
。
安装
使用包依赖项
import PackageDescription
let package = Package(
dependencies: [
.Package(url: "https://github.com/tru-ID/tru-sdk-ios.git, majorVersion: 0, minor: 0)
]
)
使用 cocoapods
pod 'tru-sdk-ios', '~> x.y.z'
兼容性
Minimum iOS: TruSDK is compatible with iOS 12+
大小
tru-sdk-ios: ~115KiB
使用示例
- 设备符合 tru.ID 静默认证的条件吗?
import TruSDK
let sdk: TruSDK = TruSDK()
// retreive access token with coverage scope from back-end
let token = ...
// open the device_ip public API endpoint
sdk.openWithDataCellularAndAccessToken(url: URL(string: "https://{data_residency}.api.tru.id/coverage/v0.1/device_ip")!, accessToken: token, debug: false) { (resp) in
if (resp["error_code"]) != nil {
NSLog("\(resp["error_description"])")
} else {
let status = resp["http_status"] as! Int
if (status == 200) {
let body = resp["response_body"] as! [String : Any]
NSLog("\n==>device is reachable on \(body["network_name"] as! String)")
} else if (status == 400) {
NSLog("\n==>MNO not supported")
} else if (status == 412) {
NSLog("\n==>Not mobile IP")
} else {
NSLog("\n==>Other error")
}
}
}
- 如何打开由 PhoneCheck API 或 SubscriberCheck API 返回的检查 URL
import TruSDK
let sdk: TruSDK = TruSDK()
sdk.openWithDataCellular(url: URL(string: checkUrl) , debug: true) { (resp) in
if (resp["error"]) != nil {
// error
} else {
let status = resp["http_status"] as! Int
if (status == 200) {
let body = resp["response_body"] as! [String : Any]
if let checkId = body["check_id"], let code = body["code"] {
var ref: String = ""
if let _ref = body["reference_id"] { ref = _ref as! String}
// send code, checkId and ref to back-end
// to trigger a PATCH /checks/{check_id}
} else {
if let error = body["error"], let desc = body["error_description"] {
// error
} else {
// invalid response format
}
}
} else if (status == 400) {
// MNO not supported
} else if (status == 412) {
// MNO a mobile IP
} else {
// error
}
}
元信息
在 MIT 协议下分发。更多信息请参阅 LICENSE
文件。