一、自定义的html简单代码
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<script language="javascript">
<!-- callFullScreen_H5();-->
<!-- function callFullScreen_H5 (){-->
<!-- alert(111);-->
<!-- callFullScreen();-->
<!-- alert(222);-->
<!-- }-->
function locationClick0() {
callScanCode();
}
function locationClick1() {
JavacallUserInfo();
}
function locationClick2() {
callCutCrossScreen();
}
function locationClick3() {
callCutVerticalScreen();
}
function locationClick4() {
callFullScreen();
}
function locationClick5() {
callCommonScreen();
}
function locationClick6() {
callTelePhoneScreen(10086);
}
function callBackScanCode(location) {
asyncAlert(location);
document.getElementById("returnValue").value = location;
}
function ShowUserInfoFromAndroid(location) {
asyncAlert(location);
document.getElementById("returnValue").value = location;
}
function asyncAlert(content) {
setTimeout(function(){
alert(content);
},1);
}
</script>
</head>
<body>
<h1>这是按钮调用</h1>
<input type="button" value="扫码" onclick="locationClick0()" />
<input type="button" value="用户数据" onclick="locationClick1()" />
<input type="button" value="设备横屏" onclick="locationClick2()" />
<input type="button" value="设备竖屏" onclick="locationClick3()" />
<input type="button" value="隐藏导航栏" onclick="locationClick4()" />
<input type="button" value="显示导航栏" onclick="locationClick5()" />
<input type="button" value="拨打电话" onclick="locationClick6()" />
<textarea id ="returnValue" type="value" rows="5" cols="50">
</textarea>
</body>
</html>
二、oc代码实现
1、导入#import <MessageUI/MessageUI.h> 和#import <JavaScriptCore/JavaScriptCore.h>框架,添加UIWebViewDelegate,MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate代理方法
2、加载html文件
NSURLCache * cache = [NSURLCache sharedURLCache];
[cache removeAllCachedResponses];
[cache setDiskCapacity:0];
self.currentWebView = [[UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:self.currentWebView];
self.currentWebView.delegate = self;
NSURL *htmlURL = [[NSBundle mainBundle] URLForResource:@"index.html" withExtension:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:htmlURL];
[self.currentWebView loadRequest:request];
3、方法实现
#pragma - mark WebViewDelegate 必须都实现,否则会有警告
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
return true;
}
//这个代理必须实现,否则将无法注入JS
- (void)webViewDidStartLoad:(UIWebView *)webView{
[self addSreenChangeActions];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[self addCustomActions];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[self addCustomActions];
}
#pragma mark - private method
- (void)addSreenChangeActions{
JSContext *context = [self.currentWebView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
[self callCutCrossScreenWithContext:context];
[self callCutVerticalScreenWithContext:context];
[self callFullScreenWithContext:context];
[self callCommonScreenWithContext:context];
[self callcallTelePhoneWithContext:context];
}
- (void)addCustomActions{
JSContext *context = [self.currentWebView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
[self callScanCodeWithContext:context];
[self callUserInfoWithContext:context];
}
- (void)callUserInfoWithContext:(JSContext *)context{
__WEAK
context[@"JavacallUserInfo"] = ^() {
//返回用户数据
// NSArray *arr=[[NSUserDefaults standardUserDefaults]objectForKey:@"infomationArr"];
// NSDictionary *dict = @{@"uid":arr[0],@"nickName":arr[1],@"email":arr[2],@"loginName":arr[3]};
NSDictionary *dict = @{@"uid":@"uid",@"nickName":@"uid",@"email":@"uid",@"loginName":@"uid"};
[ws userInfoToHTMLWithUserDict:dict];
};
}
- (void)userInfoToHTMLWithUserDict:(NSDictionary *)userDic{
JSContext *context = [self.currentWebView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userDic options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonStrr=[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *jsStr = [NSString stringWithFormat:@"ShowUserInfoFromAndroid(%@)",jsonStrr];
[context evaluateScript:jsStr];
}
- (void)callScanCodeWithContext:(JSContext *)context{
__WEAK
context[@"callScanCode"] = ^() {
[ws scanResultStr:@"扫码数据"];
};
}
- (void)callCutCrossScreenWithContext:(JSContext *)context{
__WEAK
context[@"callCutCrossScreen"] = ^() {
//切换横屏
dispatch_async(dispatch_get_main_queue(), ^{
[ws interfaceOrientation:UIInterfaceOrientationLandscapeRight];
self.currentWebView.frame = self.view.bounds;
});
};
}
- (void)callCutVerticalScreenWithContext:(JSContext *)context{
__WEAK
context[@"callCutVerticalScreen"] = ^() {
//切换竖屏
dispatch_async(dispatch_get_main_queue(), ^{
[ws interfaceOrientation:UIInterfaceOrientationPortrait];
self.currentWebView.frame = self.view.bounds;
});
};
}
- (void)callFullScreenWithContext:(JSContext *)context{
__WEAK
context[@"callFullScreen"] = ^() {
//满屏隐藏状态栏和头部
dispatch_async(dispatch_get_main_queue(), ^{
self.isShowStaus = YES;
[ws prefersStatusBarHidden];
[ws.navigationController setNavigationBarHidden:YES animated:YES];
self.currentWebView.frame = self.view.bounds;
});
};
}
- (void)callCommonScreenWithContext:(JSContext *)context{
__WEAK
context[@"callCommonScreen"] = ^() {
//恢复显示状态栏和头部
dispatch_async(dispatch_get_main_queue(), ^{
self.isShowStaus = NO;
[ws prefersStatusBarHidden];
[ws.navigationController setNavigationBarHidden:NO animated:YES];
self.currentWebView.frame = self.view.bounds;
});
};
}
- (void)callcallTelePhoneWithContext:(JSContext *)context{
__WEAK
context[@"callTelePhoneScreen"] = ^(NSString *phoneStr) {
//恢复显示状态栏和头部
dispatch_async(dispatch_get_main_queue(), ^{
self.isShowStaus = NO;
[ws prefersStatusBarHidden];
[ws.navigationController setNavigationBarHidden:NO animated:YES];
self.currentWebView.frame = self.view.bounds;
});
};
}
- (void)scanResultStr:(NSString *)resultStr{
JSContext *context = [self.currentWebView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
NSString *jsStr = [NSString stringWithFormat:@"callBackScanCode('%@')",resultStr];
[context evaluateScript:jsStr];
}
- (BOOL)prefersStatusBarHidden {
return self.isShowStaus;
}
/**
* 横竖屏切换接口
*
* @param orientation 切换方向
*/
- (void)interfaceOrientation:(UIInterfaceOrientation)orientation{
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = orientation;
// 从2开始是因为0 1 两个参数已经被selector和target占用
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
- (BOOL)shouldAutorotate{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
}
4、效果图
邮箱、短信、电话请跳转http://www.jianshu.com/p/8cf6396f6934查看