cocos2d-x 使用UIWebView加载网页(顺便可以看到如何用OC调C++)

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

FMUIWebViewBridge.mm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
 
//  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];
 
}
 
&nbsp;
 
-(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];
 
&nbsp;
 
// add the webView to the view
 
[mView addSubview:mWebView];
 
[[EAGLView sharedEGLView] addSubview:mView];
 
}
 
&nbsp;
 
- (void)webViewDidStartLoad:(UIWebView *)thisWebView {
 
}
 
&nbsp;
 
- (void)webViewDidFinishLoad:(UIWebView *)thisWebView{
 
[mWebView setUserInteractionEnabled:YES];
 
mLayerWebView->webViewDidFinishLoad();
 
}
 
&nbsp;
 
- (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.
 
&nbsp;
 
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];
 
&nbsp;
 
}
 
}
 
&nbsp;
 
-(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
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
 
//  FMLayerWebView.h
 
//  WebViewDemo
 
//
 
//  Created by Yanghui Liu on 12-6-5.
 
//  Copyright (c) 2012年 FMinutes company. All rights reserved.
 
//
 
&nbsp;
 
#ifndef WebViewDemo_FMLayerWebView_h
 
#define WebViewDemo_FMLayerWebView_h
 
&nbsp;
 
#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;
 
};
 
&nbsp;
 
#endif
<div></div>

FMLayerWebView.mm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
 
//  FMLayerWebView.mm
 
//  WebViewDemo
 
//
 
//  Created by Yanghui Liu on 12-6-5.
 
//  Copyright (c) 2012年 FMinutes company. All rights reserved.
 
//
 
&nbsp;
 
#include "FMLayerWebView.h"
 
#include "FMUIWebViewBridge.h"
 
&nbsp;
 
static FMUIWebViewBridge *g_FMUIWebViewBridge=nil;
 
&nbsp;
 
FMLayerWebView::FMLayerWebView(){
 
}
 
&nbsp;
 
FMLayerWebView::~FMLayerWebView(){
 
[g_FMUIWebViewBridge release];
 
}
 
&nbsp;
 
void FMLayerWebView::webViewDidFinishLoad(){
 
}
 
&nbsp;
 
void FMLayerWebView::onBackbuttonClick(){
 
this->removeFromParentAndCleanup(true);
 
}
 
&nbsp;
 
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
2
3
4
5
FMLayerWebView* web = FMLayerWebView::node();
 
web->setPosition(CCPointZero);
 
addChild(web);
原文地址:https://www.cnblogs.com/willbin/p/3193694.html