//The header file: /* Copyright 2007 Lee S. BarneyThis file is part of QuickConnectiPhoneHybrid.QuickConnectiPhoneHybrid is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the 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 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.You should have received a copy of the GNU Lesser General Public License along 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 modify it under the terms of the GNU Lesser General Public License as published by the 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 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.You should have received a copy of the GNU Lesser General Public License along 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 controller UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; contentView.backgroundColor = [UIColor blueColor];// important for view orientation rotation contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); self.view = contentView; self.view.autoresizesSubviews = YES; //create a frame that will be used to size and place the web view CGRect webFrame = [[UIScreen mainScreen] applicationFrame]; webFrame.origin.y -= 20.0; // shift the display up so that it covers the default open space from the content view UIWebView *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 directory NSString *filePathString = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; //build the URL and the request for the index.html file NSURL *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 照著范例代码用
/*
Copyright 2007 Lee S. BarneyThis file is part of QuickConnectiPhoneHybrid.QuickConnectiPhoneHybrid is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the 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 of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.You should have received a copy of the GNU Lesser General Public License
along 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 modify
it under the terms of the GNU Lesser General Public License as published by
the 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 of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.You should have received a copy of the GNU Lesser General Public License
along 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 controller
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blueColor];// important for view orientation rotation
contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.view = contentView;
self.view.autoresizesSubviews = YES;
//create a frame that will be used to size and place the web view
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
webFrame.origin.y -= 20.0; // shift the display up so that it covers the default open space from the content view
UIWebView *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 directory
NSString *filePathString = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
//build the URL and the request for the index.html file
NSURL *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
照著范例代码用
记得翻墙出去