1、 h5頁面的<html>外面嵌入js實現原生調用js方法並 傳遞內容值
<script type=”text/javascript”>
FunctiongetMessageFromApp(message){
Var testdiv=doucument.getElementById(“testDiv”);
Testdiv.innertText=message;
}
</script>
這樣原生webview加載完成後調用這個方法就對<div>的內容進行賦值原生中的方法如下[self.webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@”這裏寫h5界面中的方法名字”,@”這裏寫要傳遞的內容”]]
2、 js方法調用原生方法實現重啓另一個app
在h5方法中定義一個按鈕
<inputtype=”button ”value=”啓動app” οnclick=”sendCommand(‘alert’,’要傳遞過去的值’);”/>
在原生中實現如下方法:
-(BOOL)webView(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequst *)request navigationType:(UIWebViewNavigationtype)navigationType{
NSString*requesturl=[[request URL] absolutString];//獲取請求的絕對路徑
NSArray*components=[requstString componentsSeparatedByString:@”:”];//分開參數的符號
If([componentscount]>1){
If([(NSString*)[components objectAtIndex:1] isEqualToString:@”alert”]){
NSString*message=(NSString *)[components objectAtIndex:2];//獲取內容值
UIAlertView*alert=[[UIAlertView alloc] initWithTitle:@”js向app提交的數據” message:message delegate:self cancelButtontitle:@”取消” otherButtonTitles:@”確定”,nil];
[alert show];
}
Return NO; }
Return YES;
}
//##################js調用原生方法還沒來得及研究