配置
Pod依赖
swift
pod 'AliyunOSSiOS'
导入framework
根据github中的教程生成framework并引入项目
swift
# clone工程
$ git clone git@github.com:aliyun/aliyun-oss-ios-sdk.git
# 进入目录
$ cd aliyun-oss-ios-sdk
# 执行打包脚本
$ sh ./buildiOSFramework.sh
# 进入打包生成目录,AliyunOSSiOS.framework生成在该目录下
$ cd Products && ls
Xcode配置
一般只需要加入 -ObjC(注意大小写)
兼容IPv6-Only网络
添加一下3个库
swift
libresolv.tbd
CoreTelephony.framework
SystemConfiguration.framework
OSS下载
swift
import AliyunOSSiOS
// 配置访问凭证(方式二:使用STS Token)
let token = OSSFederationToken()
token.tAccessKey = "Key的ID"
token.tSecretKey = "Key的密钥"
token.tToken = "Key的token"
let provider = OSSStsTokenCredentialProvider(accessKeyId: token.tAccessKey, secretKeyId: token.tSecretKey, securityToken: token.tToken)
let client: OSSClient = OSSClient(endpoint: "OSS访问域名", credentialProvider: provider)
// 下载请求
let request = OSSGetObjectRequest()
request.bucketName = "存储空间名称"
request.objectKey = "对象名称"
// 下载路径
request.downloadToFileURL = ""
// 下载进度
request.downloadProgress = { (bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) -> Void in
print("bytesSent:\(bytesSent),totalBytesSent:\(totalBytesSent),totalBytesExpectedToSend:\(totalBytesExpectedToSend)");
}
// 下载
let task = client.getObject(request)
task.waitUntilFinished()
iPhone的文件保存路径
1、Documents Directory
用户生成的数据文件存放的地方
1、Library Directory
应用程序支持文件,缓存文件等存放位置
1、Temporary Directory
存放临时文件,系统可能会在未来清理此文件夹中的内容
swift
// 1. 获取Documents目录路径
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// 2. 获取Library目录路径
let libraryDirectory = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first!
// 3. 获取Temporary目录路径
let temporaryDirectory = FileManager.default.temporaryDirectory
OSS下载可以放在Library目录的Caches中
swift
// 获取Library目录的Cache路径
let cachesDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!
// 作为OSS下载路径时必须有文件的名称
let path = cachesDirectory.appendingPathComponent("文件名")
// 显示Caches目录的内容
func showlistCacheslist() {
let fileManager = FileManager.default
let cachesDirectoryURL = fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!
do {
let directoryContents = try fileManager.contentsOfDirectory(at: cachesDirectoryURL, includingPropertiesForKeys: nil)
for path in directoryContents {
print(path.lastPathComponent)
}
} catch {
print("Error while listing Caches directory contents: \(error.localizedDescription)")
}
}