iOS With Dropbox

iPhone上已經有iCloud可以用來同步及備份資料了,但iCloud有空間的限制,而有在使用Dropbox的人,如果有參加一些特殊的活動或者是學生的身份,都可以多取得到一些額外的空間容量(或者是有邀請許多朋友加入也都可以額外增加空間)。雖然Dropbox官方的說明已經很清楚了,但是我還是記錄一下,免得之後要用忘記….

Step 1.建立Single View Application專案

Step 2.編輯MainStroyboard.storyboard檔案

這邊我使用三個Round Rect Button,分別用來建立檔案、檢驗檔案是否存在與上傳檔案

Step 3.建置Dropbox App

到Dropbox開發者網站,建立一個新的App。建立完成後可以看到Dropbox根據這個App會給你一個App keyApp secret,到時會需要這兩個值。

下載Dropbox的Framework,並且加入到Xcode專案;還有要加入Security.frameworkQuartzCore.framework到專案中。

Step 4.編輯AppDelegate.m

AppDelegate.m中加入下列程式碼

AppDelegate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#import <DropboxSDK/DropboxSDK.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
DBSession* dbSession =
[[DBSession alloc]
initWithAppKey:@"APP_KEY"
appSecret:@"APP_SECRET"
root:ACCESS_TYPE]; // either kDBRootAppFolder or kDBRootDropbox
[DBSession setSharedSession:dbSession];
return YES;
}

- (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;
}

**Step 5.編輯專案名稱-Info.plist檔案

使用open source開啟專案名稱-Info.plist,找到第一個在下方加入這段:

專案名稱-Info.plist
1
2
3
4
5
6
7
8
9
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>db-APP_KEY</string>
</array>
</dict>
</array>

**Step 6.編輯ViewController

首先在ViewController.h程式碼如下:

ViewController.h
1
2
3
4
5
6
7
8
9
10
11
12
#import <UIKit/UIKit.h>
#import <DropboxSDK/DropboxSDK.h>

@interface ViewController : UIViewController<DBRestClientDelegate>
{
DBRestClient *restClient;
}
- (IBAction)loginDropbox:(id)sender;
- (IBAction)upload:(id)sender;
- (IBAction)creatFile:(id)sender;

@end

接著在ViewController.m撰寫三個方法的實作內容,並且撰寫一個restClient的getter方法。

ViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
- (DBRestClient *)restClient {
if (!restClient) {
restClient =
[[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
restClient.delegate = self;
}
return restClient;
}

- (IBAction)loginDropbox:(id)sender {

if (![[DBSession sharedSession] isLinked]) {
[[DBSession sharedSession] linkFromController:self];
} else {
NSLog(@"linked");
}
}

- (IBAction)upload:(id)sender {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"file1.txt"];;
NSString *pathString = @"/";
[self.restClient uploadFile:@"file1.txt" toPath:pathString fromPath:path];

}

- (IBAction)creatFile:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"file1.txt"];;

// String to write
NSString *str = @"iPhone Developer Tips\nhttp://iPhoneDevelopTips,com";
NSError *error;
// Write the file
[str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];

NSFileManager *filemgr;

filemgr = [NSFileManager defaultManager];

if ([filemgr fileExistsAtPath: path ] == YES)
NSLog (@"File exists");
else
NSLog (@"File not found");

}

Step 7.編譯專案

點選link dropbox按鈕,跟Dropbox做連接;在點選create & check按鈕建立檔案與確認檔案是否存在;最後才點選upload按鈕,將建立的檔案上傳到Dropbox。