问题描述
我使用handleOpenURL()作为自定义网址计划,从电子邮件中的链接启动我的应用程序。工作完美,我可以根据链接中的网址参数在我的应用程序。
I'm using handleOpenURL() for a Custom URL Scheme to launch my app from a link in an email. Works perfectly and I can do stuff in my app based on the URL parameters in the link.
问题是handleOpenURL()似乎没有得到调用,当我的应用程序做冷启动(不在后台运行)。是否有其他处理程序可用于冷启动与已运行的实例?
The problem is handleOpenURL() doesn't seem to get call when my app does a cold start (not running in the background). Is there another handler that I can use for a cold start vs an already running instance?
或
有一个全局变量,我可以读,将告诉我什么调用URL是?
Is there a global variable that I can read that will tell me what the invoke URL was? I read about invokeString, but it never seems to be set?
我正在使用PhoneGap 2.0
I'm using PhoneGap 2.0
推荐答案
如果仔细阅读应用程序上面的注释:handleOpenURL方法,您可能会理解:
if you read carefully the comment above the application:handleOpenURL method, you will probably understand:
// 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
没有运行。我的解决方案是调整项目与以下更改:
This methods will not be called if the application is not running. My solution was to tweak project with the following changes:
MainViewController.h
MainViewController.h
@interface MainViewController : CDVViewController
@property (nonatomic, retain) NSString *URLToHandle;
@end
MainViewController.m
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;
AppDelegate.m
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;
[...]
}
Cyril
另外注意:测试时确保停止xcode。当xcode运行应用程序将抛出异常。如果我停止xcode这个解决方案工作正常。
Additional note: when testing make sure you stop xcode. When xcode was running application would throw an exception. if I stop the xcode this solution works fine.
这篇关于如何在PhoneGap中处理自定义URL方案以进行冷启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!