如题:这篇博客主要是为自己做笔记,实现的功能是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;
@end
2.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;