2016-05-09 4 views
1

Ich habe eine Cordova-Anwendung erstellt und ich füge nur einige native Funktionalität in xcode. Ich möchte die URLs aus meiner Anwendung abzufangen, wie hier gezeigt wird:iOS Cordova-Anwendung - shouldStartLoadWithRequest

How to invoke Objective C method from Javascript and send back data to Javascript in iOS?

Also in meinem HTML I

index.html

<a class="MyButton" id="id" href="req://ResultA">Item A</a> 

dann einen Link enthalten habe ich haben Sie einfach eine wirklich einfache Header-Datei, die von der UIViewController

erbt MyViewController.h

#import <UIKit/UIKit.h> 
#import <Cordova/CDVViewController.h> 

@interface MyViewController : UIViewController 

@end 

Und dann in meiner MyViewController.m Datei alles, was ich tun möchte, ist die URL von meinem Link zu interpretieren. Ich möchte etwas wie unten erreichen.

MyViewController.m

- (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. 
} 

aber für die CDVViewController. Außerdem habe ich gelesen, dass es eine schlechte Übung ist, den ViewController als Delegat für den CDVViewController in Bezug auf diese Methode zu verwenden, da er die API-Aufrufe durcheinanderbringt.


Alternativ könnte ich auch versuchen, die Mainviewcontroller von der Cordova Anwendung zur Verfügung gestellt zu kopieren und eine Viewcontroller erstellen, die CDVViewController erbt ...

MyViewController.h

#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. 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 

Allerdings bin ich mir nicht sicher, wie man dies entsprechend ändert, um den URL-Befehl entsprechend anzuzapfen. Hat jemand irgendwelche Ideen?

+0

Wenn Sie mit nativem Code aus HTML/Javascript kommunizieren müssen, können Sie diesen Ansatz verwenden, aber Sie müssen sich mit dem nativen Projekt von Cordova herumschlagen und bedingte Blöcke in der WebView-Delegate-Methode erstellen (schlecht). Normalerweise bevorzuge ich ein natives Plugin für diese Szenarien, für mich ist es viel sauberer und einfacher. Sie können nach Beispielen für iOS-Plugins googlen und sich [hier] (https://cordova.apache.org/docs/en/5.1.1/guide/platforms/ios/plugin.html) informieren. –

+0

Vielen Dank für Ihre Antwort! das ist unglaublich hilfreich! Ich bin immer noch ein wenig verwirrt, was die Struktur angeht, die ich für mein eigenes Plugin verwende. Ich habe ein frisches Cordova-Projekt. Würdest du mir sagen können, welche Komponenten ich brauche und wohin sie gehen sollen? Ich bin nicht sicher über Dinge wie 'Verwenden der plugin.xml Datei, um dieses Markup automatisch zu injizieren'. Ich weiß, dass ich die CDVPlugin-Dateien im Ordner xcodeproject plugins benötige, ich kann die Datei config.xml in der ios-Plattform konfigurieren, ich muss nur wissen, wohin das geht. Vielen Dank! – AlecGamble

Antwort

1

Dies ist, was Sie ein iOS-Plugin erstellen müssen:

  • 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.

m
#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/YourPluginName.js

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

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

module.exports = YourPluginName; 

jede Datei in einem Ordner ablegen und dann aus dem Stammordner Ihres cordova Projekttyp:

cordova plugin add pluginfolderpath 

Dann von Ihrem Javascript (Nach dem Ereignis onDeviceReady) können Sie Folgendes tun:

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

das ist ** sehr ** sehr geschätzt, danke! – AlecGamble