cocos2d-x 使用UIWebView加载网页

猴子原创,欢迎转载。转载请注明: 转载自Cocos2D开发网--Cocos2Dev.com,谢谢!

原文地址: http://www.cocos2dev.com/?p=248

前段时间项目中要微博授权登陆,使用的是web登陆方式。所以要在cocos2d-x中显示网页。所以就将UIWebView用进来了。

实现上讲也不是很难,你开打AppController.mm,你可以看到cocos2d-x的场景是被加载在一个EAGLView上面。所以我只需要取到EAGLView,在上面加一个UIView,UIView上面就可以放我的UIWebView。

实现:FMUIWebViewBridge.h

  1. #import <Foundation/Foundation.h>   
  2.   
  3. #import <CoreLocation/CoreLocation.h>   
  4.   
  5. #import <UIKit/UIKit.h>   
  6.   
  7.    
  8.   
  9. #import "FMLayerWebView.h"   
  10.   
  11.    
  12.   
  13. @interface FMUIWebViewBridge : NSObject<UIWebViewDelegate,UIAlertViewDelegate>{  
  14.   
  15. FMLayerWebView * mLayerWebView;  
  16.   
  17. UIView    *mView;  
  18.   
  19. UIWebView *mWebView;  
  20.   
  21. UIToolbar *mToolbar;  
  22.   
  23. UIBarButtonItem *mBackButton;  
  24.   
  25. }  
  26.   
  27.    
  28.   
  29. -(void) setLayerWebView : (FMLayerWebView*) iLayerWebView URLString:(const char*) urlString;  
  30.   
  31. -(void) backClicked:(id)sender;  
  32.   
  33.    
  34.   
  35. @end  
#import <Foundation/Foundation.h>

#import <CoreLocation/CoreLocation.h>

#import <UIKit/UIKit.h>

 

#import "FMLayerWebView.h"

 

@interface FMUIWebViewBridge : NSObject<UIWebViewDelegate,UIAlertViewDelegate>{

FMLayerWebView * mLayerWebView;

UIView    *mView;

UIWebView *mWebView;

UIToolbar *mToolbar;

UIBarButtonItem *mBackButton;

}

 

-(void) setLayerWebView : (FMLayerWebView*) iLayerWebView URLString:(const char*) urlString;

-(void) backClicked:(id)sender;

 

@end

FMUIWebViewBridge.mm

  1. //   
  2.   
  3. //  FMUIWebViewBridge.cpp   
  4.   
  5. //  WebViewDemo   
  6.   
  7. //   
  8.   
  9. //  Created by Yanghui Liu on 12-6-5.   
  10.   
  11. //  Copyright (c) 2012年 FMinutes company. All rights reserved.   
  12.   
  13. //   
  14.   
  15. #import "FMUIWebViewBridge.h"   
  16.   
  17. #import "EAGLView.h"   
  18.   
  19. @implementation FMUIWebViewBridge  
  20.   
  21. - (id)init{  
  22.   
  23. self = [super init];  
  24.   
  25. if (self) {  
  26.   
  27. // init code here.   
  28.   
  29. }  
  30.   
  31. return self;  
  32.   
  33. }  
  34.   
  35. - (void)dealloc{  
  36.   
  37. [mBackButton release];  
  38.   
  39. [mToolbar release];  
  40.   
  41. [mWebView release];  
  42.   
  43. [mView release];  
  44.   
  45. [super dealloc];  
  46.   
  47. }  
  48.   
  49.    
  50.   
  51. -(void) setLayerWebView : (FMLayerWebView*) iLayerWebView URLString:(const char*) urlString{  
  52.   
  53. mLayerWebView = iLayerWebView;  
  54.   
  55. cocos2d::CCSize size = mLayerWebView-> getContentSize();  
  56.   
  57. mView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width , size.height)];  
  58.   
  59. // create webView   
  60.   
  61. //Bottom size   
  62.   
  63. int wBottomMargin = size.height*0.10;  
  64.   
  65. int wWebViewHeight = size.height - wBottomMargin;  
  66.   
  67. mWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, size.width, wWebViewHeight)];  
  68.   
  69. mWebView.delegate = self;  
  70.   
  71. NSString *urlBase = [NSString stringWithCString:urlString encoding:NSUTF8StringEncoding];  
  72.   
  73. [mWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlBase ]]];  
  74.   
  75. [mWebView setUserInteractionEnabled:NO]; //don't let the user scroll while things are   
  76.   
  77. //create a tool bar for the bottom of the screen to hold the back button   
  78.   
  79. mToolbar = [UIToolbar new];  
  80.   
  81. [mToolbar setFrame:CGRectMake(0, wWebViewHeight, size.width, wBottomMargin)];  
  82.   
  83. mToolbar.barStyle = UIBarStyleBlackOpaque;  
  84.   
  85. //Create a button   
  86.   
  87. mBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"  
  88.   
  89. style: UIBarButtonItemStyleDone  
  90.   
  91. target: self  
  92.   
  93. action:@selector(backClicked:)];  
  94.   
  95. //[backButton setBounds:CGRectMake(0.0, 0.0, 95.0, 34.0)];   
  96.   
  97. [mToolbar setItems:[NSArray arrayWithObjects:mBackButton,nil] animated:YES];  
  98.   
  99. [mView addSubview:mToolbar];  
  100.   
  101. //[mToolbar release];   
  102.   
  103.    
  104.   
  105. // add the webView to the view   
  106.   
  107. [mView addSubview:mWebView];  
  108.   
  109. [[EAGLView sharedEGLView] addSubview:mView];  
  110.   
  111. }  
  112.   
  113.    
  114.   
  115. - (void)webViewDidStartLoad:(UIWebView *)thisWebView {  
  116.   
  117. }  
  118.   
  119.    
  120.   
  121. - (void)webViewDidFinishLoad:(UIWebView *)thisWebView{  
  122.   
  123. [mWebView setUserInteractionEnabled:YES];  
  124.   
  125. mLayerWebView->webViewDidFinishLoad();  
  126.   
  127. }  
  128.   
  129.    
  130.   
  131. - (void)webView:(UIWebView *)thisWebView didFailLoadWithError:(NSError *)error {  
  132.   
  133. if ([error code] != -999 && error != NULL) { //error -999 happens when the user clicks on something before it's done loading.   
  134.   
  135.    
  136.   
  137. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Error" message:@"Unable to load the page. Please keep network connection."  
  138.   
  139. delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];  
  140.   
  141. [alert show];  
  142.   
  143. [alert release];  
  144.   
  145.    
  146.   
  147. }  
  148.   
  149. }  
  150.   
  151.    
  152.   
  153. -(void) backClicked:(id)sender {  
  154.   
  155. mWebView.delegate = nil; //keep the webview from firing off any extra messages   
  156.   
  157. //remove items from the Superview...just to make sure they're gone   
  158.   
  159. [mToolbar removeFromSuperview];  
  160.   
  161. [mWebView removeFromSuperview];  
  162.   
  163. [mView removeFromSuperview];  
  164.   
  165. mLayerWebView->onBackbuttonClick();  
  166.   
  167. }  
  168.   
  169. @end  
//

//  FMUIWebViewBridge.cpp

//  WebViewDemo

//

//  Created by Yanghui Liu on 12-6-5.

//  Copyright (c) 2012年 FMinutes company. All rights reserved.

//

#import "FMUIWebViewBridge.h"

#import "EAGLView.h"

@implementation FMUIWebViewBridge

- (id)init{

self = [super init];

if (self) {

// init code here.

}

return self;

}

- (void)dealloc{

[mBackButton release];

[mToolbar release];

[mWebView release];

[mView release];

[super dealloc];

}

 

-(void) setLayerWebView : (FMLayerWebView*) iLayerWebView URLString:(const char*) urlString{

mLayerWebView = iLayerWebView;

cocos2d::CCSize size = mLayerWebView-> getContentSize();

mView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width , size.height)];

// create webView

//Bottom size

int wBottomMargin = size.height*0.10;

int wWebViewHeight = size.height - wBottomMargin;

mWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, size.width, wWebViewHeight)];

mWebView.delegate = self;

NSString *urlBase = [NSString stringWithCString:urlString encoding:NSUTF8StringEncoding];

[mWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlBase ]]];

[mWebView setUserInteractionEnabled:NO]; //don't let the user scroll while things are

//create a tool bar for the bottom of the screen to hold the back button

mToolbar = [UIToolbar new];

[mToolbar setFrame:CGRectMake(0, wWebViewHeight, size.width, wBottomMargin)];

mToolbar.barStyle = UIBarStyleBlackOpaque;

//Create a button

mBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"

style: UIBarButtonItemStyleDone

target: self

action:@selector(backClicked:)];

//[backButton setBounds:CGRectMake(0.0, 0.0, 95.0, 34.0)];

[mToolbar setItems:[NSArray arrayWithObjects:mBackButton,nil] animated:YES];

[mView addSubview:mToolbar];

//[mToolbar release];

 

// add the webView to the view

[mView addSubview:mWebView];

[[EAGLView sharedEGLView] addSubview:mView];

}

 

- (void)webViewDidStartLoad:(UIWebView *)thisWebView {

}

 

- (void)webViewDidFinishLoad:(UIWebView *)thisWebView{

[mWebView setUserInteractionEnabled:YES];

mLayerWebView->webViewDidFinishLoad();

}

 

- (void)webView:(UIWebView *)thisWebView didFailLoadWithError:(NSError *)error {

if ([error code] != -999 && error != NULL) { //error -999 happens when the user clicks on something before it's done loading.

 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Error" message:@"Unable to load the page. Please keep network connection."

delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];

[alert show];

[alert release];

 

}

}

 

-(void) backClicked:(id)sender {

mWebView.delegate = nil; //keep the webview from firing off any extra messages

//remove items from the Superview...just to make sure they're gone

[mToolbar removeFromSuperview];

[mWebView removeFromSuperview];

[mView removeFromSuperview];

mLayerWebView->onBackbuttonClick();

}

@end

因为这些都是OC代码,所以文件后缀使用mm,为了和C++代码混编。代码很简答,应该看得懂,我就不解释了。

下面就是封装这个oc代码,

FMLayerWebView.h

  1. //   
  2.   
  3. //  FMLayerWebView.h   
  4.   
  5. //  WebViewDemo   
  6.   
  7. //   
  8.   
  9. //  Created by Yanghui Liu on 12-6-5.   
  10.   
  11. //  Copyright (c) 2012年 FMinutes company. All rights reserved.   
  12.   
  13. //   
  14.   
  15.    
  16.   
  17. #ifndef WebViewDemo_FMLayerWebView_h   
  18.   
  19. #define WebViewDemo_FMLayerWebView_h   
  20.   
  21.    
  22.   
  23. #include "CCCommon.h"   
  24.   
  25. #include "cocos2d.h"   
  26.   
  27. USING_NS_CC;  
  28.   
  29. class FMLayerWebView : public CCLayer{  
  30.   
  31. public:  
  32.   
  33. FMLayerWebView();  
  34.   
  35. ~FMLayerWebView();  
  36.   
  37. virtual bool init();  
  38.   
  39. LAYER_NODE_FUNC(FMLayerWebView);  
  40.   
  41. void webViewDidFinishLoad();  
  42.   
  43. void onBackbuttonClick();  
  44.   
  45. private:  
  46.   
  47. int mWebViewLoadCounter;  
  48.   
  49. };  
  50.   
  51.    
  52.   
  53. #endif  
//

//  FMLayerWebView.h

//  WebViewDemo

//

//  Created by Yanghui Liu on 12-6-5.

//  Copyright (c) 2012年 FMinutes company. All rights reserved.

//

 

#ifndef WebViewDemo_FMLayerWebView_h

#define WebViewDemo_FMLayerWebView_h

 

#include "CCCommon.h"

#include "cocos2d.h"

USING_NS_CC;

class FMLayerWebView : public CCLayer{

public:

FMLayerWebView();

~FMLayerWebView();

virtual bool init();

LAYER_NODE_FUNC(FMLayerWebView);

void webViewDidFinishLoad();

void onBackbuttonClick();

private:

int mWebViewLoadCounter;

};

 

#endif

FMLayerWebView.mm

  1. //   
  2.   
  3. //  FMLayerWebView.mm   
  4.   
  5. //  WebViewDemo   
  6.   
  7. //   
  8.   
  9. //  Created by Yanghui Liu on 12-6-5.   
  10.   
  11. //  Copyright (c) 2012年 FMinutes company. All rights reserved.   
  12.   
  13. //   
  14.   
  15.    
  16.   
  17. #include "FMLayerWebView.h"   
  18.   
  19. #include "FMUIWebViewBridge.h"   
  20.   
  21.    
  22.   
  23. static FMUIWebViewBridge *g_FMUIWebViewBridge=nil;  
  24.   
  25.    
  26.   
  27. FMLayerWebView::FMLayerWebView(){  
  28.   
  29. }  
  30.   
  31.    
  32.   
  33. FMLayerWebView::~FMLayerWebView(){  
  34.   
  35. [g_FMUIWebViewBridge release];  
  36.   
  37. }  
  38.   
  39.    
  40.   
  41. void FMLayerWebView::webViewDidFinishLoad(){  
  42.   
  43. }  
  44.   
  45.    
  46.   
  47. void FMLayerWebView::onBackbuttonClick(){  
  48.   
  49. this->removeFromParentAndCleanup(true);  
  50.   
  51. }  
  52.   
  53.    
  54.   
  55. bool FMLayerWebView::init(){  
  56.   
  57. if ( !CCLayer::init() ){  
  58.   
  59. return false;  
  60.   
  61. }  
  62.   
  63. g_FMUIWebViewBridge = [[FMUIWebViewBridge alloc] init];  
  64.   
  65. [g_FMUIWebViewBridge setLayerWebView : this URLString:"http://www.cocos2dev.com"];  
  66.   
  67. return true;  
  68.   
  69. }  
//

//  FMLayerWebView.mm

//  WebViewDemo

//

//  Created by Yanghui Liu on 12-6-5.

//  Copyright (c) 2012年 FMinutes company. All rights reserved.

//

 

#include "FMLayerWebView.h"

#include "FMUIWebViewBridge.h"

 

static FMUIWebViewBridge *g_FMUIWebViewBridge=nil;

 

FMLayerWebView::FMLayerWebView(){

}

 

FMLayerWebView::~FMLayerWebView(){

[g_FMUIWebViewBridge release];

}

 

void FMLayerWebView::webViewDidFinishLoad(){

}

 

void FMLayerWebView::onBackbuttonClick(){

this->removeFromParentAndCleanup(true);

}

 

bool FMLayerWebView::init(){

if ( !CCLayer::init() ){

return false;

}

g_FMUIWebViewBridge = [[FMUIWebViewBridge alloc] init];

[g_FMUIWebViewBridge setLayerWebView : this URLString:"http://www.cocos2dev.com"];

return true;

}

OK ,这样就完成了。

调用方法:

  1. FMLayerWebView* web = FMLayerWebView::node();  
  2.   
  3. web->setPosition(CCPointZero);  
  4.   
  5. addChild(web);  
FMLayerWebView* web = FMLayerWebView::node();

web->setPosition(CCPointZero);

addChild(web);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值