适用于iOS的Dropbox SDK - 检测登录取消

时间:2012-12-15 17:26:31

标签: ios dropbox

我开始使用适用于iOS的DropBox SDK,我看到检测登录是否成功的代码是这样的:

在AppDelegate中

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    if ([[DBSession sharedSession] isLinked])
    {
       // Success
    }
    else
    {
        // Failed
    }
    return YES;
}

如果发生故障,我该如何确定原因?我想至少区分错误和取消。

3 个答案:

答案 0 :(得分:3)

识别取消

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

答案 1 :(得分:1)

如果有人碰到这个问题并且卡在了Bala的回答上,因为该方法handleOpenURL已经过时了。 Dropbox现在使用openURL。 openURL进入app delegate。

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

基本上,为了检测取消,您只需检查网址的组成部分即可取消'取消'如果发现您只是在适用的情况下向您的应用程序发送通知,告知用户取消了该过程。

通知发布的观察员代码,将其放在您想要检测上面发布的通知的任何位置。

    [[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*/}

答案 2 :(得分:0)

我使用下面的代码对我有用,如果用户取消了Dropbox登录或用户登录成功,它会在两种情况下都有帮助

- (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:@"/"];
 }
}

我希望它会有所帮助。