2013-08-22 38 views
0

我正在使用phonegap。 目前在Android上我能利用我已经做了非常简单的功能,改变页面:使用插件更改当前页面

public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException { 
     MainActivity ma = (MainActivity) cordova.getActivity(); 
     ma.reload(args.getString(0)); 
     return (true); 
    } 

,并在MainActivity:

super.loadUrl("file:///android_asset/www/" + url, 1000); 

但我新的目标C和iOs和我无法找到正确的关键字或答案的开头。 我'能有execute功能相当于后来我不知道怎么去在MainActivity并重新加载页面

直到现在I'n我的插件,我有以下的代码,但它不工作:/

- (void)execute:(CDVInvokedUrlCommand*)command 
{ 
//id message = [command.arguments objectAtIndex:0]; 
id message = @"buy/buy.html";  
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:message]]]; 
} 

编辑:

我已经尽量做到以下几点:

@implementation Redirect 

- (void)execute:(CDVInvokedUrlCommand*)command 
{ 
    id message = [command.arguments objectAtIndex:0]; 

    NSString* newUrl = message; 
    NSString* javaScript = nil; 
    NSString *jsCallBack = [[NSString alloc] initWithFormat:@"changeUrl('%@');", newUrl]; 



    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message]; 
    javaScript = [pluginResult toSuccessCallbackString:jsCallBack]; 
    [self writeJavascript:javaScript]; 
} 
@end 

,并在html:

window.changeUrl = function (url){ 
     alert("ici"); 
     navigator.app.loadUrl(url); 
    } 

var Redirect= function(){ 
    return cordova.exec(function(data){ alert(data);console.log("Success");}, function(){ console.log("Fail");}, 
          "Redirect", 
          "execute", 
          [location.href]); 
}; 

我敢肯定,这个插件被调用的时候,但如果我在changeUrl功能没有把警报显示

回答

2

我已经找到了解决方案,因此在插件:

- (void)execute:(CDVInvokedUrlCommand*)command 
{ 
    id message = [command.arguments objectAtIndex:0]; 
    AppDelegate* mainDelegate =[[UIApplication sharedApplication]delegate]; 
    [mainDelegate reloadUrl:message]; 
} 

在AppDelegate.m

- (void)reloadUrl:(NSString *)url 
{ 
#if __has_feature(objc_arc) 
    self.viewController = [[MainViewController alloc] init]; 
#else 
    self.viewController = [[[MainViewController alloc] init] autorelease]; 
#endif 

    self.viewController.startPage = url; 
    self.window.rootViewController = self.viewController; 
    [self.window reloadInputViews]; 
} 

,我只需调用插件的JS:

return cordova.exec(function(data){ alert(data);console.log("Success");}, function(){ console.log("Fail");}, 
            "Redirect", 
            "execute", 
            ["index2.hmtl"]); 
+0

感谢分享。 – Ross

1

你可以这样做一对夫妇不同的方式(即我所知道的)。确保两种方法的窗口名称空间中都有changeUrl函数。

1方法(这可以从任何地方被触发,并不一定是一个科尔多瓦插件类) -

function changeUrl(url){ 
    navigator.app.loadUrl(url); 
} 

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
NSString* newUrl = @"file:///android_asset/www/buy/buy.html"; 
NSString *jsCallBack = [[NSString alloc] initWithFormat:@"changeUrl('%@');", newUrl]; 
[appDelegate.viewController.webView stringByEvaluatingJavaScriptFromString:jsCallBack]; 

第二个方法(这必须从科尔多瓦插件类,这是什么触发你的当前代码是) -

function changeUrl(url){ 
    navigator.app.loadUrl(url); 
} 

NSString* newUrl = @"file:///android_asset/www/buy/buy.html"; 
NSString* javaScript = nil; 
NSString *jsCallBack = [[NSString alloc] initWithFormat:@"changeUrl('%@');", newUrl]; 
javaScript = [pluginResult toSuccessCallbackString:jsCallBack]; 
[self writeJavascript:javaScript]; 
相关问题