All Products
Search
Document Center

Drive and Photo Service:Panduan SDK untuk iOS

Last Updated:Jan 17, 2026

Topik ini memberikan panduan untuk mengintegrasikan dan menggunakan Alibaba Cloud Drive and Photo Service (PDS) SDK untuk iOS.

Panduan integrasi

Integrasikan SDK

Jika Anda menggunakan CocoaPods untuk mengintegrasikan PDS SDK, tambahkan pod 'PDS_SDK' ke dalam Podfile, lalu jalankan perintah pod update untuk menyelesaikan integrasi SDK.

Inisialisasi SDK

Sebelum menginisialisasi SDK, pastikan PDS telah diaktifkan dan domain telah dibuat di Konsol PDS. Untuk informasi selengkapnya, lihat Memulai dengan PDS.

Kode contoh berikut menunjukkan cara menginisialisasi SDK:

PDSClientConfig *clientConfig = [[PDSClientConfig alloc] init];
clientConfig.userAgent = config.userAgent;//The user agent of the current device
clientConfig.dbName = config.dbName;//Optional. The name of the database
clientConfig.host = config.apiHost;//The API Host. You can obtain it from the console
[PDSClientManager setupWithAccessToken:config.accessToken clientConfig:clientConfig];//The access token is obtained by the server through accessKeyId and accessKeySecret and then sent to the client

Refresh Access Token:

Catatan

Perhatikan bahwa pada versi saat ini, SDK tidak menangani refresh Access Token. Pengguna SDK harus secara berkala meminta refresh Access Token dari server mereka dan meneruskannya ke SDK.

[[PDSClientManager defaultClient] setAccessToken:accessToken];

Unggah file

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];

Unggah file berdasarkan ID gambar

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];
Catatan

Perhatikan bahwa uploadTask yang dibuat di sini adalah variabel lokal. Anda harus menyimpan referensinya sendiri. Jika tidak, callback akan mengalami masalah setelah objek ini dilepas.

Callback untuk status dan progres tugas:

 @weakify(self);
    [uploadTask setResponseBlock:^(PDSFileMetadata *result, PDSRequestError *requestError, NSString *taskIdentifier) {
        @strongify(self);
        if (requestError) {//Upload failed
            //TODO Error handling
        } else {//Upload successful, remove temporary file
            [[PDSClientManager defaultClient].file cleanUploadTaskWithTaskIdentifier:taskIdentifier force:YES];
        }
    }                      queue:self.transferQueue];
    [uploadTask setProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
        @strongify(self);
        //TODO Progress callback
    }                      queue:self.transferQueue];
Catatan

Setelah unggahan selesai atau gagal, jika Anda tidak berniat melanjutkan operasi seperti unggah yang dapat dilanjutkan pada file ini, panggil metode cleanUploadTaskWithTaskIdentifer untuk menghapus cache yang terkait dengan tugas unggah tersebut.

Unduh file

  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];

Callback untuk progres dan status tugas:

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

        }
        if (requestError) {//Download failed

        }
    }                        queue:self.transferQueue];
    [downloadTask setProgressBlock:^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
        @strongify(self);
        //Progress callback
    }                        queue:self.transferQueue];

Operasi API pada file

Panggil antarmuka file umum melalui metode kelas di bawah [PDSClientManager defaultClient].file. Operasi file yang didukung saat ini meliputi:

  • Complete

  • Create

  • Move

  • Delete

  • Copy

  • Update

  • GetDownloadUrl

  • GetUploadUrl

  • GetAsyncTask

Untuk deskripsi parameter antarmuka yang lebih rinci, lihat Manajemen file.

Kode sumber terbuka

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

Catatan rilis

0.0.2 2022-01-24

  1. Menambahkan kemampuan untuk langsung mengunggah aset foto lokal.

  2. Menambahkan API antarmuka file.

  3. Memperbaiki unggah dan unduh yang dapat dilanjutkan.

  4. Memperbaiki masalah verifikasi CRC64 yang gagal untuk beberapa file.

  5. Memperbaiki penanganan kode kesalahan server tertentu yang tidak sesuai.

0.0.1 2021-12-01

Versi pertama, mendukung unggah/unduh file dan operasi file dasar.