// 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(@"%@
%d
%@", 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);