Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropbox SDK - linkFromController: delegate or callback?

I'm adding Dropbox to my app using the SDK available on their site. Is there any way of calling some method once [[DBSession sharedSession] linkFromController:self]; links with an account?

Basically I'd like to call [self.tableView reloadData] once the app has tried to log in to Dropbox. It doesn't even need to discriminate between a successful or unsuccessful login.

like image 910
Josh Avatar asked Aug 31 '12 10:08

Josh


2 Answers

The Dropbox SDK uses your AppDelegate as a callback receiver. So when you have called [[DBSession sharedSession] linkFromController:self]; the Dropbox SDK will in any case call your AppDelegate's – application:openURL:sourceApplication:annotation: method.

So within the AppDelegate you can check by [[DBSession sharedSession] isLinked] if the login was successful or not. Unfortunately, there is no callback for your viewController, so you have to notify it by other means (direct reference or post a notification).

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    if ([[DBSession sharedSession] handleOpenURL:url]) {
        if ([[DBSession sharedSession] isLinked]) {
            // At this point you can start making API Calls. Login was successful
            [self doSomething];
        } else {
            // Login was canceled/failed.
        }
        return YES;
    }
    // Add whatever other url handling code your app requires here
    return NO;
}

This rather strange way of calling the app back was introduced by Dropbox due to an issue with Apple's policies. In older versions of the SDK, an external Safari page would have been opened to do the login. Apple would not accept such Apps at some point in time. So the Dropbox guys introduced the internal view controller login but kept the AppDelegate as the receiver of the results. If the user has the Dropbox App installed on his device, the login would be directed to the Dropbox App an also the AppDelegate will be called on return.

like image 139
marcus Avatar answered Nov 20 '22 04:11

marcus


in App delegate add:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { 
    if ([[DBSession sharedSession] handleOpenURL:url]) {

        [[NSNotificationCenter defaultCenter]
         postNotificationName:@"isDropboxLinked"
         object:[NSNumber numberWithBool:[[DBSession sharedSession] isLinked]]];

        return YES;
    }

    return NO;
}

and in you custom class:

- (void)viewDidLoad {
    [super viewDidLoad];

    //Add observer to see the changes
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isDropboxLinkedHandle:) name:@"isDropboxLinked" object:nil];

}

and

  - (void)isDropboxLinkedHandle:(id)sender
{
    if ([[sender object] intValue]) {
       //is linked.
    }
    else {
       //is not linked
    }
}
like image 44
Arash Zeinoddini Avatar answered Nov 20 '22 02:11

Arash Zeinoddini