Dropbox的SDK – linkFromController:委托或callback?

我使用他们的网站上提供的SDK将Dropbox添加到我的应用程序。 有没有办法调用一些方法[[DBSession sharedSession] linkFromController:self]; 链接一个帐户?

基本上我想调用[self.tableView reloadData]一旦应用程序已经尝试login到Dropbox。 它甚至不需要区分成功或不成功的login。

Dropbox SDK使用您的AppDelegate作为callback接收器。 所以当你调用[[DBSession sharedSession] linkFromController:self]; Dropbox SDK将在任何情况下调用您的AppDelegate的– application:openURL:sourceApplication:annotation:方法。

因此,在AppDelegate中,如果login成功或不成功,您可以通过[[DBSession sharedSession] isLinked]进行检查。 不幸的是,你的viewController没有callback,所以你必须通过其他方式通知它(直接引用或发布通知)。

 - (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; } 

由于苹果的政策问题,Dropbox引入了这种调用应用程序的奇怪方式。 在较早版本的SDK中,外部Safari页面将被打开以进行login。 苹果公司在某个时候不会接受这样的应用程序。 所以Dropbox的人介绍了内部视图控制器login,但保留了AppDelegate作为结果的接收者。 如果用户在其设备上安装了Dropbox应用程序,则login将被引导至Dropbox应用程序,并且还将返回AppDelegate。

在App委托中添加:

 - (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; } 

并在你自定义的类:

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

  - (void)isDropboxLinkedHandle:(id)sender { if ([[sender object] intValue]) { //is linked. } else { //is not linked } }