oc 與 js互動之vue.js
- 、vue.js 呼叫oc的方法並傳值
vue.js 元件中呼叫方法:
//
// HDSpecialViewController.h
// headhunter
//
// Created by peter.zhang on 2017/11/20.
// Copyright © 2017年 HunterOn. All rights reserved.
//
#import "WebViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
@interface HDSpecialView:UIView <UIWebViewDelegate>
@property (nonatomic,strong) UIViewController * viewController;
//初始化
- (instancetype)initWithFrame:(CGRect)frame withViewController:(UIViewController *)viewController;
//清空快取
- (void)clearCacheAndCookie;
@end
@protocol specialJavaScriptDelegate <JSExport>
-(void)goToSpecial:(NSString *)specialId type:(NSString *)type;
@end
@interface SpecialJsModel : NSObject <specialJavaScriptDelegate>
@property (nonatomic, weak) JSContext *jsContext;
@property (nonatomic, weak) UIWebView *webView;
@property (nonatomic,strong) UIViewController *vc;
@end
.m檔案
//
// HDSpecialViewController.m
// headhunter
//
// Created by peter.zhang on 2017/11/20.
// Copyright © 2017年 HunterOn. All rights reserved.
//
#import "HDSpecialView.h"
#import "HDHotSearchViewController.h"
@implementation SpecialJsModel
//JS呼叫此方法進入高階專場
-(void)goToSpecial:(NSString *)specialId type:(NSString *)type
{
dispatch_async(dispatch_get_main_queue(), ^{
if (specialId&&![specialId isEqualToString:@""]) {
HDHotSearchViewController * vc = [[HDHotSearchViewController alloc]init];
Adver *adver = [[Adver alloc]init];
adver.pictureId = [specialId longLongValue];
adver.type = [type longLongValue];
vc.adver = adver;
[self.vc.navigationController pushViewController:vc animated:YES];
}
});
}
@end
@interface HDSpecialView ()
@property (strong, nonatomic) UIWebView *webView;
@property (nonatomic, strong) JSContext *jsContext;
@property (strong, nonatomic) NSString *urlString;
@end
@implementation HDSpecialView
- (instancetype)initWithFrame:(CGRect)frame withViewController:(UIViewController *)viewController{
self = [super initWithFrame:frame];
if (self) {
self.viewController = viewController;
self.backgroundColor =[UIColor whiteColor];
self.jsContext = [[JSContext alloc] init];
[self initWebView];
}
return self;
}
-(void)clearCacheAndCookie
{
[self cleanCacheAndCookie];
}
-(void)initWebView
{
NSString *str=nil;
#ifdef __DEBUG
//測試環境
str=@"https://xxxxxxxxxxxxx/special.html#/";
#else
//正式環境
str=@"https://xxxxxxxxxxxxx/special.html#/";
#endif
UIWebView *myWebView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0,kScreen_Width,kScreen_Height-kHeight_NavBar - kHeight_TabBar)];
myWebView.delegate =self;
NSURL *url=[NSURL URLWithString:str];
self.webView=myWebView;
[myWebView loadRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:120]];
myWebView.backgroundColor = kColor_BackGround;
[self addSubview:myWebView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
SpecialJsModel *model = [[SpecialJsModel alloc] init];
self.jsContext[@"mobileObject"] = model;
model.jsContext = self.jsContext;
model.webView = self.webView;
model.vc = self.viewController;
self.jsContext.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
context.exception = exceptionValue;
NSLog(@"異常資訊:%@", exceptionValue);
};
[MBProgressHUD hideHUDInView:self.viewController.view];
//去除長按後出現的文字選取框
[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect='none';"];
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
_urlString=request.URL.absoluteString;
return YES;
}
-(void)setUrlString:(NSString *)urlString
{
_urlString =urlString;
}
/**清除快取和cookie*/
- (void)cleanCacheAndCookie
{
[self.webView stringByEvaluatingJavaScriptFromString:@"localStorage.clear();"];
//清除cookies
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
[storage deleteCookie:cookie];
}
//清除UIWebView的快取
NSURLCache * cache = [NSURLCache sharedURLCache];
[cache removeAllCachedResponses];
[cache setDiskCapacity:0];
[cache setMemoryCapacity:0];
}
@end
二、oc呼叫vue.js 元件的方法
在webViewDidFinishLoad 代理方法中呼叫,因為這個時候vue的所有的元件節點都已經渲染
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
self.jsContext = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//該方法是vue中元件的方法
[self call];
}
- (void)call{
// 之後在回撥js的方法Callback把內容傳出去
JSValue *Callback = self.jsContext[@"mobileChangeContent"];
//傳值給web端
[Callback callWithArguments:@[@"4"]];
}
vue中的實現mobileChangeContent(引數)的方法
1、注意我們都知道呼叫vue的方法的時候要掛載到window上的方法
隨意在元件中需要特殊處理,讓元件的方法掛載到window的mobileChangeContent方法上
元件A
相關文章
- oc 與js 的原生互動JS
- OC WKWebView的JS與OC互動、Cookie管理WebViewJSCookie
- JS 與 OC 互動的三種方法JS
- Cordova JS OC互動方法JS
- WebViewJavascriptBridge JS和OC互動WebViewJavaScriptJS
- iOS開發:網頁JS與OC互動(JavaScriptCore)iOS網頁JSJavaScript
- HTML5接入與OC互動HTML
- iOS與JS互動之UIWebView-JavaScriptCore框架iOSJSUIWebViewJavaScript框架
- swift3.0與OC的互動注意事項Swift
- OC與C++ 混編的檔案互動C++
- js 與WKWebView 互動JSWebView
- WKWebView與JS互動WebViewJS
- iOS與JS互動之UIWebView協議攔截iOSJSUIWebView協議
- webview與JS的互動WebViewJS
- Vue.js 動態元件與非同步元件Vue.js元件非同步
- Android webview 與 js(Vue) 互動AndroidWebViewJSVue
- iOS 與 JS 互動手冊 - JavaScriptCoreiOSJSJavaScript
- iOS 與JS Html常見互動iOSJSHTML
- angularjs與伺服器互動AngularJS伺服器
- H5 與 Native 互動之 JSBridge 技術H5JS
- Vue和Node.js互動之tokenVueNode.js
- WKWebView與Js實戰(OC版)WebViewJS
- 原生App與javascript互動之JSBridge介面原理、設計與實現APPJavaScriptJS
- Flutter使用JsBridge與WebView互動FlutterJSWebView
- Flutter WebView與JS互動簡易指南FlutterWebViewJS
- WKWebView和WebView與JS的互動方式WebViewJS
- ios/oc中的結構體 與字串互轉iOS結構體字串
- JS原生互動JS
- Vue.js 移動端適配之 vw 解決方案Vue.js
- Vue.js 動態元件使用Vue.js元件
- vue.js無縫滾動Vue.js
- 【18】vue.js — 動態元件Vue.js元件
- Android混合開發之WebViewJavascriptBridge實現JS與java安全互動AndroidWebViewJavaScriptJS
- 2017-12-04(週一)JS和OC互調JS
- WebView與JS的互動,以及注意事項WebViewJS
- WPF和js互動 WebBrowser資料互動JSWeb
- 開心檔之Vue.js 路由Vue.js路由
- Vue.js 筆記之 img srcVue.js筆記