iOS Swift 快速开始
- AI 快速开始
- 完整开发指引
准备工作
在开始之前,请确保您已完成以下准备:
- 开通云开发环境:开通云开发环境
- 获取 API 访问凭证:在 CloudBase 控制台 获取环境 ID 和 API 密钥
Client
配置你的 AI 工具以连接 CloudBase 能力 支持本地与托管两种连接方式,详见 连接方式。
步骤 1:安装 / 配置 CloudBase
步骤 2:和 AI 对话
在 AI 对话中依次输入以下内容:
prompt
安装 CloudBase Skills:命令 npx skills add tencentcloudbase/cloudbase-skills -y使用 CloudBase Skills: 使用 CloudBase Skills:在 iOS Swift 应用中集成 CloudBase HTTP API准备工作
在开始之前,请确保您已完成以下准备:
- 开通云开发环境:开通云开发环境
- 获取 API 访问凭证:在 CloudBase 控制台 获取环境 ID 和 API 密钥
更多详情请参考:iOS Swift 完整文档
身份认证
- 短信验证码注册
- 邮箱验证码注册
- 账号密码登录
- 短信验证码登录
- 邮箱验证码登录
func signUpWithPhoneCode(cloudbase: CloudBaseClient, phoneNumber: String, verificationCode: String, username: String? = nil, password: String? = nil, captchaToken: String? = nil, completion: @escaping ([String: Any]?) -> Void) {
// 步骤1: 发送短信验证码
var body: [String: Any] = [
"phone_number": phoneNumber.hasPrefix("+86") ? phoneNumber : "+86\(phoneNumber)",
"target": "NON_USER" // "NON_USER" - 账号不存在才发送; "ANY" - 不限制
]
var headers: [String: String] = [:]
if let token = captchaToken {
headers["x-captcha-token"] = token
}
cloudbase.request(
method: "POST",
path: "/auth/v1/verification",
body: body,
customHeaders: headers
) { (sendResult: [String: Any]?) in
guard let sendResult = sendResult,
let verificationId = sendResult["verification_id"] as? String else {
print("发送验证码失败")
completion(nil)
return
}
print("验证码发送成功! ID: \(verificationId)")
// 步骤2: 验证验证码
cloudbase.request(
method: "POST",
path: "/auth/v1/verification/verify",
body: [
"verification_id": verificationId,
"verification_code": verificationCode
]
) { (verifyResult: [String: Any]?) in
guard let verifyResult = verifyResult,
let verificationToken = verifyResult["verification_token"] as? String else {
print("验证码错误")
completion(nil)
return
}
print("验证成功!")
// 步骤3: 使用验证令牌注册
var signUpBody: [String: Any] = [
"phone_number": phoneNumber.hasPrefix("+86") ? phoneNumber : "+86\(phoneNumber)",
"verification_token": verificationToken
]
// 可选:添加用户名和密码
if let username = username {
signUpBody["username"] = username
}
if let password = password {
signUpBody["password"] = password
}
cloudbase.request(
method: "POST",
path: "/auth/v1/signup",
body: signUpBody
) { (signUpResult: [String: Any]?) in
if let signUpResult = signUpResult,
let accessToken = signUpResult["access_token"] as? String,
let userId = signUpResult["sub"] as? String {
print("注册成功! 用户ID: \(userId)")
print("访问令牌: \(String(accessToken.prefix(20)))...")
// 更新访问令牌
cloudbase.updateAccessToken(accessToken)
completion(signUpResult)
} else {
print("注册失败")
completion(nil)
}
}
}
}
}
// 使用示例
// signUpWithPhoneCode(
// cloudbase: cloudbase,
// phoneNumber: "13800138000",
// verificationCode: "123456",
// username: "myusername",
// password: "mypassword"
// ) { result in
// if result != nil {
// print("手机号注册成功")
// }
// }
func signUpWithEmailCode(cloudbase: CloudBaseClient, email: String, verificationCode: String, username: String? = nil, password: String? = nil, captchaToken: String? = nil, completion: @escaping ([String: Any]?) -> Void) {
// 步骤1: 发送邮箱验证码
var body: [String: Any] = [
"email": email,
"target": "NON_USER" // "NON_USER" - 账号不存在才发送; "ANY" - 不限制
]
var headers: [String: String] = [:]
if let token = captchaToken {
headers["x-captcha-token"] = token
}
cloudbase.request(
method: "POST",
path: "/auth/v1/verification",
body: body,
customHeaders: headers
) { (sendResult: [String: Any]?) in
guard let sendResult = sendResult,
let verificationId = sendResult["verification_id"] as? String else {
print("发送验证码失败")
completion(nil)
return
}
print("验证码发送成功! ID: \(verificationId)")
// 步骤2: 验证验证码
cloudbase.request(
method: "POST",
path: "/auth/v1/verification/verify",
body: [
"verification_id": verificationId,
"verification_code": verificationCode
]
) { (verifyResult: [String: Any]?) in
guard let verifyResult = verifyResult,
let verificationToken = verifyResult["verification_token"] as? String else {
print("验证码错误")
completion(nil)
return
}
print("验证成功!")
// 步骤3: 使用验证令牌注册
var signUpBody: [String: Any] = [
"email": email,
"verification_token": verificationToken
]
// 可选:添加用户名和密码
if let username = username {
signUpBody["username"] = username
}
if let password = password {
signUpBody["password"] = password
}
cloudbase.request(
method: "POST",
path: "/auth/v1/signup",
body: signUpBody
) { (signUpResult: [String: Any]?) in
if let signUpResult = signUpResult,
let accessToken = signUpResult["access_token"] as? String,
let userId = signUpResult["sub"] as? String {
print("注册成功! 用户ID: \(userId)")
print("访问令牌: \(String(accessToken.prefix(20)))...")
// 更新访问令牌
cloudbase.updateAccessToken(accessToken)
completion(signUpResult)
} else {
print("注册失败")
completion(nil)
}
}
}
}
}
// 使用示例
// signUpWithEmailCode(
// cloudbase: cloudbase,
// email: "user@example.com",
// verificationCode: "123456",
// username: "myusername",
// password: "mypassword"
// ) { result in
// if result != nil {
// print("邮箱注册成功")
// }
// }
func signIn(cloudbase: CloudBaseClient, username: String, password: String, completion: @escaping ([String: Any]?) -> Void) {
// 账号密码登录
cloudbase.request(
method: "POST",
path: "/auth/v1/signin",
body: ["username": username, "password": password]
) { (result: [String: Any]?) in
if let result = result,
let accessToken = result["access_token"] as? String,
let userId = result["sub"] as? String {
print("登录成功! 用户ID: \(userId)")
print("访问令牌: \(String(accessToken.prefix(20)))...")
// 更新访问令牌
cloudbase.updateAccessToken(accessToken)
}
completion(result)
}
}
// 使用示例(async/await)
// Task {
// let result: [String: Any]? = await cloudbase.request(
// method: "POST",
// path: "/auth/v1/signin",
// body: ["username": "your_username", "password": "your_password"]
// )
// if let result = result,
// let accessToken = result["access_token"] as? String {
// // 更新访问令牌
// cloudbase.updateAccessToken(accessToken)
// }
// print(result)
// }
func loginWithPhoneCode(cloudbase: CloudBaseClient, phoneNumber: String, verificationCode: String, captchaToken: String? = nil, completion: @escaping (Bool) -> Void) {
// 步骤1: 发送短信验证码
var body: [String: Any] = [
"phone_number": phoneNumber.hasPrefix("+86") ? phoneNumber : "+86\(phoneNumber)",
"target": "ANY" // "ANY" - 不限制,无论用户是否存在都发送; "USER" - 账号必须存在才发送
]
var headers: [String: String] = [:]
if let token = captchaToken {
headers["x-captcha-token"] = token
}
cloudbase.request(
method: "POST",
path: "/auth/v1/verification",
body: body,
customHeaders: headers
) { (sendResult: [String: Any]?) in
guard let sendResult = sendResult,
let verificationId = sendResult["verification_id"] as? String else {
print("发送验证码失败")
completion(false)
return
}
print("验证码发送成功! ID: \(verificationId)")
// 步骤2: 验证验证码
cloudbase.request(
method: "POST",
path: "/auth/v1/verification/verify",
body: [
"verification_id": verificationId,
"verification_code": verificationCode
]
) { (verifyResult: [String: Any]?) in
guard let verifyResult = verifyResult,
let verificationToken = verifyResult["verification_token"] as? String else {
print("验证码错误")
completion(false)
return
}
print("验证成功!")
// 步骤3: 使用验证令牌登录
cloudbase.request(
method: "POST",
path: "/auth/v1/signin",
body: [
"phone_number": phoneNumber.hasPrefix("+86") ? phoneNumber : "+86\(phoneNumber)",
"verification_token": verificationToken
]
) { (loginResult: [String: Any]?) in
if let loginResult = loginResult,
let accessToken = loginResult["access_token"] as? String {
print("登录成功!")
cloudbase.updateAccessToken(accessToken)
completion(true)
} else {
print("