我已经开始探索Dropbox API了解我希望用户能够备份数据库文件的应用程序。我遇到的问题是,在用户将应用程序与他们的帐户链接后(类似于通过Facebook登录),应用程序不会返回到前台。当我手动返回应用程序时,它仍然在备份屏幕上,但该帐户尚未链接(尽我所知)并且未调用handleOpenUrl应用程序委托方法。
有什么想法吗?或者也许有人知道一个很好的教程。示例Dropbox应用程序工作正常,我正在尽力使用它作为指导,但显然我已经搞砸了。
App 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
在主菜单中,用户按下了一个备份按钮,打开了以下视图控制器:
#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
答案 0 :(得分:11)
要检查的事项是
db-APP_KEY
确保在您的应用程序委托中只实现其中一个(而不是两者)。
(a)中
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
(b)中
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
选项(b)已弃用,请在新应用程序中选择(a)
您已在网址方案中输入了正确的APP_KEY
。
答案 1 :(得分:4)
我遇到了同样的问题,但是在从模拟器中删除示例应用DBRoulette
后才开始工作。我也删除了我自己的应用程序并重新启动了模拟器,但我不确定这些步骤是否必要。
答案 2 :(得分:0)
您是否已将展示框网址架构添加到应用的info.plist中?
答案 3 :(得分:0)
我认为这个问题与在模拟器中运行有关。我在设备上运行它,它工作正常。