【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 1767|回复: 0

cocos2d-x 使用UIWebView加载网页

[复制链接]
发表于 2013-8-14 14:14:10 | 显示全部楼层 |阅读模式

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

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

实现:FMUIWebViewBridge.h

  1. #import <Foundation/Foundation.h>

  2. #import <CoreLocation/CoreLocation.h>

  3. #import <UIKit/UIKit.h>



  4. #import "FMLayerWebView.h"



  5. @interface FMUIWebViewBridge : NSObject<UIWebViewDelegate,UIAlertViewDelegate>{

  6. FMLayerWebView * mLayerWebView;

  7. UIView    *mView;

  8. UIWebView *mWebView;

  9. UIToolbar *mToolbar;

  10. UIBarButtonItem *mBackButton;

  11. }



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

  13. -(void) backClicked:(id)sender;



  14. @end
复制代码
FMUIWebViewBridge.mm
  1. //

  2. //  FMUIWebViewBridge.cpp

  3. //  WebViewDemo

  4. //

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

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

  7. //

  8. #import "FMUIWebViewBridge.h"

  9. #import "EAGLView.h"

  10. @implementation FMUIWebViewBridge

  11. - (id)init{

  12. self = [super init];

  13. if (self) {

  14. // init code here.

  15. }

  16. return self;

  17. }

  18. - (void)dealloc{

  19. [mBackButton release];

  20. [mToolbar release];

  21. [mWebView release];

  22. [mView release];

  23. [super dealloc];

  24. }



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

  26. mLayerWebView = iLayerWebView;

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

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

  29. // create webView

  30. //Bottom size

  31. int wBottomMargin = size.height*0.10;

  32. int wWebViewHeight = size.height - wBottomMargin;

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

  34. mWebView.delegate = self;

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

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

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

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

  39. mToolbar = [UIToolbar new];

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

  41. mToolbar.barStyle = UIBarStyleBlackOpaque;

  42. //Create a button

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

  44. style: UIBarButtonItemStyleDone

  45. target: self

  46. action:@selector(backClicked:)];

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

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

  49. [mView addSubview:mToolbar];

  50. //[mToolbar release];



  51. // add the webView to the view

  52. [mView addSubview:mWebView];

  53. [[EAGLView sharedEGLView] addSubview:mView];

  54. }



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

  56. }



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

  58. [mWebView setUserInteractionEnabled:YES];

  59. mLayerWebView->webViewDidFinishLoad();

  60. }



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

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



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

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

  65. [alert show];

  66. [alert release];



  67. }

  68. }



  69. -(void) backClicked:(id)sender {

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

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

  72. [mToolbar removeFromSuperview];

  73. [mWebView removeFromSuperview];

  74. [mView removeFromSuperview];

  75. mLayerWebView->onBackbuttonClick();

  76. }

  77. @end
复制代码

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

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

  1. //

  2. //  FMLayerWebView.h

  3. //  WebViewDemo

  4. //

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

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

  7. //



  8. #ifndef WebViewDemo_FMLayerWebView_h

  9. #define WebViewDemo_FMLayerWebView_h



  10. #include "CCCommon.h"

  11. #include "cocos2d.h"

  12. USING_NS_CC;

  13. class FMLayerWebView : public CCLayer{

  14. public:

  15. FMLayerWebView();

  16. ~FMLayerWebView();

  17. virtual bool init();

  18. LAYER_NODE_FUNC(FMLayerWebView);

  19. void webViewDidFinishLoad();

  20. void onBackbuttonClick();

  21. private:

  22. int mWebViewLoadCounter;

  23. };



  24. #endif
  25. <div></div>
复制代码
FMLayerWebView.mm
  1. //

  2. //  FMLayerWebView.mm

  3. //  WebViewDemo

  4. //

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

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

  7. //



  8. #include "FMLayerWebView.h"

  9. #include "FMUIWebViewBridge.h"



  10. static FMUIWebViewBridge *g_FMUIWebViewBridge=nil;



  11. FMLayerWebView::FMLayerWebView(){

  12. }



  13. FMLayerWebView::~FMLayerWebView(){

  14. [g_FMUIWebViewBridge release];

  15. }



  16. void FMLayerWebView::webViewDidFinishLoad(){

  17. }



  18. void FMLayerWebView::onBackbuttonClick(){

  19. this->removeFromParentAndCleanup(true);

  20. }



  21. bool FMLayerWebView::init(){

  22. if ( !CCLayer::init() ){

  23. return false;

  24. }

  25. g_FMUIWebViewBridge = [[FMUIWebViewBridge alloc] init];

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

  27. return true;

  28. }
复制代码

OK ,这样就完成了。

调用方法:

  1. FMLayerWebView* web = FMLayerWebView::node();

  2. web->setPosition(CCPointZero);

  3. addChild(web);
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-11-23 16:39

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表