我开始使用适用于iOS的DropBox SDK,我看到检测登录是否成功的代码是这样的:
在应用程序代表中:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
if ([[DBSession sharedSession] isLinked])
{
// Success
}
else
{
// Failed
}
return YES;
}
如果发生故障,如何确定原因?我想至少区分错误和取消。
标识取消
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
NSArray *components = [[url path] pathComponents];
NSString *methodName = [components count] > 1 ? [components objectAtIndex:1] : nil;
if ([methodName isEqual:@"cancel"]) {
NSLog(@"Dropbox link Cancelled");
}
}
如果有人遇到这个问题并卡在巴拉的答案上,那是因为该方法handleOpenURL
已经过时了。Dropbox 现在使用openURL
。openURL 进入应用程序委托。
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url sourceApplication:(NSString *)source annotation:(id)annotation {
if ([[DBSession sharedSession] handleOpenURL:url]) {
if ([[DBSession sharedSession] isLinked]) {
NSLog(@"App linked successfully!");
// At this point you can start making API calls
// Send notification to load an initial root dropbox path
[[NSNotificationCenter defaultCenter] postNotificationName:@"getDropboxRoot" object:self];
}else{// Add whatever other url handling code your app requires here in this else
//if the user clicks cancel that will appear here in the methodName variable,
//we post a notification to wherever we want.
NSArray* components = [[url path] pathComponents];
NSString *methodName = [components count] > 1 ? [components objectAtIndex:1] : nil;
if ([methodName isEqual:@"cancel"]) {
NSLog(@"Dropbox link Cancelled");
[[NSNotificationCenter defaultCenter] postNotificationName:@"dropboxRegistrationCancel" object:self];
}
}
return YES;
}
return NO;}
基本上,要检测取消,您只需检查 url 组件中的单词"取消",如果发现,您只需在适用于您的应用程序的地方发送通知,告诉它用户取消了该过程。
通知发布的观察者代码,将其放在要检测上面发布的通知的任何位置。
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(dropboxRegistrationCancel)
name:@"dropboxRegistrationCancel"
object:nil];
-(void) dropboxRegistrationCancel{
/*do stuff here that you want to do when the @"dropboxRegistrationCancelled" is triggered*/}
我使用了以下对我有用的代码,如果用户取消保管箱登录或用户登录成功,它在这两种情况下都有帮助
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
NSString *stringUrl = [url absoluteString];
if ([stringUrl containsString:@"cancel"]) {
// Handle if user cancelled the login
[[NSNotificationCenter defaultCenter] postNotificationName:@"dropboxRegistrationCancel" object:self];
return NO;
}
if ([[DBSession sharedSession] handleOpenURL:url]) {
if ([[DBSession sharedSession] isLinked]) {
// From below notification u can fetch your data from Dropbox
[[NSNotificationCenter defaultCenter]
postNotificationName:@"isDropboxLinked"
object:[NSNumber numberWithBool:[[DBSession sharedSession] isLinked]]];
// Add whatever other url handling code your app requires here
}
return YES;
} return NO;
}
为了在登录后第一次获取文件,请将此代码放在您的类中,您将在viewDidLoad中显示文件列表
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(isDropboxLinkedHandle:) name:@"isDropboxLinked" object:nil];
以及isDropboxLinkedHandle的实现:
- (void)isDropboxLinkedHandle:(id)sender {
if ([[sender object] intValue]) {
// fetch all files
[[self restClient] loadMetadata:@"/"];
}
}
我希望它会有所帮助,谢谢。