Cocos2d-x embedded WebView on iOS/Android dual Platform

Source: Internet
Author: User

Cocos2d-x embedded WebView on iOS/Android dual Platform

 

The help and game announcements in the game can be implemented by embedding the WebView display solution. It is convenient to modify and update the announcement style and content by editing HTML.

IOS platform embedding

The embedding on the iOS platform is smooth. There are ready-made tutorials and Code on the Internet. The key is to encapsulate the mixed compilation of OC and C ++ code.
See Implementation in the following link: cocos2d-x using UIWebView to load web pages

Android platform embedded 1. Through JNI from the cocos2d-x C ++ code to call java code.

This is a prerequisite for Android to implement WebView embedding. You must learn it carefully. Otherwise, you cannot continue. For details, see JNI user guide.

This is my summary of a JNI usage tutorial, which basically covers all key points related to the use of JNI.

2. java code implementation

See the following code for the implementation of standard Android WebView:

Public void displayWebView () {this. runOnUiThread (new Runnable () {public void run () {// actInstance is the member variable and is the current Activity. M_webView is the member variable LinearLayout layout = new LinearLayout (actInstance); actInstance. addContentView (layout, new LayoutParams (LayoutParams. FILL_PARENT, LayoutParams. FILL_PARENT); m_webView = new WebView (actInstance); layout. addView (m_webView); LinearLayout. layoutParams linearParams = (LinearLayout. layoutParams) m_webView.getLayoutParams (); // optional webview position, which can be x, y, width, or height. It can also be input as a function parameter. LinearParams. leftMargin = x; linearParams. topMargin = y; linearParams. width = width; linearParams. height = height; m_webView.setLayoutParams (linearParams); // optional webview configuration m_webView.setBackgroundColor (0); m_webView.getSettings (). setCacheMode (WebSettings. LOAD_NO_CACHE); m_webView.getSettings (). setAppCacheEnabled (false );}});}

Note the following:

The Code called by JNI is not in the main thread Program Stream, And the UI display needs to be placed in the main thread. This problem took me half a day, and I finally asked fengge to understand what I needed to do on Android.

Source code sharing

 

3. Configure the webView to jump to the link in the Custom webView, instead of opening the system browser.

This requires setting WebViewClient for webView and implementing the shouldOverrideUrlLoading method as follows:

Change the size and position of the WebView

Set the LayoutParamaters and setLayoutParams of webView.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.