How to add an UIWebView to Cocos2d-x?

Hello
I found a code that use UIWebView in Cocos2D by Bryan Duke at (http://blog.acceleroto.com/2009/08/04/occurro-game-design-101-with-cocos2d-iphone/)
and I want to convert it into Cocos2d-x.

.h file :

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import “cocos2d.h”

`interface LayerWebView : Layer <UIWebViewDelegate, UIAlertViewDelegate>
{
Menu * menu;

Label *label;
UIView *view;
UIWebView *webView;
UIToolbar *toolbar;
UIBarButtonItem *backButton;
int webViewLoadCounter;

}
`property(nonatomic, retain, readonly) UIToolbar* toolbar;

~~getScores;
~~(void) backClicked:(id)sender;
@end

Can any one suggest me how to convert it ? Can I just do something similar to the CCEGLView_ios.h and .mm ?

Thanks
Dac

OK,
I got the UIView that contains the UIWebView displays inside Cocos2D-X scene.
Now I am looking at the back button for swapping the UIWebView scene with the Game scene. I will post the solution,once I finish.

Hello
Here are the files.
Dac

Thanks a lot for sharing, that is very useful to me.
I just found a small problem with orientation, my design is in landscape mode and the webview is displayed in portrait. I’ll try to find why, and after that I must find how to do the same on Android :slight_smile:
Thanks again.

EDIT: forget about orientation problem, that was a bug on my side.

For info on Android you have to play with JNI :

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    JniMethodInfo methodInfo;
    if (!JniHelper::getStaticMethodInfo(methodInfo, "YOUR_PACKAGE", "DisplayWebView", "()V"))
    {
        // error handling
    }
    methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
    methodInfo.env->DeleteLocalRef(methodInfo.classID);
#endif

and in your java code :

public static void DisplayWebView() {
        _activity.runOnUiThread(new Runnable() {
            public void run() {
                _webView.loadUrl("http://www.google.com");
            }
        });
    }

thank you for what you are sharing!

I tried it today and got lots of NSObjRuntime error. I am using latest cocos2d-2.1beta3-x-2.1.0 . Here are what I did:

  1. Create a new Cocos2dx project with HelloWorldScene. 2) add four files above. 3) Build.

Thanks for any advices in advance.

Dac Toan Ho wrote:

Hello
I found a code that use UIWebView in Cocos2D by Bryan Duke at (http://blog.acceleroto.com/2009/08/04/occurro-game-design-101-with-cocos2d-iphone/)
and I want to convert it into Cocos2d-x.
>
.h file :
>
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import “cocos2d.h”
>
`interface LayerWebView : Layer <UIWebViewDelegate, UIAlertViewDelegate>
{
Menu * menu;

Label *label;
UIView *view;
UIWebView *webView;
UIToolbar *toolbar;
UIBarButtonItem *backButton;
int webViewLoadCounter;
}
`property(nonatomic, retain, readonly) UIToolbar* toolbar;
>
getScores;
>
(void) backClicked:(id)sender;
@end
>
Can any one suggest me how to convert it ? Can I just do something similar to the CCEGLView_ios.h and .mm ?
>
Thanks
Dac

web ui

zhang mouz: what do you mean “web ui” ?

i did this like this

void settings::showFaceBook(){
    [[settingshelper instance]FacebookUrl];
    UIViewController *vc = [[UIApplication sharedApplication]keyWindow].rootViewController;
    [vc.view addSubview:[[settingshelper instance]web]];
        [[[settingshelper instance]button]addTarget:vc action:@selector(hideWebView:) forControlEvents:UIControlEventTouchUpInside];

}

and than in ur .cpp class call this ur . mm class method

void socialMediaLayer::facebook(){

  settings::showFaceBook();

}