objective c - how to upload/download a file from/to dropbox using xcode -
i new ios. how upload/download file from/to dropbox? integrated dropbox applications in app, couldn't upload/download file.
this upload code:
- (nsstring *)getdocumentpath { nsmutabledata * pdfdata = [nsmutabledata data]; uigraphicsbeginpdfcontexttodata(pdfdata, self.view.bounds, nil); uigraphicsbeginpdfpage(); cgcontextref pdfcontext = uigraphicsgetcurrentcontext(); [self.view.layer renderincontext:pdfcontext]; uigraphicsendpdfcontext(); nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *path = [[paths objectatindex:0] stringbyappendingpathcomponent:@"vani.doc"]; [pdfdata writetofile:path atomically:yes]; return path; } - (ibaction)upload:(id)sender { nsstring *path = [self getdocumentpath]; nsstring * local = [path lastpathcomponent]; nsstring *destdir = @"/plist folder/vani.doc"; [restclient uploadfile:local topath:destdir withparentrev:nil frompath:path];
this downloading:
restclient = [[dbrestclient alloc] initwithsession:[dbsession sharedsession]]; restclient.delegate = self; nsstring *filename = [nsstring stringwithformat:@"/vani.doc"]; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring* path2 = [documentsdirectory stringbyappendingstring: [nsstring stringwithformat:@"%@", filename]]; //nslog(@"%@", path2); [restclient loadfile:filename intopath:path2];
please me. confused.
let me in case, have created wrapper class dropbox find code below used in 1 of project. it not support arc. create dropboxmanager header , implementation file may if beginner try read whole answer , follow step step. let me know in case of problem, help.
code dropboxmanager.h
#import <foundation/foundation.h> #import <dropboxsdk/dropboxsdk.h> #define kdropbox_appkey @"" // provide key here #define kdropbox_appsecret @"" // provide secret key here #define kdropbox_rootfolder kdbrootdropbox //decide level access root or app @protocol dropboxdelegate; typedef enum { dropboxtypestatusnone = 0, dropboxgetaccountinfo = 1, dropboxgetfolderlist = 2, dropboxcreatefolder = 3, dropboxuploadfile = 4 } dropboxposttype; @interface dropboxmanager : nsobject <dbrestclientdelegate,dbsessiondelegate,uialertviewdelegate> { uiviewcontroller<dropboxdelegate> *apicalldelegate; dbsession *objdbsession; nsstring *relinkuserid; dbrestclient *objrestclient; dropboxposttype currentposttype; nsstring *strfilename; nsstring *strfilepath; nsstring *strdestdirectory; nsstring *strfoldercreate; nsstring *strfoldertolist; } @property (nonatomic,retain) dbsession *objdbsession; @property (nonatomic,retain) nsstring *relinkuserid; @property (nonatomic,assign) uiviewcontroller<dropboxdelegate> *apicalldelegate; @property (nonatomic,retain) dbrestclient *objrestclient; @property (nonatomic,assign) dropboxposttype currentposttype; @property (nonatomic,retain) nsstring *strfilename; @property (nonatomic,retain) nsstring *strfilepath; @property (nonatomic,retain) nsstring *strdestdirectory; @property (nonatomic,retain) nsstring *strfoldercreate; @property (nonatomic,retain) nsstring *strfoldertolist; //singleton +(id)dropboxmanager; //initialize dropbox -(void)initdropbox; //authentication verification -(bool)handledurl:(nsurl*)url; -(void)dropboxdidlogin; -(void)dropboxdidnotlogin; //upload file -(void)uploadfile; //download file -(void)downlaodfilefromsourcepath:(nsstring*)pstrsourcepath destinationpath:(nsstring*)topath; //create folder -(void)createfolder; //get account information -(void)logintodropbox; -(void)logoutfromdropbox; -(bool)isloggedin; //list folders -(void)listfolders; @end @protocol dropboxdelegate <nsobject> @optional - (void)finishedlogin:(nsmutabledictionary*)userinfo; - (void)failedtologin:(nsstring*)withmessage; - (void)finishedcreatefolder; - (void)failedtocreatefolder:(nsstring*)withmessage; - (void)finisheduploadfile; - (void)failedtouploadfile:(nsstring*)withmessage; - (void)finisheddownloadfile; - (void)failedtodownloadfile:(nsstring*)withmessage; - (void)getfoldercontentfinished:(dbmetadata*)metadata; - (void)getfoldercontentfailed:(nsstring*)withmessage; @end
code dropboxmanager.m
#import "dropboxmanager.h" @implementation dropboxmanager @synthesize objdbsession,relinkuserid,apicalldelegate; @synthesize objrestclient; @synthesize currentposttype; @synthesize strfilename; @synthesize strfilepath; @synthesize strdestdirectory; @synthesize strfoldercreate; @synthesize strfoldertolist; static dropboxmanager *singletonmanager = nil; +(id)dropboxmanager { if(!singletonmanager) singletonmanager = [[dropboxmanager alloc] init]; return singletonmanager; } -(void)initdropbox { dbsession* session = [[dbsession alloc] initwithappkey:kdropbox_appkey appsecret:kdropbox_appsecret root:kdropbox_rootfolder]; session.delegate = self; [dbsession setsharedsession:session]; [session release]; if([[dbsession sharedsession] islinked] && objrestclient == nil) { self.objrestclient = [[dbrestclient alloc] initwithsession:[dbsession sharedsession]]; self.objrestclient.delegate = self; } } -(void)checkforlink { if(![[dbsession sharedsession] islinked]) [[dbsession sharedsession] linkfromcontroller:apicalldelegate]; } -(bool)handledurl:(nsurl*)url { bool islinked=no; if ([[dbsession sharedsession] handleopenurl:url]) { if([[dbsession sharedsession] islinked]) { islinked=yes; [self dropboxdidlogin]; } else { islinked = no; [self dropboxdidnotlogin]; } } return islinked; } #pragma mark - #pragma mark handle login -(void)dropboxdidlogin { nslog(@"logged in"); if([[dbsession sharedsession] islinked] && self.objrestclient == nil) { self.objrestclient = [[dbrestclient alloc] initwithsession:[dbsession sharedsession]]; self.objrestclient.delegate = self; } switch(currentposttype) { case dropboxtypestatusnone: break; case dropboxgetaccountinfo: [self logintodropbox]; break; case dropboxgetfolderlist: [self listfolders]; break; case dropboxcreatefolder: [self createfolder]; break; case dropboxuploadfile: [self uploadfile]; break; } //[(mainviewcontroller*)apicalldelegate setloginstatus]; } -(void)dropboxdidnotlogin { nslog(@"not logged in"); switch(currentposttype) { case dropboxtypestatusnone: break; case dropboxuploadfile: if([self.apicalldelegate respondstoselector:@selector(failedtouploadfile:)]) [self.apicalldelegate failedtouploadfile:@"problem connecting dropbox. please try again later."]; break; case dropboxgetfolderlist: break; case dropboxcreatefolder: break; case dropboxgetaccountinfo: break; } } #pragma mark - #pragma mark dbsessiondelegate methods - (void)sessiondidreceiveauthorizationfailure:(dbsession*)session userid:(nsstring *)userid { relinkuserid = [userid retain]; [[[[uialertview alloc] initwithtitle:@"dropbox session ended" message:@"do want relink?" delegate:self cancelbuttontitle:@"cancel" otherbuttontitles:@"relink", nil] autorelease] show]; } - (void)alertview:(uialertview *)alertview clickedbuttonatindex:(nsinteger)index { if (index != alertview.cancelbuttonindex) [[dbsession sharedsession] linkuserid:relinkuserid fromcontroller:apicalldelegate]; [relinkuserid release]; relinkuserid = nil; } #pragma mark - #pragma mark fileupload -(void)uploadfile { if([[dbsession sharedsession] islinked]) [self.objrestclient uploadfile:strfilename topath:strdestdirectory withparentrev:nil frompath:strfilepath]; else [self checkforlink]; } -(void)downlaodfilefromsourcepath:(nsstring*)pstrsourcepath destinationpath:(nsstring*)topath { if([[dbsession sharedsession] islinked]) [self.objrestclient loadfile:pstrsourcepath intopath:topath]; else [self checkforlink]; } - (void)restclient:(dbrestclient*)client uploadedfile:(nsstring*)destpath from:(nsstring*)srcpath metadata:(dbmetadata*)metadata { if([self.apicalldelegate respondstoselector:@selector(finisheduploadefile)]) [self.apicalldelegate finisheduploadfile]; nslog(@"file uploaded path: %@", metadata.path); } - (void)restclient:(dbrestclient*)client loadedfile:(nsstring*)destpath contenttype:(nsstring*)contenttype { if([self.apicalldelegate respondstoselector:@selector(finisheddownloadfile)]) [self.apicalldelegate finisheddownloadfile]; } -(void)restclient:(dbrestclient *)client loadfilefailedwitherror:(nserror *)error { if([self.apicalldelegate respondstoselector:@selector(failedtodownloadfile:)]) [self.apicalldelegate failedtodownloadfile:[error description]]; } - (void)restclient:(dbrestclient*)client uploadfilefailedwitherror:(nserror*)error { if([self.apicalldelegate respondstoselector:@selector(failedtouploadfile:)]) [self.apicalldelegate failedtouploadfile:[error description]]; nslog(@"file upload failed error - %@", error); } #pragma mark - #pragma mark create folder -(void)createfolder { if([[dbsession sharedsession] islinked]) [self.objrestclient createfolder:strfoldercreate]; else [self checkforlink]; } - (void)restclient:(dbrestclient*)client createdfolder:(dbmetadata*)folder { if([self.apicalldelegate respondstoselector:@selector(finishedcreatefolder)]) [self.apicalldelegate finishedcreatefolder]; nslog(@"folder created path: %@", folder.path); } - (void)restclient:(dbrestclient*)client createfolderfailedwitherror:(nserror*)error { if([self.apicalldelegate respondstoselector:@selector(failedtocreatefolder:)]) [self.apicalldelegate failedtocreatefolder:[error description]]; nslog(@"folder create failed error - %@", error); } #pragma mark - #pragma mark load account information -(void)logintodropbox { if([[dbsession sharedsession] islinked]) [self.objrestclient loadaccountinfo]; else [self checkforlink]; } - (void)restclient:(dbrestclient*)client loadedaccountinfo:(dbaccountinfo*)info { if([self.apicalldelegate respondstoselector:@selector(finishedlogin:)]) { nsmutabledictionary *userinfo = [[[nsmutabledictionary alloc] init] autorelease]; [userinfo setobject:info.displayname forkey:@"username"]; [userinfo setobject:info.userid forkey:@"userid"]; [userinfo setobject:info.referrallink forkey:@"refferellink"]; [self.apicalldelegate finishedlogin:userinfo]; } nslog(@"got information: %@", info.displayname); } - (void)restclient:(dbrestclient*)client loadaccountinfofailedwitherror:(nserror*)error { if([self.apicalldelegate respondstoselector:@selector(failedtologin:)]) [self.apicalldelegate failedtologin:[error description]]; nslog(@"failed account information error - %@", error); } #pragma mark - #pragma mark logout -(void)logoutfromdropbox { [[dbsession sharedsession] unlinkall]; [self.objrestclient release]; } #pragma mark - #pragma mark check login -(bool)isloggedin { return [[dbsession sharedsession] islinked] ? yes : no; } #pragma mark - #pragma mark load folder list -(void)listfolders { nslog(@"here-->%@",self.strfoldertolist); if([[dbsession sharedsession] islinked]) [self.objrestclient loadmetadata:self.strfoldertolist]; else [self checkforlink]; } - (void)restclient:(dbrestclient*)client loadedmetadata:(dbmetadata*)metadata { if (metadata.isdirectory) { nslog(@"folder '%@' contains:", metadata.contents); (dbmetadata *file in metadata.contents) { nslog(@"\t%@", file); } if([apicalldelegate respondstoselector:@selector(getfoldercontentfinished:)]) [apicalldelegate getfoldercontentfinished:metadata]; } nslog(@"folder list success: %@", metadata.path); } - (void)restclient:(dbrestclient*)client metadataunchangedatpath:(nsstring*)path { } - (void)restclient:(dbrestclient*)client loadmetadatafailedwitherror:(nserror*)error { nslog(@"load meta data failed error - %@", error); if([apicalldelegate respondstoselector:@selector(getfoldercontentfailed:)]) [apicalldelegate getfoldercontentfailed:[error localizeddescription]]; }
e.g. usage header file
//your view controller header file. #import <uikit/uikit.h> #import "dropboxmanager.h" @interface yourviewcontroller : uiviewcontroller <dropboxdelegate> { dropboxmanager *objmanager; } @property (nonatomic,assign) dropboxmanager *objmanager; -(ibaction)btnuploadfiletapped:(id)sender; @end
e.g. usage implementation file
#import "yourviewcontroller.h" @implementation yourviewcontroller @synthesize objmanager; // implement viewdidload additional setup after loading view, typically nib. - (void)viewdidload { [super viewdidload]; objmanager = [dropboxmanager dropboxmanager]; objmanager.apicalldelegate =self; [objmanager initdropbox]; } -(ibaction)btnuploadfiletapped:(id)sender { objmanager.currentposttype = dropboxuploadfile; objmanager.strfilename = @"yourfilename"; objmanager.strfilepath = @"yourfilepath"; objmanager.strdestdirectory = @"/"; [objmanager uploadfile]; } #pragma mark - #pragma mark file upload delegate - (void)finisheduploadfile { nslog(@"uploaded successfully."); } - (void)failedtouploadfile:(nsstring*)withmessage { nslog(@"failed upload error %@",withmessage); } @end