cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Update: Find information on Dropbox support during COVID-19 here
Close

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: 
Highlighted

Help migrating from v1 to v2 Objective C

Explorer | Level 4

Hello,

 

I am not very familiar with Dropbox API, though I got version 1 working just fine on my app.  But I am not really following the path from V1 to V2 all that well.

 

I am attempting to do some very simple things, but find this new API hard to digest I thought I would ask.

 

App Delegate Code went from this:

 

DBSession *dbSession = [[DBSession alloc]
initWithAppKey:@"my key"
appSecret:@"my secret"
root:kDBRootAppFolder]; // either kDBRootAppFolder or kDBRootDropbox
[DBSession setSharedSession:dbSession];

 

To This:

 

[DropboxClientsManager setupWithAppKey:@"my key"];

 

Initially I did the following:

 

 

if ([[DBSession sharedSession] isLinked]) {
  [self.restClient loadMetadata:@"/"];
}

 

I replaced with this:

 

 

if([DropboxClientsManager authorizedClient] != nil ) {
    [[client.filesRoutes getMetadata:@"/"]response:^(DBFILESMetadata * _Nullable result, DBFILESGetMetadataError * _Nullable routeError, DBRequestError * _Nullable error) {}

 

I am not sure if I missed anything, but I can't seem to get back from that, it it always thinks it's not approved in the ios simulator.

 

My original calls are this:

 

[restClient loadMetadata:@"/"];
[restClient uploadFile:fileName toPath:destDir withParentRev:rev fromPath:path];
[restClient loadFile:@"" into:localpath];

 

However after the first link, it doesn't seem to stick...

12 Replies 12
Highlighted

Re: Help migrating from v1 to v2 Objective C

Dropboxer
Dropboxer

Did you follow all of the instructions for setting up the app authorization flow?

 

https://github.com/dropbox/dropbox-sdk-obj-c#handling-the-authorization-flow

 

If so, what error(s) are you getting?

 

A few notes from what you shared so far though:

  • In API v2, the Dropbox root folder is identified by the empty string "", not "/".
  • The getMetadata method doesn't supported the root folder. In API v2, you should use listFolder[Continue] to list the contents of folders.
  • You can use reauthorizeClient to retrieve a previously authorized client.
Highlighted

Re: Help migrating from v1 to v2 Objective C

Explorer | Level 4

From what I can tell - yes....

 

This is the exception being thrown.

 

2017-01-03 13:59:38.192 ServiceReport[14621:705002] -canOpenURL: failed for URL: "dbapi-8-emm://1/connect" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"

 

 

- (BOOL)application:(UIApplication application didFinishLaunchingWithOptions:(NSDictionary launchOptions
{
    //Set up dropbox link
    [DropboxClientsManager setupWithAppKey:@"mykey"];    
    return YES;
}

 

- (BOOL)application:(UIApplication app openURL:(NSURL url
            options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> options {
    DBOAuthResult *authResult = [DropboxClientsManager handleRedirectURL:url];
    if (authResult != nil) {
        if ([authResult isSuccess]) {
            NSLog(@"Success! User is logged into Dropbox.");
        } else if ([authResult isCancel]) {
            NSLog(@"Authorization flow was manually canceled by user!");
        } else if ([authResult isError]) {
            NSLog(@"Error: %@", authResult);
        }
    }
    return NO;
}

 

I get the window to log in and get success but then it doesn't remember it next time I launch.

 

My baisc use case is pretty simple

 

1) Check to see if I am linked to dropbox.

2) Check to see if 3 specific files exsit (if not I assume no backup was done)

3) Provide an option to link to DB

4) Provide an option to perform a back up - this just copies three files up

5) Provide an option to restore from a backup - this just copies the three files down.

 

It was pretty straight forward before, but I am getting lost in the translation to the new API.

Highlighted

Re: Help migrating from v1 to v2 Objective C

Dropboxer
Dropboxer

The canOpenURL error would just indicate that the application being looked for (i.e., the official Dropbox app in this case) isn't found. That's expected on the simulator, or on a device without the official device installed. This error is safe to ignore, as the SDK will fall back to using the browser.

 

Can you share the steps to reproduce, code, and output/error for the isue with it not remembering the user?

Highlighted

Re: Help migrating from v1 to v2 Objective C

Explorer | Level 4

Thanks - that makes sense.  I was trying to scale back my dropbox controller to post - which I will if I can make sense of it.  

 

Here is the basics of what I have:

 

- (void)viewDidLoad{
    [super viewDidLoad];
}
- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // Do any additional setup after loading the view.
    // Reference after programmatic auth flow
    self.restClient = [DropboxClientsManager authorizedClient];
    if (!networkLost) {
        [self checkForBackups];
    }
}
- (void) checkForBackups {
    //Determine if backup exists
    if(self.restClient != nil ) {
        _loadingView = [LoadingView loadingViewInView:[self.view.window.subviews objectAtIndex:0] withMessage:@"Checking for existing backup...."];
       
        [[self.restClient.filesRoutes getMetadata:@""]response:^(DBFILESMetadata * _Nullable result, DBFILESGetMetadataError * _Nullable routeError, DBRequestError * _Nullable error) {
            if(result != nil){
                if ([result isKindOfClass:[DBFILESFolderMetadata class]]){
                    //NewObj* pNew = (NewObjoldObj;
                    DBFILESFolderMetadata * meta = (DBFILESFolderMetadata result;
                    files = [[NSMutableArray alloc]init];
                }
                [_loadingView removeView];
                
                
                //check to see if there is a backup to restore
                /*
                for (DBMetadata *file in files) {
                    if([file.path isEqualToString:@"/ServiceReport.sqlite"]){
                        hasSQLBackup = YES;
                    }
                    if([file.path isEqualToString:@"/ServiceReport.sqlite-shm"]){
                        hasSHMBackup = YES;
                    }
                    if([file.path isEqualToString:@"/ServiceReport.sqlite-wal"]){
                        hasWALBackup = YES;
                    }
                }
                 */
            }
    
        }];
        
    }
}

This call seems to crash the app - but I get no error.  Then when I run it again, it wants to link the account again.

Highlighted

Re: Help migrating from v1 to v2 Objective C

Explorer | Level 4

This I think is the basics of what I am attempting - hopefully it makes sense.  I am simply uploading the SQLITE db and files.

 

#import "AppDelegate.h"
#import "DropBoxViewController.h"
#import "LoadingView.h"
#import "ConvertNSDateToString.h"
//#import <DropboxSDK/DropboxSDK.h>

#import <ObjectiveDropboxOfficial/ObjectiveDropboxOfficial.h>

@interface DropBoxViewController ()  <UIAlertViewDelegate>

//@property (nonatomic, strong) DBRestClient *restClient;
@property (nonatomic, strong) DropboxClient *restClient;
@property (nonatomic, weak) LoadingView *loadingView;


@end

NSMutableArray *files = nil;

@implementation DropBoxViewController

- (void)viewDidLoad{
    [super viewDidLoad];
}
- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.restClient = [DropboxClientsManager authorizedClient];

    //self.restClient.delegate = self;
    if (!networkLost) {
        [self checkForBackups];
    }
}
- (void) checkForBackups {
    //Determine if backup exists
    if([DropboxClientsManager authorizedClient] != nil ) {      
        DropboxClient *client = [DropboxClientsManager authorizedClient];
        [[client.filesRoutes getMetadata:@""]response:^(DBFILESMetadata * _Nullable result, DBFILESGetMetadataError * _Nullable routeError, DBRequestError * _Nullable error) {
			//NOT SURE HOW TO CHECK THIS…
            //USED to be loadedMetadata
        }];
        [_loadingView removeView];
    }
}


- (IBAction)linkToDropBox:(id)sender {
    [DropboxClientsManager authorizeFromController:[UIApplication sharedApplication]
                                 controller:self
                                 openURL:^(NSURL *url) {
                                 	[[UIApplication sharedApplication] openURL:url];
                                 }
                                 browserAuth:YES];
    [self checkForBackups];
}
//TODO - FIX THIS COMMENT
/*
- (void)restClient:(DBRestClient client loadedMetadata:(DBMetadata metadata {
    if (metadata.isDirectory) {
        files = [[NSMutableArray alloc]init];
        for (DBMetadata *file in metadata.contents) {
            [files addObject:file];
        }
    }
    
    //check to see if there is a backup to restore
    for (DBMetadata *file in files) {
        if([file.path isEqualToString:@"/ServiceReport.sqlite"]){
            hasSQLBackup = YES;
        }
    }
    [self displayDropboxButtons];
}
*/

- (void) startBackUp {
    _loadingView = [LoadingView loadingViewInView:[self.view.window.subviews objectAtIndex:0] withMessage:@"Backing up data to Dropbox...."];
    [self uploadFileToDropbox:@"ServiceReport.sqlite"];
}

- (void) uploadFileToDropbox:(NSStringfileName {
    NSString *rev = nil;
    //check to see if there is a backup to to overwrite
    
    //TODO - FIX THIS COMMENT
    /*
    for (DBMetadata *file in files) {
        if([file.filename isEqualToString:fileName]){
            rev = file.rev;
            break;
        }
    }
    
    NSString *localDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *dbPath = [localDir stringByAppendingPathComponent:fileName];
    NSString *destDir = @"/";
    [self.restClient uploadFile:fileName toPath:destDir withParentRev:rev fromPath:dbPath];
     */
}

//TODO - FIX THIS COMMENT
/*
- (void)restClient:(DBRestClient client uploadedFile:(NSString destPath from:(NSString srcPath metadata:(DBMetadata metadata {
    NSLog(@"File uploaded successfully to path: %@", metadata.path);
}
 */

//This is the call to query for the file to restore and its call backs
//TODO - FIX THIS COMMENT

/*
- (void) startTheRestoreProcess {
    _loadingView = [LoadingView loadingViewInView:[self.view.window.subviews objectAtIndex:0] withMessage:@"Restoring from backup...."];
    //create the local path
    NSString *localDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *localPath = [localDir stringByAppendingPathComponent:@"ServiceReport.bak"];
    [self.restClient loadFile:@"/ServiceReport.sqlite" intoPath:localPath];
}
- (void)restClient:(DBRestClient client loadedFile:(NSString localPath contentType:(NSString contentType metadata:(DBMetadata metadata {
    NSLog(@"File loaded into path: %@", localPath);
}
 */

/*
 This is the error handling form Dropbox Callbacks
 */
//TODO - FIX THIS COMMENT
/*
- (void)restClient:(DBRestClient client loadMetadataFailedWithError:(NSError error {
    NSLog(@"Error loading metadata: %@", error);
    [self showErrorMessage:error];
}
- (void)restClient:(DBRestClient client uploadFileFailedWithError:(NSError error {
    NSLog(@"File upload failed with error: %@", error);
    [self showErrorMessage:error];
}
- (void)restClient:(DBRestClient client loadFileFailedWithError:(NSError error {
    if (error.code == 404) {
        NSLog(@"Your file was not found");
        [self showErrorMessage:error];
    }else if (error.code == 401) {
        NSLog(@"Your account is no longer authorized.");
        [self showAccountNotLinked];
        
    }else {
        NSLog(@"NSError ** : %@", [error userInfo]);
        [self showErrorMessage:error];
    }
}
 */
- (void) relinkToDropbox {
    /*
    [[DBSession sharedSession] unlinkAll];
     */
    [DropboxClientsManager unlinkClients];
    _restClient = nil;
    [self linkToDropBox:nil];
}

@end
Highlighted

Re: Help migrating from v1 to v2 Objective C

Dropboxer
Dropboxer

The app shouldn't be crashing without some sort of error. Can you double check the console in Xcode?

Highlighted

Re: Help migrating from v1 to v2 Objective C

Explorer | Level 4

It's weird - The only error I show is the one noted above. 

 

This always returns nil

 

self.restClient = [DropboxClientsManager authorizedClient];

Highlighted

Re: Help migrating from v1 to v2 Objective C

Dropboxer
Dropboxer
If authorizedClient is nil, that would be why your app doesn't seem to "remember" the user. That's presumably related to the crash you're getting. Perhaps the app is crashing before the access token is being stored. Can you step through in the debugger to at least see where the crash is occurring?
Highlighted

Re: Help migrating from v1 to v2 Objective C

Explorer | Level 4

I have tried - but the weird thing is once it hands back control to my application, it has disconnected from the debugger.

 

So I tried the following to force it to log something, but it doesn't.

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.restClient = [DropboxClientsManager authorizedClient];

    if (!networkLost) {
        [self checkForBackups];
    }
}
- (void) checkForBackups {
    //Determine if backup exists
    NSLog(@"trying to check for backups");
    
    if(self.restClient != nil ) {
        //_loadingView = [LoadingView loadingViewInView:[self.view.window.subviews objectAtIndex:0] withMessage:@"Checking for existing backup...."];        
        NSLog(@"You are linked: %@", self.restClient);
        
       
        [[self.restClient.filesRoutes getMetadata:@""]response:^(DBFILESMetadata * _Nullable result, DBFILESGetMetadataError * _Nullable routeError, DBRequestError * _Nullable error) {
            if(result != nil){
            }
            if(error != nil || routeError != nil){
                 NSLog(@"Error: %@", error);
                 NSLog(@"Error: %@", routeError);
            }
    
        }];
        
    }

}

 

 

 

So while I am in the same instance - when I open this after linking, it crashes.  I have placed log statements - but they aren't fired.  It's just odd..

 

Polls
Do you know how to organize your files and folders?
We have created a guide on folder best practice, so you can get organized now check it out here.

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Who's talking

Top contributors to this post

What do Dropbox user levels mean?
Need more support?