Dropbox SDK - linkFromController: delegate or callback?

孤街浪徒 提交于 2019-11-30 19:01:05

The Dropbox SDK uses your AppDelegate as callback reciever. 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 not 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
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!