猴子原创,欢迎转载。转载请注明: 转载自Cocos2D开发网--Cocos2Dev.com,谢谢!
前段时间项目中要微博授权登陆,使用的是web登陆方式。所以要在cocos2d-x中显示网页。所以就将UIWebView用进来了。
实现上讲也不是很难,你开打AppController.mm,你可以看到cocos2d-x的场景是被加载在一个EAGLView上面。所以我只需要取到EAGLView,在上面加一个UIView,UIView上面就可以放我的UIWebView。
实现:FMUIWebViewBridge.h
- #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
#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
- //
- // 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
//
// 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
- //
- // 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.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
- //
- // 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;
- }
//
// 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 ,这样就完成了。
调用方法:
- FMLayerWebView* web = FMLayerWebView::node();
- web->setPosition(CCPointZero);
- addChild(web);
FMLayerWebView* web = FMLayerWebView::node();
web->setPosition(CCPointZero);
addChild(web);