个人来说玩玩,还是用swiftui比较好,毕竟写界面的速度快很多,而且还比较直观。
有部分逻辑需要接入图片上传,因此接入了一下腾讯云对象存储cos。
官网上只有swift的教程,没有swiftui的。记录一下swiftui的接入过程。
首先引入pod集成项目。
pod 'QCloudCOSXML'
# 据说是精简版,不过看起来好像也没有啥效果,头文件的引入好像根据文档来还是会报错的。
# 本例子只是简单的上传,因此用哪个都行。
# pod 'QCloudCOSXML/Transfer'
引入头文件
import QCloudCOSXML
// 精简版用下边的这个好像会报错,找不到这个,用上边的没问题
// import QCloudCOSXMLTransfer
初始化
一般来说swiftui的app部分是这样的。不过按照教程直接继承协议是不行的。
@main
struct DemoApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
首先新建一个 appdelegate
class AppDelegate: NSObject, UIApplicationDelegate, QCloudSignatureProvider {
}
然后在里边实现初始化及签名的回调。这两部分感觉不用写在appdelegate的finish launch里边,因为现在的应用一般还要来个隐私协议。暴露出一个方法来给swiftui调用,控制流程比较方便。最好粘贴代码,不要手敲,例如 QCloudAuthentationV5Creator 这个有个虚方法QCloudAuthentationCreator,稍不注意就失败了。
另外本例子用的是客户端直接同cos通讯方式,如果出现泄漏直接换secret也是可行的。
class AppDelegate: NSObject, UIApplicationDelegate, QCloudSignatureProvider {
func signature(with fileds: QCloudSignatureFields!, request: QCloudBizHTTPRequest!, urlRequest urlRequst: NSMutableURLRequest!, compelete continueBlock: QCloudHTTPAuthentationContinueBlock!) {
let credential = QCloudCredential.init()
credential.secretID = "xxx" // 密钥ID
credential.secretKey = "xxx" // 密钥secret
let creator = QCloudAuthentationV5Creator.init(credential: credential)
let signature = creator?.signature(forData: urlRequst)
continueBlock(signature, nil)
}
func initQCloud() {
let config = QCloudServiceConfiguration.init()
let endpoint = QCloudCOSXMLEndPoint.init()
endpoint.regionName = "ap-beijing" // bucket region
endpoint.useHTTPS = true
config.endpoint = endpoint
config.signatureProvider = self
QCloudCOSXMLService.registerDefaultCOSXML(with: config)
QCloudCOSTransferMangerService.registerDefaultCOSTransferManger(
with: config)
}
}
把appdelegate加入到swiftui的app中。
struct DemoApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
ContentView()
.onAppear {
delegate.initQCloud()
}
}
}
}
初始化到此完毕。
初始化完整代码:
import SwiftUI
import QCloudCOSXML
class AppDelegate: NSObject, UIApplicationDelegate, QCloudSignatureProvider {
func signature(with fileds: QCloudSignatureFields!, request: QCloudBizHTTPRequest!, urlRequest urlRequst: NSMutableURLRequest!, compelete continueBlock: QCloudHTTPAuthentationContinueBlock!) {
let credential = QCloudCredential.init()
credential.secretID = "xxx" // 密钥ID
credential.secretKey = "xxx" // 密钥secret
let creator = QCloudAuthentationV5Creator.init(credential: credential)
let signature = creator?.signature(forData: urlRequst)
continueBlock(signature, nil)
}
func initQCloud() {
let config = QCloudServiceConfiguration.init()
let endpoint = QCloudCOSXMLEndPoint.init()
endpoint.regionName = "ap-beijing" // bucket region
endpoint.useHTTPS = true
config.endpoint = endpoint
config.signatureProvider = self
QCloudCOSXMLService.registerDefaultCOSXML(with: config)
QCloudCOSTransferMangerService.registerDefaultCOSTransferManger(
with: config)
}
}
struct DemoApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
ContentView()
.onAppear {
delegate.initQCloud()
}
}
}
}
上传部分
简单上传部分文章来源:https://www.toymoban.com/news/detail-541367.html
import QCloudCOSXML
func updatePic(img: UIImage) {
let picPath = "temp/\(Day(date: date).format(format: "yyyy-MM-dd"))/\(UUID().uuidString).png"
let put = QCloudCOSXMLUploadObjectRequest<AnyObject>()
put.bucket = "xxx" // bucket id
put.object = picPath // 上传路径
put.body = img.pngData() as AnyObject
put.setFinish { res, err in
print("upload obj finish \(res) \(err)")
}
QCloudCOSTransferMangerService.defaultCOSTransferManager().uploadObject(put)
}
到此cos接入完毕。文章来源地址https://www.toymoban.com/news/detail-541367.html
到了这里,关于swiftui接入腾讯云对象存储cos的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!