简体   繁体   中英

Dropbox SDK for iOS - Detecting a login cancel

I am starting to use the DropBox SDK for iOS, and I saw that the code to detect if the login was successful or not is something like this:

in AppDelegate:

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

In the case of a failure, how can I identify the cause? I would like to at least distinguish between an error and a cancel.

To identify the Cancel

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

If anyone comes across this and gets stuck on Bala's answer it's because that method handleOpenURL is out of date. Dropbox uses openURL now. openURL goes in the 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;}

Basically to detect a cancel you just check the url components for word 'cancel' and if it's found you just send a notification wherever applies for your application to tell it that the user cancelled the process.

Observer code for your notification posting, put this wherever you want to detect the notification posted above.

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

I have used below code which worked for me, it helps in both conditions if user cancelled the dropbox login or user login successfully

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

For fetching files very first time after login place this code in your class where you are showing list of files in viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(isDropboxLinkedHandle:) name:@"isDropboxLinked" object:nil];

and implementation of isDropboxLinkedHandle:

- (void)isDropboxLinkedHandle:(id)sender {
if ([[sender object] intValue]) {
    // fetch all files 
    [[self restClient] loadMetadata:@"/"];
 }
}

I hope it will help thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM