cocos2dx IOS 使用UIWebView来加载网页


//  WebViewIOS.h
//  evilcard
//
//  Created by keltonxian on 6/18/14.
//
//

#ifndef __WEBVIEWIOS_H__
#define __WEBVIEWIOS_H__

#import <UIKit/UIKit.h>
class WebViewIOS;

@interface SocialView : UIView <UIWebViewDelegate> {
    UIWebView *webView;
    UIToolbar *toobar;
    UIView *spinner;
    CGRect frameSpinner;
    BOOL isSending;
}

@property (nonatomic) WebViewIOS *parentLayer;

+ (void)openURL:(NSString *)url rect:(CGRect)rect parent:(WebViewIOS *)parent;
- (void)sendRequest;
- (void)sendRequest:(NSString *)url;
- (void)backClicked:(id)sender;

@end

#include "cocos2d.h"

USING_NS_CC;

class WebViewIOS : public CCLayer {
public:
    CREATE_FUNC(WebViewIOS);
    void openURL(const char *url);
    void callbackClickBack();
protected:
    virtual bool init();
    WebViewIOS(void);
    ~WebViewIOS(void);
    virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
    virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
    virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
    virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
    virtual void keyBackClicked();
    
};


#endif
//
//  WebViewIOS.h
//
//  Created by keltonxian on 6/18/14.
//
//

#include "WebViewIOS.h"
#import "EAGLView.h"

@implementation SocialView

@synthesize parentLayer;

+ (void)openURL:(NSString *)url rect:(CGRect)rect parent:(WebViewIOS *)parent
{
    SocialView *socialView = [[SocialView alloc] initWithFrame:rect];
    socialView.parentLayer = parent;
    [[EAGLView sharedEGLView] addSubview:socialView];
    [socialView sendRequest:url];
}

- (id)initWithFrame:(CGRect)frame {
	self = [super initWithFrame:frame];
	if (self == nil) {
		return self;
	}
    float toolBarheight = 35;
    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, toolBarheight, frame.size.width, frame.size.height-toolBarheight)];
	webView.opaque = NO;
	webView.backgroundColor = [UIColor clearColor];
	[self addSubview:webView];
	[webView release];
    webView.delegate = self;
    
    toobar = [[UIToolbar alloc] init];
    [toobar setFrame:CGRectMake(0, 0, frame.size.width, toolBarheight)];
    toobar.barStyle = UIBarStyleBlackOpaque;
    UIBarButtonItem *mBackButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(backClicked:)];
    [toobar setItems:[NSArray arrayWithObjects:mBackButton,nil] animated:YES];
    [self addSubview:toobar];
    [toobar release];
    
    spinner = nil;
    frameSpinner = CGRectMake(0, toolBarheight, frame.size.width, frame.size.height-toolBarheight);
    
    return self;
}

- (void)backClicked:(id)sender {
    [self removeFromSuperview];
    parentLayer->callbackClickBack();
}

- (void)dealloc {
    [super dealloc];
}

- (void)sendRequest {
    
}

- (void)sendRequest:(NSString *)url {
    NSURL *u = [NSURL URLWithString:url];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:u cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
	[webView loadRequest:requestObj];
	[webView setContentMode:UIViewContentModeScaleToFill];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSLog(@"webView start load");
    if (nil != spinner) {
        [spinner removeFromSuperview];
    }
    spinner = [[UIView alloc] initWithFrame:frameSpinner];
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
	[indicator setCenter:CGPointMake(frameSpinner.size.width/2, frameSpinner.size.height/2)];
    [spinner addSubview:indicator];
    [indicator release];
	[indicator startAnimating];
	[self addSubview:spinner];
	[spinner release];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"webView finish load");
    [spinner removeFromSuperview];
    spinner = nil;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [spinner removeFromSuperview];
    spinner = nil;
    NSLog(@"%@\n%d\n%@", error, [error code], [error domain]);
    NSString *msg = [error description];
    UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"IOS Alert" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [msgbox autorelease];
    [msgbox show];
}

@end

WebViewIOS::WebViewIOS(void)
{
    
}

WebViewIOS::~WebViewIOS(void)
{
    
}

bool WebViewIOS::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    setTouchEnabled(true);
    setTouchMode(kCCTouchesOneByOne);
    setTouchPriority(-0x7ffffff0); // MAX_INT 0x7fffffff
    setKeypadEnabled(true);
    
    return true;
}

void WebViewIOS::callbackClickBack()
{
    this->removeFromParentAndCleanup(true);
}

void WebViewIOS::openURL(const char *url)
{
    CCEGLView *openglView = CCEGLView::sharedOpenGLView();
    CCSize size = openglView->getFrameSize();
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    [SocialView openURL:[NSString stringWithCString:url encoding:NSASCIIStringEncoding] rect:rect parent:this];
}

bool WebViewIOS::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) {
    return true;
}

void WebViewIOS::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) {
}

void WebViewIOS::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {
}

void WebViewIOS::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) {
}

void WebViewIOS::keyBackClicked() {
}


不浪费时间,直接先上代码,没写注释,反正功能比较简单,调用时这样就行啦:

CCScene *scene = CCDirector::sharedDirector()->getRunningScene();
    EvilWebViewIOS *webView = EvilWebViewIOS::create();
    scene->addChild(webView, 0x7ffffff0); // MAX_INT 0x7fffffff
    webView->openURL(pszUrl);



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值