我正在使用他们网站上可用的SDK将Dropbox添加到我的应用中。 [[DBSession sharedSession] linkFromController:self];
与帐户链接后,是否可以调用某种方法?
基本上,我想在应用尝试登录Dropbox后再调用[self.tableView reloadData]
。登录甚至不需要区分登录成功与否。
Dropbox SDK使用您的AppDelegate作为回调接收者。因此,当您调用[[DBSession sharedSession] linkFromController:self];
时,Dropbox SDK将在任何情况下调用您的AppDelegate的– application:openURL:sourceApplication:annotation:
方法。
因此,在AppDelegate中,您可以通过[[DBSession sharedSession] isLinked]
检查登录是否成功。不幸的是,您的viewController没有回调,因此您必须通过其他方式(直接引用或发布通知)来通知它。
- (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页面进行登录。 Apple有时会不接受此类应用程序。因此,Dropbox成员介绍了内部视图控制器登录名,但保留了AppDelegate作为结果的接收者。如果用户在其设备上安装了Dropbox App,则登录名将直接定向到Dropbox App,并在返回时调用AppDelegate。
在应用程序委托中添加:
- (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
}
}