5

カスタムURLスキームにhandleOpenURL()を使用して、メール内のリンクからアプリを起動しています。完璧に動作し、リンクのURLパラメーターに基づいてアプリで何かを行うことができます。

問題は、アプリがコールドスタート(バックグラウンドで実行されていない)を実行したときにhandleOpenURL()が呼び出されないように見えることです。コールドスタートとすでに実行中のインスタンスに使用できる別のハンドラーはありますか?

また

呼び出しURLが何であったかを教えてくれる読み取り可能なグローバル変数はありますか?invokeStringについて読みましたが、設定されていないようです。

PhoneGap2.0を使用しています

4

2 に答える 2

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

アプリケーションが実行されていない場合、このメソッドは呼び出されません。私の解決策は、次の変更を加えてプロジェクトを微調整することでした。

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を停止すると、このソリューションは正常に機能します。

于 2012-10-03T09:43:28.163 に答える
3

ちなみに、誰かがこれに遭遇した場合、欠けているのは、で定義されているのと同じ方法URLToHandleAppDelegate(.hと.m)で定義することだけですMainViewController

また、次のAppDelegate.m場所から割り当てを逆にする必要があります。

((MainViewController *)self.viewController).URLToHandle = URLToHandle;

に:

NSString* jsString = [NSString stringWithFormat:@"window.setTimeout(function() {handleOpenURL(\"%@\"); },1);", url];
((MainViewController *)self.viewController).URLToHandle = jsString;

AppDelegate基本的に、からにURLを転送する必要がありますMainViewController

setTimeoutそれ以外の場合は機能しません。

于 2013-09-01T19:10:42.820 に答える