全部產品
Search
文件中心

Drive and Photo Service:iOS SDK操作指南

更新時間:Dec 22, 2025

本文為阿里雲PDS(Drive and Photo Service)iOS SDK的接入與使用指南,可以協助開發人員快速整合網盤與相簿服務功能。

接入指南

接入SDK

通過CocoaPods工具引入PDS SDK,只需在Podfile中添加pod 'PDS_SDK',然後執行命令pod update,即可完成SDK的接入。

初始化SDK

使用SDK前提是您已經開通PDS服務,並且在控制台建立了域執行個體。參見開始使用 PDS

SDK初始化代碼

PDSClientConfig *clientConfig = [[PDSClientConfig alloc] init];
clientConfig.userAgent = config.userAgent;//當前裝置的UA
clientConfig.dbName = config.dbName;//資料庫名稱,可選
clientConfig.host = config.apiHost;//API Host,需要從控制台中擷取
[PDSClientManager setupWithAccessToken:config.accessToken clientConfig:clientConfig];//accessToken是服務端通過accessKeyId和accessKeySecret擷取以後下發給用戶端的

Access Token重新整理。

說明

需要注意的是,在目前版本中SDK不負責重新整理Access Token。需要SDK適用方從自己的服務端那邊定時請求重新整理Access Token並傳給SDK。

[[PDSClientManager defaultClient] setAccessToken:accessToken];

上傳普通檔案

PDSDownloadUrlRequest *request = [[PDSDownloadUrlRequest alloc] initWithDownloadUrl:taskInfo.downloadUrl
                                                                            destination:filePath
                                                                               fileSize:[taskInfo.fileSize unsignedLongLongValue]
                                                                                 fileID:taskInfo.fileId
                                                                              hashValue:taskInfo.hashValue
                                                                               hashType:PDSFileHashTypeCrc64
                                                                                driveID:taskInfo.driveId
                                                                                shareID:taskInfo.shareId];
downloadTask = [[PDSClientManager defaultClient].file downloadUrl:request taskIdentifier:taskInfo.taskId];

直接通過照片ID上傳檔案

PDSUploadPhotoRequest *request = [[PDSUploadPhotoRequest alloc] initWithLocalIdentifier:localIdentifier
                                                                                   parentFileID:taskInfo.parentId
                                                                                        driveID:taskInfo.driveId
                                                                                        shareID:taskInfo.shareId
                                                                                       fileName:taskInfo.fileName];
uploadTask = [[PDSClientManager defaultClient].file uploadPhotoAsset:request taskIdentifier:taskInfo.taskId];
說明

注意,這裡建立的uploadTask是個局部變數,使用方需要自己持有對它的引用,不然這個對象被釋放以後回調會出問題。

完成和進度回調

 @weakify(self);
    [uploadTask setResponseBlock:^(PDSFileMetadata *result, PDSRequestError *requestError, NSString *taskIdentifier) {
        @strongify(self);
        if (requestError) {//上傳失敗
            //TODO 錯誤處理
        } else {//上傳成功,移除臨時檔案
            [[PDSClientManager defaultClient].file cleanUploadTaskWithTaskIdentifier:taskIdentifier force:YES];
        }
    }                      queue:self.transferQueue];
    [uploadTask setProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
        @strongify(self);
        //TODO 進度回調
    }                      queue:self.transferQueue];
說明

注意,上傳完成或者失敗以後,如果確認之後不會再繼續對這個檔案進行斷點續傳等操作,需要調用cleanUploadTaskWithTaskIdentifer方法刪除這個上傳任務的相關緩衝。

下載檔案

  PDSDownloadTask *downloadTask = nil;
    PDSDownloadUrlRequest *request = [[PDSDownloadUrlRequest alloc] initWithDownloadUrl:taskInfo.downloadUrl
                                                                            destination:filePath
                                                                               fileSize:[taskInfo.fileSize unsignedLongLongValue]
                                                                                 fileID:taskInfo.fileId
                                                                              hashValue:taskInfo.hashValue
                                                                               hashType:PDSFileHashTypeCrc64
                                                                                driveID:taskInfo.driveId
                                                                                shareID:taskInfo.shareId];

    downloadTask = [[PDSClientManager defaultClient].file downloadUrl:request taskIdentifier:taskInfo.taskId];

進度和完成回調

@weakify(self);
    [downloadTask setResponseBlock:^(PDSFileMetadata *_Nullable result, PDSRequestError *_Nullable requestError, NSString *_Nonnull taskIdentifier) {
        @strongify(self);
        if (result) {//下載成功

        }
        if (requestError) {//下載失敗

        }
    }                        queue:self.transferQueue];
    [downloadTask setProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
        @strongify(self);
        //進度回調
    }                        queue:self.transferQueue];

檔案操作API

常用的檔案介面可以通過[PDSClientManager defaultClient].file下面的類方法進行調用。當前支援的檔案操作如下:

  • Complete

  • Create

  • Move

  • Delete

  • Copy

  • Update

  • GetDownloadUrl

  • GetUploadUrl

  • GetAsyncTask

具體介面參數的說明,請參見檔案管理

開源地址

https://github.com/aliyun/aliyun-pds-objc-sdk

更新日誌

0.0.2 2022-01-24

  1. 增加直接上傳本地照片asset的功能

  2. 增加檔案介面API

  3. 修複上傳下載斷點續傳

  4. 修複部分檔案CRC64校正失敗的問題

  5. 修複部分服務端錯誤碼沒有正確處理的問題

0.0.1 2021-12-01

第一版,支援檔案上傳下載/基本檔案操作