私はCordovaアプリケーションを作成しましたが、xcodeにネイティブ機能を追加するだけです。ここに示されているように私は自分のアプリケーションからURLをインターセプトしたい:How to invoke Objective C method from Javascript and send back data to Javascript in iOS?iOS Cordovaアプリケーション - shouldStartLoadWithRequest
は、だから私のHTMLに、私は
index.htmlを
<a class="MyButton" id="id" href="req://ResultA">Item A</a>
次にリンク
が含まれているIちょうどUIViewControllerから継承する本当に基本的なヘッダーファイルを持っていますMyViewController.h私MyViewController.m
ファイル内
#import <UIKit/UIKit.h>
#import <Cordova/CDVViewController.h>
@interface MyViewController : UIViewController
@end
そして、私がやりたいすべてが私のリンクからURLを解釈するのです。私は以下のようなものを達成したい。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CDVViewController* StorySelectCDVViewController = [CDVViewController new];
StorySelectCDVViewController.view.frame = self.view.frame;
[self.view addSubview:StorySelectCDVViewController.view];
}
...
-(bool)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[url scheme] isEqualToString:@"req"])
//Store ResultA as a variable for later use.
}
しかしCDVViewControllerためMyViewController.m。これと同様に、私は、このメソッドに関して、ViewControllerをCDVViewControllerのデリゲートとして使用しようとするのは悪い習慣であることを読んできました。
代わりに私はまた、MyViewController
#import <Cordova/CDVViewController.h>
#import <Cordova/CDVCommandDelegateImpl.h>
#import <Cordova/CDVCommandQueue.h>
@interface MyViewController : CDVViewController
@end
@interface MyCommandDelegate : CDVCommandDelegateImpl
@end
@interface MyCommandQueue : CDVCommandQueue
@end
MyViewController.h
...コルドバアプリケーションによって提供さMainViewControllerをコピーして、CDVViewControllerを継承するのViewControllerを作成しようとすることができます。 m
#import "MyViewController.h"
@implementation MyViewController
- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Uncomment to override the CDVCommandDelegateImpl used
// _commandDelegate = [[MyCommandDelegate alloc] initWithViewController:self];
// Uncomment to override the CDVCommandQueue used
// _commandQueue = [[MyCommandQueue alloc] initWithViewController:self];
}
return self;
}
- (id)init
{
self = [super init];
if (self) {
// Uncomment to override the CDVCommandDelegateImpl used
// _commandDelegate = [[MyCommandDelegate alloc] initWithViewController:self];
// Uncomment to override the CDVCommandQueue used
// _commandQueue = [[MyCommandQueue alloc] initWithViewController:self];
}
return self;
}
@end
@implementation MyCommandDelegate
#pragma mark CDVCommandDelegate implementation
- (id)getCommandInstance:(NSString*)className
{
return [super getCommandInstance:className];
}
- (NSString*)pathForResource:(NSString*)resourcepath
{
return [super pathForResource:resourcepath];
}
@end
@implementation MyCommandQueue
- (BOOL)execute:(CDVInvokedUrlCommand*)command
{
return [super execute:command];
}
@end
しかし、これを適切に変更してURLコマンドを適切に活用する方法がわかりません。誰にもアイデアはありますか?
HTML/javascriptのネイティブコードと通信する必要がある場合は、この方法を使用できますが、Cordovaで作成されたネイティブプロジェクトを混乱させ、WebViewデリゲートメソッド(条件付きブロック)を作成する必要があります。私は通常、それらのシナリオのためのネイティブプラグインを作成することを好む、または私ははるかにクリーンで簡単です。 iOSのプラグインの例についてはgoogleをご覧になり、[こちら](https://cordova.apache.org/docs/en/5.1.1/guide/platforms/ios/plugin.html)をご覧ください。 –
ありがとうございました!それは信じられないほど役に立つ!私はまだ自分のプラグインを作成する構造について少し混乱しています。私は新鮮なCordovaプロジェクトを持っています。私は何が必要なのか、どこに行くべきか教えていただけますか? 'plugin.xmlファイルを使ってこのマークアップを自動的に挿入する 'のようなものについてはわかりません。私はxcodeprojectプラグインフォルダにCDVPluginファイルが必要であることを知っています。config.xmlファイルをiosプラットフォームで構成することができます。ありがとうございました! – AlecGamble