ios端,H5調oc,oc調H5(轉的別人的,僅供參考、筆記。已實踐)

原文:https://www.jianshu.com/p/a5183d8d4a65javascript

H5:<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> </head> <body> <div style="margin-top: 100px"> <h1>Objective-C和JavaScript交互</h1> </div> <div> <input type="button" value="oc掉用js彈出提示->" onclick="clickAction0(11)"> <input type="button" value="js調用oc->" onclick="clickbtn()"> </div> <script type="text/javascript"> // js函數 function clickAction0(typyId) { alert(typyId) } function clickAction1(typyId) { alert(typyId) } function clickAction2(typyId) { alert(typyId); return 'hello'; } function clickbtn() { var tempValue = window.AndroidWebView.indexOfMap(); alert(tempValue); } </script> </body> </html>html

 
  • 導入依賴庫
    JavaScriptCore.framework
  • #import <UIKit/UIKit.h>
  •  #import <JavaScriptCore/JavaScriptCore.h> 
  • @protocol JSObjcDelegate <JSExport> // AndroidWebView對象調用的JavaScript方法,必須聲明!!!
  •  - (int)indexOfMap; 
  •  @end
  •  @interface ViewController : UIViewController <UIWebViewDelegate,JSObjcDelegate> @property (nonatomic, strong) JSContext *context;
  •  @property (weak, nonatomic) IBOutlet UIWebView *webView;
  •  @end
  • 加載網頁(此處本地)
  • - (void)viewDidLoad {
  •  [super viewDidLoad]; 
  •  self.webView.delegate = self; 
  •  self.webView.scalesPageToFit = YES;//自動對頁面進行縮放以適應屏幕 // 加載本地的html測試js 
  •  NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; NSString *path = [[NSBundle mainBundle] pathForResource:@"testJsFunc" ofType:@"html"]; 
  •  NSString *html = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  •  [self.webView loadHTMLString:html baseURL:baseURL]; // Do any additional setup after loading the view, typically from a nib.
  •  }
  • 實現交互
  • - (void)webViewDidFinishLoad:(UIWebView *)webView { 
  •  // 獲取context對象
  •  self.context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; //將AndroidWebView對象指向自身 js裏面寫window.AndroidWebView.indexOfMap() 就會調用原生裏的indexOfMap方法
  • self.context[@"AndroidWebView"] = self;
  •  self.context.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) { context.exception = exceptionValue; 
  •  NSLog(@"異常信息:%@", exceptionValue); }; 
  •  // 獲取到點擊js按鈕的事件 self.context[@"clickAction0"] = ^(){ 
  •  NSLog(@"獲取到點擊js按鈕的事件"); };
  •  // oc調用js函數 並傳參 js無返回值
  •  NSString *jsAction = @"clickAction1(555)";
  •  [self.context evaluateScript:jsAction]; 
  •  // oc調用js函數 並傳參 接收js返回值
  •  NSString *str1 = [webView stringByEvaluatingJavaScriptFromString:@"clickAction2(666);"];
  •  NSLog(@"js函數給個人返回值:%@", str1); } /** 待js調用 */ 
  • - (int)indexOfMap {
  •  NSLog(@"我被js調用了");
  •  return 110; 
  • }

關於怎麼接受JS 傳過來的參數,事件已經捕獲到了java

 // 獲取到點擊js按鈕的事件
self.jsContext[@"clickAction"] = ^(){
[DLHelper showHud:@""];
NSArray *args = [JSContext currentArguments];
for (id obj in args) {
//JS 傳過來的參數
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",obj]];
UIImageView *tempIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0.001, 0.001)];
[ws.view addSubview:tempIV];
[tempIV sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image) {
// 保存圖片
[tempIV removeFromSuperview];

UIImageWriteToSavedPhotosAlbum(image, ws, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}else{
[DLHelper toast:Localized(@"En_SaveFail")];

}
}];
}
web

相關文章
相關標籤/搜索