HandleOpenURL not called after link to Dropbox - iOS

I started to learn the Dropbox API for the application that I have, where I would like the user to be able to back up the database file. The problem that I encountered is that after the user associates the application with his account (similar to logging in via Facebook), the application does not return to the foreground. When I manually return to the application, it is still on the backup screen, but the account was not connected (as far as I can tell), and the delegate method of the application handleOpenUrl is not called.

Any ideas? or maybe someone knows a good tutorial for this. The Dropbox sample application works just fine, and I'm working hard to use it as a guide, but obviously I messed up something.

Application Delegate:

#import "AppDelegate_iPad.h" #import <DropboxSDK/DropboxSDK.h> @interface AppDelegate_iPad () <DBSessionDelegate> @end @implementation AppDelegate_iPad @synthesize window,viewController; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.viewController = [[mainMenuViewController alloc]init]; [window addSubview:viewController.view]; //< this is a main menu viewcontroller for my app [self.window makeKeyAndVisible]; // Set these variables before launching the app NSString* appKey = @"XXXX"; NSString* appSecret = @"XXX"; NSString *root = kDBRootAppFolder; NSString* errorMsg = nil; if ([appKey rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]].location != NSNotFound) { errorMsg = @"Make sure you set the app key correctly in DBRouletteAppDelegate.m"; } else if ([appSecret rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]].location != NSNotFound) { errorMsg = @"Make sure you set the app secret correctly in DBRouletteAppDelegate.m"; } else if ([root length] == 0) { errorMsg = @"Set your root to use either App Folder of full Dropbox"; } else { NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"]; NSData *plistData = [NSData dataWithContentsOfFile:plistPath]; NSDictionary *loadedPlist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:0 format:NULL errorDescription:NULL]; NSString *scheme = [[[[loadedPlist objectForKey:@"CFBundleURLTypes"] objectAtIndex:0] objectForKey:@"CFBundleURLSchemes"] objectAtIndex:0]; if ([scheme isEqual:@"db-APP_KEY"]) { errorMsg = @"Set your URL scheme correctly in DBRoulette-Info.plist"; } } DBSession* session = [[DBSession alloc] initWithAppKey:appKey appSecret:appSecret root:root]; session.delegate = self; // DBSessionDelegate methods allow you to handle re-authenticating [DBSession setSharedSession:session]; [session release]; if (errorMsg != nil) { [[[[UIAlertView alloc] initWithTitle:@"Error Configuring Session" message:errorMsg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease] show]; } NSURL *launchURL = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey]; NSInteger majorVersion = [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] integerValue]; if (launchURL && majorVersion < 4) { // Pre-iOS 4.0 won't call application:handleOpenURL; this code is only needed if you support // iOS versions 3.2 or below [self application:application handleOpenURL:launchURL]; return NO; } return YES; } - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { /// this is never called if ([[DBSession sharedSession] handleOpenURL:url]) { if ([[DBSession sharedSession] isLinked]) { NSLog(@"App linked successfully!"); // At this point you can start making API calls } return YES; } return NO; } @end 

In the main menu, the user clicks the backup button and opens the following controller:

 #import "BackupManagerViewController.h" #import <DropboxSDK/DropboxSDK.h> #import <stdlib.h> @interface BackupManagerViewController () <DBRestClientDelegate> //@property (nonatomic, readonly) DBRestClient* restClient; @end @implementation BackupManagerViewController @synthesize itemsArray,delegate; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } #pragma mark - View lifecycle - (void)viewDidLoad { //[super viewDidLoad]; // Do any additional setup after loading the view from its nib. } -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation { return (orientation != UIDeviceOrientationLandscapeLeft) && (orientation != UIDeviceOrientationLandscapeRight); } - (IBAction)didPressLink { if (![[DBSession sharedSession] isLinked]) { [[DBSession sharedSession] link]; } else { [[DBSession sharedSession] unlinkAll]; [[[[UIAlertView alloc] initWithTitle:@"Account Unlinked!" message:@"Your dropbox account has been unlinked" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease] show]; } } -(DBRestClient *)restClient{ if (restClient == nil) { restClient = [[DBRestClient alloc]initWithSession:[DBSession sharedSession]]; restClient.delegate = self; } return restClient; } -(IBAction) closeButtonPressed { [delegate closeBackupManager]; } @end 
+4
source share
4 answers

What you need to check:

  • Make sure you do not have two applications with the same db-APP_KEY
  • Make sure that only one of them is implemented (not both) in the application deletion.

    (a) - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

    (b) - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

    Option (b) is deprecated, so please go to option (a) in new applications

  • In the URL scheme, you entered the correct APP_KEY .

+11
source

I ran into the same problem but got it working after removing the DBRoulette sample application from the simulator. I also uninstalled my application and restarted the simulator, but I'm not sure if these steps are needed.

+4
source

Have you added quotation mark url scheme in your info.plist app?

0
source

I believe this problem is related to running in the simulator. I ran it on the device and it worked fine.

0
source

Source: https://habr.com/ru/post/1385995/


All Articles