当前位置: 代码迷 >> Iphone >> 怎样使用webview控件?解决方案
  详细解决方案

怎样使用webview控件?解决方案

热度:254   发布时间:2016-04-25 06:53:24.0
怎样使用webview控件?
我就这个问题,不会用!

谢谢大家!!!

------解决方案--------------------
C/C++ code
//The header file:/*Copyright 2007 Lee S. BarneyThis file is part of QuickConnectiPhoneHybrid.QuickConnectiPhoneHybrid is free software: you can redistribute it and/or modifyit under the terms of the GNU Lesser General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.QuickConnectiPhoneHybrid is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU Lesser General Public License for more details.You should have received a copy of the GNU Lesser General Public Licensealong with QuickConnectiPhoneHybrid. If not, see .*/#import#define kAccelerometerFrequency .001 //Hz@interface BrowserViewController : UIViewController {UIWebView *webView;}@property (nonatomic, retain) UIWebView *webView;@end//The class file:/*Copyright 2007 Lee S. BarneyThis file is part of QuickConnectiPhoneHybrid.QuickConnectiPhoneHybrid is free software: you can redistribute it and/or modifyit under the terms of the GNU Lesser General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.QuickConnectiPhoneHybrid is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU Lesser General Public License for more details.You should have received a copy of the GNU Lesser General Public Licensealong with QuickConnectiPhoneHybrid. If not, see .*/#import "BrowserViewController.h"@implementation BrowserViewController@synthesize webView;- (id)init{/*if (self = [super init]) {}*/return self;}- (void)loadView{NSLog(@"loading view");// the base view for this view controllerUIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];contentView.backgroundColor = [UIColor blueColor];// important for view orientation rotationcontentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);self.view = contentView;self.view.autoresizesSubviews = YES;//create a frame that will be used to size and place the web viewCGRect webFrame = [[UIScreen mainScreen] applicationFrame];webFrame.origin.y -= 20.0; // shift the display up so that it covers the default open space from the content viewUIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];self.webView = aWebView;//aWebView.scalesPageToFit = YES;aWebView.autoresizesSubviews = YES;aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);//set the web view and acceleration delagates for the web view to be itself[aWebView setDelegate:self];//determine the path the to the index.html file in the Resources directoryNSString *filePathString = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];//build the URL and the request for the index.html fileNSURL *aURL = [NSURL fileURLWithPath:filePathString];NSURLRequest *aRequest = [NSURLRequest requestWithURL:aURL];//load the index.html file into the web view.[aWebView loadRequest:aRequest];//add the web view to the content view[contentView addSubview:webView];[aWebView release];[contentView release];}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{// Return YES for supported orientations.return YES;}- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{if(fromInterfaceOrientation == UIInterfaceOrientationPortrait){NSString* result = [webView stringByEvaluatingJavaScriptFromString:@"rotate(0)"];//NSString a = *result;NSLog(result);}else{[webView stringByEvaluatingJavaScriptFromString:@"rotate(1)"];}//[self.webView sizeToFit];//CGRect curBounds = [[UIScreen mainScreen] bounds];//[self.webView setBounds:self.origViewRectangle];//[[UIScreen mainScreen] bounds]]//NSLog(@"orienting");}- (void) accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration{NSString* javaScriptCall = [NSString stringWithFormat:@"accelerate(%f, %f, %f)", acceleration.x, acceleration.y, acceleration.z];[webView stringByEvaluatingJavaScriptFromString:javaScriptCall];//Use a basic low-pass filter to only keep the gravity in the accelerometer values//_accelerometer[0] = acceleration.x * kFilteringFactor + _accelerometer[0] * (1.0 - kFilteringFactor);//_accelerometer[1] = acceleration.y * kFilteringFactor + _accelerometer[1] * (1.0 - kFilteringFactor);//_accelerometer[2] = acceleration.z * kFilteringFactor + _accelerometer[2] * (1.0 - kFilteringFactor);}- (void)didReceiveMemoryWarning{[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview.// Release anything that's not essential, such as cached data.}- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{NSLog(@"An error happened during load");}- (void)webViewDidStartLoad:(UIWebView *)webView{NSLog(@"loading started");}- (void)webViewDidFinishLoad:(UIWebView *)webView{NSLog(@"finished loading");}- (void)dealloc{[super dealloc];}@end
  相关解决方案