Cocos2d-x loading UIWebView display web page

Source: Internet
Author: User

In 2dx, click the button to load the webpage and use UIWebView on ios to display it. This allows you to roll back and refresh the current page. In 2dx, call [cpp] HelloWebView * webview = [[HelloWebView alloc] initWithNibName: nil bundle: nil]; [[UIApplication sharedApplication]. keyWindow addSubview: [webview view]; [objc] // HelloWebView. h // HelloTest /// Created by on 13-10-15. // Copyright (c) 2013 _ MyCompanyName __. all rights reserved. // # import <UIKit/UIKit. h> @ interface HelloWebView: UIViewController <UIWebViewDelegate> @ property (retain, nonatomic) IBOutlet UIWebView * webview; @ property (retain, nonatomic) IBOutlet callback * btn_back;-(IBAction) goBack :( id) sender; @ property (retain, nonatomic) IBOutlet progress * progress; @ property (retain, nonatomic) IBOutlet UIBarButtonItem * btn_reload;-(IBAction) ViewReload :( id) sender; @ end [objc] /// HelloWebView. m // HelloTest /// Created by on 13-10-15. // Copyright (c) 2013 _ MyCompanyName __. all rights reserved. // # import "HelloWebView. h "@ implementation HelloWebView @ synthesize progress; @ synthesize btn_reload; @ synthesize webview; @ synthesize btn_back;-(id) initWithNibName :( NSString *) bundle :( NSBundle *) nibBundleOrNil {self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil]; if (self) {// Custom initialization} return self;}-(void) didReceiveMemoryWarning {// Releases the view if it doesn' t have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use .} # pragma mark-View lifecycle-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view from its nib. // set the webview attribute. scalesPageToFit = YES; // automatically scales down the page to adapt to the screen. // loads the content NSURL * url = [NSURL URLWithString :@" http://www.baidu.com "]; // NSURL * url = [NSURL fileURLWithPath: filePath]; // load local resources NSURLRequest * request = [NSURLRequest requestWithURL: url]; [webview loadRequest: request]; webview. delegate = self; [progress setActivityIndicatorViewStyle: progress]; // [progress startAnimating]; [progress setHidden: YES];}-(void) viewDidUnload {[self setWebview: nil]; [self setBtn_back: nil]; [self setProgress: nil]; [Self setBtn_reload: nil]; [self setBtn_back: nil]; [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self. myOutlet = nil;}-(BOOL) initialize :( UIInterfaceOrientation) interfaceOrientation {// Return YES for supported orientations return (interfaceOrientation = UIInterfaceOrientationPortrait);}-(void) dealloc {[webview release]; [btn _ Back release]; [progress release]; [btn_reload release]; [btn_back release]; [super dealloc];}-(IBAction) GoBack :( id) sender {if ([webview canGoBack]) {[webview goBack];} else [self. view removeFromSuperview];}-(BOOL) webView :( UIWebView *) webView preview :( NSURLRequest *) request navigationType :( UIWebViewNavigationType) navigationType {return YES;}-(void) webViewDidStartLoad :( UIWebView *) webView {[progress setHidden: NO]; [progress startAnimating];}-(void) webViewDidFinishLoad :( UIWebView *) webView {[progress stopAnimating]; [progress setHidden: YES];}-(void) webView :( UIWebView *) webView 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] ;}} (IBAction) ViewReload :( id) sender {[webview reload] ;}@ end

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.