官方教學網址:

https://www.dropbox.com/developers/reference/sdk

 

 

Step 1.

 

先到上面的網頁下載dropbox sdk for ios

 

並且在上面的網頁登記app

 

登記之後,可以在 My Apps的地方,查詢App Key 還有 App Secret

 

App名稱不能重覆,請注意

 

每個App可以設定,所能存取的目錄,有兩種,一種是所有Dropbox的目錄

 

一種是App專門的目錄,請注意App專門的目錄不能共用,無法兩個App共用同一個目錄

 

這個設定也是設好了就無法修改,請注意

 

 

Step 2.

 

建立自己的project, 加入Dropboxsdk

 

將下載的sdk解開之後會有一個Dropbox SDKfolder

還有一個DropboxSDK.framework

 

請將這兩個東西都拉到project

 

Copy items into destination group's folder 的選項要記得點選

 

然後新增Security.framework 

新增方法:先選擇Target,然後選擇Build Phase->Link Binary with Libraries

按下面的+,然後從選單裡面加進去

 

 

Step 3.

 

Dropbox的初始化

 

#import <DropboxSDK/DropboxSDK.h>

 

...

 

DBSession* dbSession =

   [[[DBSession alloc]

     initWithAppKey:@"APP_KEY"

     appSecret:@"APP_SECRET"

     root:ACCESS_TYPE]// either kDBRootAppFolder or kDBRootDropbox

     autorelease];

[DBSession setSharedSession:dbSession];

 

上面App KeyApp Secret 的部份要填入在Dropbox 申請的資料

 

 

Step 4.

 

Dropbox連結

 

if(![[DBSession sharedSession] isLinked]){

       [[DBSession sharedSession] link];

   }

 

呼叫了就連結了,超簡單

 

 

Step 5.

 

請到appplist的地方

 

新增key

 

<key>CFBundleURLTypes</key>

<array>

   <dict>

       <key>CFBundleURLSchemes</key>

       <array>

           <string>db-APP_KEY</string>

       </array>

   </dict>

</array>

 

 

上面APP_KEY的地方,請輸入所申請的App key

 

 

URLSchemes的功能是讓這個App可以被其他的App所開啟

 

任何App,只要在上面這個地方,加入名稱

 

其他的app就可以用openURL 的方式,打入這個名稱來開啟App

 

 

Step 6.

 

 

請在UIApplication delegate的地方,加入

 

-    (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

 

 

4.2之後,請改用

 

-    (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

 

 

官方的範例

 

-(BOOL)application:(UIApplication*)application handleOpenURL:(NSURL *)url {

   if([[DBSession sharedSession] handleOpenURL:url]){

       if([[DBSession sharedSession] isLinked]){

           NSLog(@"App linked successfully!");

           // At this point you can start making API calls

       }

       return YES;

   }

   // Add whatever other url handling code your app requires here

   return NO;

}

 

可以用 [[DBSession sharedSession] handleOpenURL:url]來判斷是否成功

 

如果你是用別的程式來開啟app的話,就會呼叫到這個function

 

 

 

Step 7.

其他操作檔案的部份

 

DBRestClient*restClient;

 

#import <DropboxSDK/DropboxSDK.h>

 

...

 

-(DBRestClient*)restClient {

   if(!restClient){

     restClient =

         [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];

     restClient.delegate=self;

   }

   return restClient;

}

 

使用這個物件來操作

 

ex:

 

 

上傳檔案

 

NSString*localPath =[[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];

NSString*filename =@"Info.plist";

NSString*destDir =@"/";

[[self restClient] uploadFile:filename toPath:destDir

                   withParentRev:nil fromPath:localPath];

 

 

以下是delegatecallback

 

-(void)restClient:(DBRestClient*)client uploadedFile:(NSString*)destPath

   from:(NSString*)srcPath metadata:(DBMetadata*)metadata {

 

   NSLog(@"File uploaded successfully to path: %@", metadata.path);

}

 

-(void)restClient:(DBRestClient*)client uploadFileFailedWithError:(NSError*)error {

   NSLog(@"File upload failed with error - %@", error);

}

 

 

顯示目錄下的檔案

 

[[self restClient] loadMetadata:@"/"];

 

 

-(void)restClient:(DBRestClient*)client loadedMetadata:(DBMetadata*)metadata {

   if(metadata.isDirectory){

       NSLog(@"Folder '%@' contains:", metadata.path);

   for(DBMetadata*file in metadata.contents){

      NSLog(@"\t%@", file.filename);

   }

   }

}

 

-(void)restClient:(DBRestClient*)client

   loadMetadataFailedWithError:(NSError*)error {

 

   NSLog(@"Error loading metadata: %@", error);

}

 

 

 

下載檔案

 

[[self restClient] loadFile:dropboxPath intoPath:localPath]

 

 

 

delegatecallback

 

 

-(void)restClient:(DBRestClient*)client loadedFile:(NSString*)localPath {

   NSLog(@"File loaded into path: %@", localPath);

}

 

-(void)restClient:(DBRestClient*)client loadFileFailedWithError:(NSError*)error {

   NSLog(@"There was an error loading the file - %@", error);

}

 

 

 

 

dropbox 的指令都是會背景執行的

 

因為還是得透過網路去傳輸

 

他還有其他的指令

 

沒有列在教學文件裡面

 

例如有抓取縮圖,建立檔案夾之類的

 

delegate都會有對應的callback

 

如果要查詢的話,可以去找

 

DBRestClient的定義,會有列出來

 

DBMetadatad 是Dropbox的檔案描述的格式,也有很多檔案的資訊可以查詢

 

以上~謝謝大家

arrow
arrow
    全站熱搜

    jrdevil 發表在 痞客邦 留言(1) 人氣()