Dropbox SDK - linkFromController: delegate or call

2019-02-12 12:58发布

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.

2条回答
三岁会撩人
2楼-- · 2019-02-12 13:32

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.

查看更多
叼着烟拽天下
3楼-- · 2019-02-12 13:37

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
    }
}
查看更多
登录 后发表回答