2013-08-22 50 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]; 
相關問題