cocos2d-x加載UIWebView顯示網頁

2dx中點擊按鈕之後加載網頁,在ios上用UIWebView來顯示,實現回退和刷新當前界面。

2dx中調用方法

HelloWebView *webview = [[HelloWebView alloc]initWithNibName:nil bundle:nil];
[[UIApplication sharedApplication].keyWindow addSubview:[webview view]];

用到的類

//
//  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 UIBarButtonItem *btn_back;

- (IBAction)GoBack:(id)sender;
@property (retain, nonatomic) IBOutlet UIActivityIndicatorView *progress;
@property (retain, nonatomic) IBOutlet UIBarButtonItem *btn_reload;
- (IBAction)ViewReload:(id)sender;

@end

//
//  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 *)nibNameOrNil 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.
    
    //設置屬性
    webview.scalesPageToFit = YES;//自動對頁面精細鬼縮放以適應屏幕
    //加載內容
    NSURL* url = [NSURL URLWithString:@"http://www.baidu.com"];
    //NSURL *url = [NSURL fileURLWithPath:filePath];//加載本地資源
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    [webview loadRequest:request];
    
    webview.delegate = self;
    
    [progress setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    //[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)shouldAutorotateToInterfaceOrientation:(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 shouldStartLoadWithRequest:(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

xib界面顯示如下


最終運行效果如下





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章