1

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

在 AppDelegate 中:

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

在失败的情况下,我如何确定原因?我想至少区分错误和取消。

4

3 回答 3

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");
    }
}
于 2012-12-17T08:58:50.887 回答
1

如果有人遇到此问题并停留在 Bala 的答案上,那是因为该方法handleOpenURL已过时。DropboxopenURL现在使用。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*/}
于 2015-07-19T00:55:39.277 回答
0

我使用了下面对我有用的代码,如果用户取消了保管箱登录或用户登录成功,这两种情况都有帮助

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

我希望它会有所帮助谢谢。

于 2016-06-17T07:38:37.073 回答