cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How do I upload a file to my dropbox apps folder in dropbox in objective c?

How do I upload a file to my dropbox apps folder in dropbox in objective c?

Chez B.
New member | Level 1

Currently nothing happens when I run my code.

This is how I am setting the up client. I did not put the client set up in my viewDidLoad method because this class will never be shown it just handles server side actions.

(instancetype)initWithClient  
{

  self = [super init];

  if (self) {

    self.restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];

    self.restClient.delegate = self;

      }

  return self;  
}  



This is how I make the API call to upload

(void)uploadToDropBox:(UIImage *)img  
{

  NSLog(@"Entered the DB upload method");


  NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  
                                                             NSUserDomainMask,  
                                                             YES);


  NSString *documentDirectory = [directories firstObject];

  NSString *imagePath = [documentDirectory stringByAppendingPathComponent:@"cardImage"];

  NSString *destDir = @"Apps/BirthdayScrapbook";

  NSLog(@"Just about to execute the upload method");

  NSData *imageData = UIImagePNGRepresentation(img);

  [imageData writeToFile:imagePath atomically: YES];

  [self.restClient uploadFile:@"cardImage" toPath:destDir withParentRev:nil fromPath:imagePath];

  NSLog(@"Executed the DB  upload method");

  [self sendBDayCard];  
}  



I think I am going about it wrong. I know what my access token is, but I don't know what to do with it nor where to put it. There is a client init method that takes in the userID, but I don't know where to find my accounts userID. Can someone give some direction with this?

17 Replies 17

Greg-DB
Dropbox Staff

The nonce should just be any random string, which should only be used once. You can get the current timestamp however you wish.

 

Also, for what it's worth, if it saves you some trouble, since you just need the one account, I believe you can supply any arbitrary string for userID in the updateAccessToken method. (I can't promise that will always work though.)

 

Finally, I recommend getting your account linked properly first, but there are a few things that might cause your delegate methods to not be called:

  1. Your rest client is nil or is being released (e.g., by ARC) prematurely.
  2. You're making the call in a background thread that doesn't have a run loop.
  3. Your delegate method that should be called back has a typo in it. Unfortunately the SDK doesn't warn you if it can't find a delegate method to call; it just completes without telling anyone.

Be sure to check your console for any errors though.

Chez B.
New member | Level 1

How do I go about making sure that my account is linked properly? I am using the Facebook SDK in my app as well. Do you think it is a problem that I had to hack together (BOOL)application:(UIApplication *)app openURL:(NSURL *)url sourceApplication:(NSString *)source annotation:(id)annotation method in order to please both API's like this 

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

  return [FBAppCall handleOpenURL:url

                sourceApplication:sourceApplication

                      withSession:[PFFacebookUtils session]] && [[DBSession sharedSession] handleOpenURL:url] && [[DBSession sharedSession] isLinked];

}

Instead of doing this like in the tutorial

- (BOOL)application:(UIApplication \*)app openURL:(NSURL \*)url
 sourceApplication:(NSString \*)source annotation:(id)annotation {
    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;
}  

In addition, what would be considered calling in the background thread as oppose to calling in the main thread?

Chez B.
New member | Level 1

In my app delegate method I checked to see if the session was linked and from my test the logs states that my session is linked.

if ([[DBSession sharedSession] isLinked]) {

    NSLog(@"The session IS linked");

  } else {

    NSLog(@"The session is not linked");

 

  }

 

I have checked my app console and is says that one users is connected to the app and it also says that I made one API call yesterday. However I don't see the test  image that I wast trying to upload to dropbox. I am wondering if  I am uploading the file incorrectly. This is my upload method

  • (void)uploadToDropBox:(UIImage *)img

{

  UIImage *testPic = [UIImage imageNamed:@"IMG_0005.jpg"];

  NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,

                                                             NSUserDomainMask,

                                                             YES);

  NSString *documentDirectory = [directories firstObject];

  NSString *imagePath = [documentDirectory stringByAppendingPathComponent:@"cardImage"];

  NSString *destDir = @"/";

  NSData *imageData = UIImagePNGRepresentation(testPic);

  [imageData writeToFile:imagePath atomically: YES];

  [self.restClient uploadFile:@"cardImage" toPath:destDir withParentRev:nil fromPath:imagePath];

  [self sendBDayCard];

  

}

 

Leah C.1
New member | Level 1

You may want to check that [UIImage imageNamed:@"IMG\_0005.jpg"] exists in your project and that it is getting saved to your local filesystem correctly. Another thing to check would be that your uploadToDropBox: method is getting called at all.

Steve M.
Dropbox Staff

Also, have you implemented the uploadedFile and uploadFileFailedWithError delegates as described in https://www.dropbox.com/developers/core/start/ios#uploading?

If the error delegate is called, you should be able to see exactly what failed.

Mitchel K.1
New member | Level 1

Hello

I'm trying out the sdk using a generated access token but I keep getting an "Access token not found" error even though the token works fine in android.

Here is my case (swift):

let dropboxSession = DBSession(appKey: DB_KEY, appSecret: DB_SECRET, root: kDBRootAppFolder)
dropboxSession.updateAccessToken(DB_ACCESS_TOKEN, accessTokenSecret: DB_ACCESS_TOKEN_SECRET, forUserId: DB_USER_ID)
DBSession.setSharedSession(dropboxSession)

So I'd appreciate to know what "DB_ACCESS_TOKEN_SECRET" should be since the generated token is a single string?

Steve M.
Dropbox Staff

Mitchel, your issue doesn't seem related to the one in this thread. Please start a new thread.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Mitchel K.1 New member | Level 1
  • User avatar
    Steve M. Dropbox Staff
  • User avatar
    Leah C.1 New member | Level 1
What do Dropbox user levels mean?