IOS下的H5页面的支持
来源:互联网 发布:库克 乔布斯 知乎 编辑:程序博客网 时间:2024/06/06 12:53
如题:这篇博客主要是为自己做笔记,实现的功能是ios的H5支持。
其中不仅实现了H5的支持,还包括了View的跳转操作,由于该H5是用于unity游戏内的,所以需要将H5和控制逻辑一起封装到一个view里面去,
对外置提供打开H5连接的接口。
先看下代码:
1.h文件,只需要定义并实现一个ViewController,并为其添加WebViewDelegate。
//// H5WebView.h//// Created by lining on 16/10/31.// Copyright © 2016年 lining. All rights reserved.//#import <UIKit/UIKit.h>#import <WebKit/WebKit.h>@interface H5WebView:UIViewController<UIWebViewDelegate>-(void) startWebView:(NSString *)strl;-(void) startWebView:(NSString*)str v:(UIView*) view;@end2.m文件,实现view和需要开放的接口
//// H5WebView.m// H5Test//// Created by lining on 16/10/31.// Copyright 漏 2016骞?lining. All rights reserved.//#import "H5WebView.h"#import "UnityAppController.h"@interface H5WebView ()@end@implementation H5WebView-(void) loadUrl:(NSString * )str{ //NSString *str = @"http://www.baidu.com"; [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];}bool canGoBack = false;bool canGoForward = false;UIWebView *webView = NULL;UIButton *btGoBack = NULL;UIButton *btGoForward = NULL;UIButton *btClose = NULL;UIActivityIndicatorView * activityInd = NULL;UIView *mainWebView = NULL;H5WebView * viewCon = NULL;-(void) webViewDidStartLoad:(UIWebView *)webView{ //add wait view UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,75, self.view.frame.size.width, viewCon.view.frame.size.height - 70 )]; [view setTag:108]; [view setBackgroundColor:[UIColor blackColor]]; [view setAlpha:0.5]; [self.view addSubview:view]; activityInd = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)]; [activityInd setCenter:view.center]; [activityInd setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite]; [view addSubview:activityInd]; [activityInd startAnimating]; //do something canGoBack = [webView canGoBack]; canGoForward = [webView canGoForward]; NSLog(@"WebView start load");}-(void) webViewDidFinishLoad:(UIWebView *)webView{ //remove wait view [activityInd stopAnimating]; UIView * view = (UIView *)[self.view viewWithTag:108]; [view removeFromSuperview]; //do something canGoBack = [webView canGoBack]; canGoForward = [webView canGoForward]; NSLog(@"WebView finish load");}-(void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{ //remove wait view [activityInd stopAnimating]; UIView * view = (UIView *)[self.view viewWithTag:108]; [view removeFromSuperview]; NSString *str = @"about:blank"; [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]]; //do something canGoBack = [webView canGoBack]; canGoForward = [webView canGoForward]; NSLog(@"WebView fail load"); NSString * msg = [error description]; NSLog(msg);}- (void)btCloseClick{ if(webView != NULL) { NSLog(@"WebView closed"); [mainWebView removeFromSuperview]; mainWebView = nil; UIViewController * vc = UnityGetGLViewController(); [vc dismissViewControllerAnimated:YES completion:nil]; }}- (void)btBackClick{ if(canGoBack) { NSLog(@"WebView back"); [webView goBack]; }}- (void)btForwardClick{ if(canGoForward) { NSLog(@"WebView forward"); [webView goForward]; } }-(void) initWebView{ //web view btGoBack = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btGoBack.frame = CGRectMake(0, 0, 60,40); btGoBack.center = CGPointMake(40, 50); btGoBack.backgroundColor = [UIColor blackColor]; [btGoBack setAlpha:0.5]; [btGoBack setTitle:@"<" forState:UIControlStateNormal]; [btGoBack addTarget:self action:@selector(btBackClick) forControlEvents:UIControlEventTouchUpInside]; btGoForward = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btGoForward.frame = CGRectMake(0, 0, 60,40); btGoForward.center = CGPointMake(110, 50); btGoForward.backgroundColor = [UIColor blackColor]; [btGoForward setAlpha:0.5]; [btGoForward setTitle:@">" forState:UIControlStateNormal]; [btGoForward addTarget:self action:@selector(btForwardClick) forControlEvents:UIControlEventTouchUpInside]; btClose = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btClose.frame = CGRectMake(0, 0, 60,40); btClose.center = CGPointMake(self.view.frame.size.width - 40, 50); btClose.backgroundColor = [UIColor blackColor]; [btClose setAlpha:0.5]; [btClose setTitle:@"X" forState:UIControlStateNormal]; [btClose addTarget:self action:@selector(btCloseClick) forControlEvents:UIControlEventTouchUpInside]; mainWebView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height)]; [mainWebView addSubview:btClose]; [mainWebView addSubview:btGoBack]; [mainWebView addSubview:btGoForward]; webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,75, self.view.frame.size.width, self.view.frame.size.height - 70 )]; [webView setDelegate:self]; [mainWebView addSubview:webView]; [self.view addSubview:mainWebView]; }-(void) startWebView:(NSString*)str v:(UIView*)view{ //web view btGoBack = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btGoBack.frame = CGRectMake(0, 0, 60,40); btGoBack.center = CGPointMake(40, 50); btGoBack.backgroundColor = [UIColor blackColor]; [btGoBack setAlpha:0.5]; [btGoBack setTitle:@"<" forState:UIControlStateNormal]; [btGoBack addTarget:self action:@selector(btBackClick) forControlEvents:UIControlEventTouchUpInside]; btGoForward = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btGoForward.frame = CGRectMake(0, 0, 60,40); btGoForward.center = CGPointMake(110, 50); btGoForward.backgroundColor = [UIColor blackColor]; [btGoForward setAlpha:0.5]; [btGoForward setTitle:@">" forState:UIControlStateNormal]; [btGoForward addTarget:self action:@selector(btForwardClick) forControlEvents:UIControlEventTouchUpInside]; btClose = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btClose.frame = CGRectMake(0, 0, 60,40); btClose.center = CGPointMake(self.view.frame.size.width - 40, 50); btClose.backgroundColor = [UIColor blackColor]; [btClose setAlpha:0.5]; [btClose setTitle:@"X" forState:UIControlStateNormal]; [btClose addTarget:self action:@selector(btCloseClick) forControlEvents:UIControlEventTouchUpInside]; [mainWebView addSubview:btClose]; [mainWebView addSubview:btGoBack]; [mainWebView addSubview:btGoForward]; webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,75, self.view.frame.size.width, self.view.frame.size.height - 70 )]; [webView setDelegate:self]; [mainWebView addSubview:webView]; [self loadUrl:str]; [view addSubview:mainWebView]; }-(void) startWebView:(NSString *)str{ NSLog(@"BQP: show H5view start here!!!!!!!"); UIViewController * con = UnityGetGLViewController(); NSLog(@"BQP: show H5view use present here!!!!!!!"); //[self initWebView:str]; //viewCon = [[H5WebView alloc] init]; //[con presentViewController:self animated:YES completion:^{[self startWebView:str v:con.view];}]; [con presentViewController:self animated:YES completion:^{[self loadUrl:str];}]; NSLog(@"BQP: end show H5view use present here!!!!!!!"); NSLog(@"BQP: show H5view end here!!!!!!!"); }-(void) didReceiveMemoryWarning{ [super didReceiveMemoryWarning];}-(void) viewDidLoad{ [super viewDidLoad]; [self initWebView]; //[self.view addSubview:mainWebView];}@end
注意:加了底色的代码是为unity专门封装的view跳转方法,如果感觉不太好的话,替换成下面的通用代码:
UIViewController * con = [UIApplication sharedApplication].keyWindow.rootViewController;
0 0
- IOS下的H5页面的支持
- iOS下的app和h5交互
- H5的页面元素
- iOS webView与H5的交互(返回页面的处理)
- iOS webView与H5的交互(返回页面的处理)
- iOS webView与H5的交互(返回页面的处理)
- 在IOS中嵌套H5页面发生空白的问题
- iOS 加载H5页面的时候添加一个菊花
- H5页面在 ios 端滑动不流畅的问题
- H5下自适应页面的meta 与 CSS
- 那些过目不忘的 H5 页面
- 新增的h5页面元素
- 手机h5页面的mata
- 一个基本的H5页面
- H5页面遇到的BUG
- H5增强的页面元素
- h5增强的页面元素
- iOS webView 和 H5 页面交互(点击获取H5页面中的按钮,做自己想做的操作)
- HOOK API入门之Hook
- spark广播变量和累加器
- 欠拟合、过拟合及其解决方法
- 文章标题
- js函数中参数的传递
- IOS下的H5页面的支持
- more命令
- Java基础学习总结(96)——Java虚拟机JVM及Tomcat中的JVM有关内存的设置与调优
- maven+springmvc+mybatis案例---遇到的2个问题:dataSource、方法找不到
- Linux Shell 之 Shell中的函数调用
- Region和Available Zone的区别
- OGRE中OIS与CEGUI的屏幕坐标转换
- Echarts3之字符云WorldCloud
- ThreadLocal的作用与使用