Dropbox SDK - linkFromController: delegate or callback?
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.
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
}
}