0

我正在从 PhoneGap 应用程序调用 Objective C 以使用 Dropbox 执行功能。

我遇到的问题是,在将文件从 Dropbox 下载到手机的本地文件系统之前,我对 JavaScript 的回调正在触发。

这是我的目标 C 方法,它开始从 Dropbox 下载文件 -

- (void) restore:(CDVInvokedUrlCommand*)command
{

    CDVPluginResult* pluginResult = nil;
    NSString* javaScript = nil;
    NSLog(@"Dropbox restore method is executing");

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
    NSString *localPath = [documentsDirectory stringByAppendingPathComponent:@"PocketHealth-backup.bk"];
    NSString *dropBoxFile = @"/PocketHealth-backup.bk";

    [[self restClient] loadFile:dropBoxFile intoPath:localPath];

    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    javaScript = [pluginResult toSuccessCallbackString:command.callbackId];

    [self writeJavascript:javaScript];

}

Objective C 中有一个方法在 Dropbox 文件下载到手机时执行。在 Dropbox 文件下载完成后,我确实得到了此方法输出的 NSLog。问题是我需要知道此事件何时发生,然后才能返回我的 JavaScript 回调。

这是 Dropbox 文件下载完成时执行的方法 -

- (void) restClient:(DBRestClient*)client loadedFile:(NSString*)localPath 
{
    NSLog(@"File loaded into path: %@", localPath);
}

How can I wait until the Dropbox file download is complete before returning the JavaScript callback that is in the restore method?

4

1 回答 1

1

Instead of writing the JavaScript into a local variable inside your restore method, add it as an iVar to your class, remove the call to writeJavascript: in your restore method and call writeJavascript from restClient:loadedFile:. Then it should get called when the download finished instead of when the actual download is started.

- (void) restore:(CDVInvokedUrlCommand*)command
{

    CDVPluginResult* pluginResult = nil;
    NSLog(@"Dropbox restore method is executing");

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
    NSString *localPath = [documentsDirectory stringByAppendingPathComponent:@"PocketHealth-backup.bk"];
    NSString *dropBoxFile = @"/PocketHealth-backup.bk";

    [[self restClient] loadFile:dropBoxFile intoPath:localPath];

    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    self.javaScript = [pluginResult toSuccessCallbackString:command.callbackId];
}

- (void) restClient:(DBRestClient*)client loadedFile:(NSString*)localPath 
{
    NSLog(@"File loaded into path: %@", localPath);
    [self writeJavascript:javaScript];
}
于 2012-09-28T19:17:38.993 回答