IOS下的H5頁面的支持

如題:這篇博客主要是爲自己做筆記,實現的功能是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;



發佈了31 篇原創文章 · 獲贊 15 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章