ios原生方法和h5相互調用的實現

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調用原生方法還沒來得及研究

發佈了31 篇原創文章 · 獲贊 9 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章