如何在PhoneGap中处理自定义URL方案以进行冷启动?

Som*_*gOn 5 cordova

我正在使用handleOpenURL()进行自定义URL方案,以便从电子邮件中的链接启动我的应用程序.完美的工作,我可以根据链接中的URL参数在我的应用程序中执行操作.

问题是当我的应用程序冷启动(不在后台运行)时,handleOpenURL()似乎没有调用.是否有另一个处理程序可用于冷启动与已运行的实例?

要么

是否有一个我可以阅读的全局变量,它会告诉我调用URL是什么?我读了一下invokeString,但似乎从来没有设置过?

我正在使用PhoneGap 2.0

cyr*_*lPA 3

如果你仔细阅读 application:handleOpenURL 方法上面的注释,你可能会明白:

// this happens while we are running ( in the background, or from within our own app )
// only valid if Calinda-Info.plist specifies a protocol to handle
- (BOOL) application:(UIApplication*)application handleOpenURL:(NSURL*)url
Run Code Online (Sandbox Code Playgroud)

如果应用程序未运行,则不会调用此方法。我的解决方案是通过以下更改来调整项目:

MainViewController.h

@interface MainViewController : CDVViewController
@property (nonatomic, retain) NSString *URLToHandle;
@end
Run Code Online (Sandbox Code Playgroud)

MainViewController.m

- (void) webViewDidFinishLoad:(UIWebView*) theWebView 
{
     if (self.URLToHandle)
     {         
         NSString* jsString = [NSString stringWithFormat:@"window.setTimeout(function() {handleOpenURL(\"%@\"); },1);", self.URLToHandle];
         [theWebView stringByEvaluatingJavaScriptFromString:jsString];
     }
     [...]
}

- (void)dealloc
{
    self.URLToHandle = nil;
    [super dealloc];
}

@synthesize URLToHandle;
Run Code Online (Sandbox Code Playgroud)

AppDelegate.m

- (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{   
    [...]
    self.viewController = [[[MainViewController alloc] init] autorelease];
    self.viewController.useSplashScreen = YES;
    self.viewController.wwwFolderName = @"www";
    self.viewController.startPage = @"index.html";
    self.viewController.view.frame = viewBounds;

    // Patch for handleOpenURL
    ((MainViewController *)self.viewController).URLToHandle = URLToHandle;

    [...]
}
Run Code Online (Sandbox Code Playgroud)

希望有帮助。

西里尔

附加说明:测试时请确保停止 Xcode。当 xcode 运行时,应用程序会抛出异常。如果我停止 xcode 这个解决方案工作正常。