2016-05-09 9 views
1

私は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コマンドを適切に活用する方法がわかりません。誰にもアイデアはありますか?

+0

HTML/javascriptのネイティブコードと通信する必要がある場合は、この方法を使用できますが、Cordovaで作成されたネイティブプロジェクトを混乱させ、WebViewデリゲートメソッド(条件付きブロック)を作成する必要があります。私は通常、それらのシナリオのためのネイティブプラグインを作成することを好む、または私ははるかにクリーンで簡単です。 iOSのプラグインの例についてはgoogleをご覧になり、[こちら](https://cordova.apache.org/docs/en/5.1.1/guide/platforms/ios/plugin.html)をご覧ください。 –

+0

ありがとうございました!それは信じられないほど役に立つ!私はまだ自分のプラグインを作成する構造について少し混乱しています。私は新鮮なCordovaプロジェクトを持っています。私は何が必要なのか、どこに行くべきか教えていただけますか? 'plugin.xmlファイルを使ってこのマークアップを自動的に挿入する 'のようなものについてはわかりません。私はxcodeprojectプラグインフォルダにCDVPluginファイルが必要であることを知っています。config.xmlファイルをiosプラットフォームで構成することができます。ありがとうございました! – AlecGamble

答えて

1

これは、あなたがiOSのプラグインを作成するために必要なものです:

  • plugin.xmlの
  • のsrc/IOS/YourPluginName.h
  • のsrc/IOS/YourPluginName.m
  • WWW/YourPluginName .jsファイル

plugin.xmlの

<?xml version="1.0" encoding="UTF-8"?> 
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" 
      id="yourpluginid" 
     version="1.0.0"> 
    <name>YourPluginName</name> 
    <description>Your Plugin Description</description> 
    <license>Apache 2.0</license> 
    <js-module src="www/YourPluginName.js" name="YourPluginName"> 
     <clobbers target="YourPluginName" /> 
    </js-module> 
    <platform name="ios"> 
     <config-file target="config.xml" parent="/*"> 
      <feature name="YourPluginName"> 
       <param name="ios-package" value="YourPluginName"/> 
      </feature> 
     </config-file> 
     <header-file src="src/ios/YourPluginName.h" /> 
     <source-file src="src/ios/YourPluginName.m" /> 
    </platform> 
</plugin> 

のsrc/IOS/YourPluginName.h

#import <Foundation/Foundation.h> 
#import <Cordova/CDV.h> 

@interface YourPluginName : CDVPlugin 

- (void)pluginMethodName:(CDVInvokedUrlCommand*)command; 

@end 

のsrc/IOS/YourPluginName。(あなたのJavaScriptから次に

cordova plugin add pluginfolderpath 

var exec = require('cordova/exec'); 

var YourPluginName = { 
    pluginMethodName:function(param, successCallback, errorCallback) { 
     exec(successCallback, errorCallback, "YourPluginName", "pluginMethodName", [param]); 
    } 
}; 

module.exports = YourPluginName; 

があなたのコルドバプロジェクトのルートフォルダから入力したフォルダ内の各ファイルを入れて、/ YourPluginName.jsメートル

#import "YourPluginName.h" 

@implementation YourPluginName 

- (void)pluginMethodName:(CDVInvokedUrlCommand*)command { 
    CDVPluginResult* pluginResult = nil; 
    //Get param 
    NSString *param = [command.arguments objectAtIndex:0]; 
    //Do something 
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; 
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; 
} 

WWW onDeviceReadyイベントの後)に行うことができます:

YourPluginName.pluginMethodName("param", function(){}, function(){}); 
+0

**非常に**ありがとうございます! – AlecGamble

関連する問題