如果你仔细阅读 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
如果应用程序未运行,则不会调用此方法。我的解决方案是通过以下更改来调整项目:
MainViewController.h
@interface MainViewController : CDVViewController
@property (nonatomic, retain) NSString *URLToHandle;
@end
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
- (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;
[...]
}
希望对您有所帮助。
西里尔
附加说明:测试时请确保停止 xcode。当 xcode 运行时,应用程序会抛出异常。如果我停止 xcode,这个解决方案可以正常工作。