简体   繁体   中英

How to Upload images on DropBox from iOS Application

I am working with an IOS application in which i select photos from my IPhone simulator and i want to upload those pics on DropBox but i am unable to do that. I am not getting the correct parameters in my uploading method.

Here is my code for sending selected pics from my controller. This code will send the images which i selected from my simulator to the the controller in which i write the code for uploading pics to the DropBox:

// UIImage *image;
// NSMutableArray *images
// @property (nonatomic, copy) NSArray *chosenImages;

for (NSDictionary *dict in info)
{
    image = [dict objectForKey:UIImagePickerControllerOriginalImage];

    [images addObject:image];

    imageview = [[UIImageView alloc] initWithImage:image];

    [imageview setContentMode:UIViewContentModeScaleAspectFit];

    imageview.frame = workingFrame;

    self.chosenImages = images;

    RootViewController *rvc=[[RootViewController alloc]init];

    [rvc _startUpload:image];

    [self.navigationController pushViewController:rvc animated:YES];
}

Now in my RootViewController i am writing codes for uploading images in this method: _startUpload:image method is used for uploading pics

-(void)_startUpload:image
{
    NSString *fileName = @"myImage.png";

    NSString *tempDir = NSTemporaryDirectory();

    NSString *imagePath = [tempDir stringByAppendingPathComponent:fileName];

    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(self.loadQRCodeImage.image)];

    [imageData writeToFile:imagePath atomically:YES];

    [self.restClient uploadFile:fileName toPath:dropBoxFolder fromPath:imagePath];
}

I am not getting my pics in dropbox so please help me that what am i doing wrong and which paths i really used for uploading images. Please help me.

Thanks in advance.

There is a iOS SDK, which provide APIs to interact with drop box through your iOS application Following is the tutorial provided on drop box developer web site.

https://www.dropbox.com/developers/core/start/ios

Following is the operation you need to look at.

https://www.dropbox.com/developers/core/docs#files_put

IN .h file :-

@interface UploadOptionViewController : UIViewController<UIImagePickerControllerDelegate,DBRestClientDelegate,UINavigationControllerDelegate>
{
 DBRestClient *restClient;
 NSString *file;
}
-(IBAction)photoLibrary:(id)sender;
@property (nonatomic,retain) NSString *file;
@property(retain,nonatomic) DBRestClient *restClient;
@end

In .m file :-

- (DBRestClient *)restClient {
if (!restClient) {
    restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
    restClient.delegate = self;
}
return restClient;
}

 //-------------Upload file from dropbox --------------------------------------------

 - (void)restClient:(DBRestClient*)client uploadedFile:(NSString*)destPath
          from:(NSString*)srcPath metadata:(DBMetadata*)metadata {
[presenter hideHud];
NSLog(@"File uploaded successfully to path: %@", metadata.path);
[[[[UIAlertView alloc]
   initWithTitle:@"File Uplaod" message:@"File uploaded successfully"
   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]
  autorelease]
 show];
 }

- (void)restClient:(DBRestClient*)client uploadFileFailedWithError:(NSError*)error {
[presenter hideHud];
NSLog(@"File upload failed with error - %@", error);
[[[[UIAlertView alloc]
   initWithTitle:@"File Upload" message:@"File upload failed"
   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]
  autorelease]
 show];
 }

And called below where appropriate

NSString *localPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
NSString *filename = @"Info.plist";
NSString *destDir = @"/";
[[self restClient] uploadFile:filename toPath:destDir
               withParentRev:nil fromPath:localPath];

Here filename is name of file, localpath is where file is sitiuated. Hope this helps you !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM