Created
October 31, 2012 08:00
-
-
Save bugcloud/3985732 to your computer and use it in GitHub Desktop.
Load local html,javascript,css in iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @interface ViewController : UIViewController { | |
| UIWebView *mainWebView; | |
| } | |
| @property(nonatomic, retain) UIWebView *mainWebView; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "ViewController.h" | |
| @interface ViewController () | |
| @end | |
| @implementation ViewController | |
| @synthesize mainWebView; | |
| - (void)viewDidLoad | |
| { | |
| [super viewDidLoad]; | |
| // initialize UIWebView | |
| mainWebView = [[UIWebView alloc] initWithFrame:[self frameRectWithDeviceOrientation]]; | |
| mainWebView.scalesPageToFit = YES; | |
| [self.view addSubview:mainWebView]; | |
| NSString *path = [[NSBundle mainBundle] bundlePath]; | |
| NSURL *baseURL = [NSURL fileURLWithPath:path]; | |
| NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"login" ofType:@"html"]; | |
| NSString *htmlStr = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; | |
| [mainWebView loadHTMLString:htmlStr baseURL:baseURL]; | |
| } | |
| - (void)didReceiveMemoryWarning | |
| { | |
| [super didReceiveMemoryWarning]; | |
| // Dispose of any resources that can be recreated. | |
| } | |
| - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | |
| { | |
| return YES; | |
| } | |
| - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation | |
| { | |
| // WebViewのframeをviewに合わせる | |
| mainWebView.frame = self.view.bounds; | |
| } | |
| // デバイスの向きに応じてCGRectを作成して返す | |
| // 例) iPadの場合 | |
| // 縦向き => CGRectMake(0, 0, 748, 1024) | |
| // 横向き => CGRectMake(0, 0, 1024, 748) | |
| - (CGRect)frameRectWithDeviceOrientation | |
| { | |
| CGRect frameRect; | |
| if ([self isOrientationPortrait]) { | |
| frameRect = self.view.bounds; | |
| } else { | |
| frameRect = CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width); | |
| } | |
| return frameRect; | |
| } | |
| // デバイスの向きが縦向きかどうかを返す | |
| - (BOOL)isOrientationPortrait | |
| { | |
| return | |
| ( | |
| self.interfaceOrientation == UIInterfaceOrientationPortrait || | |
| self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown | |
| ); | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment