Skip to content

Instantly share code, notes, and snippets.

@bugcloud
Created October 31, 2012 08:00
Show Gist options
  • Select an option

  • Save bugcloud/3985732 to your computer and use it in GitHub Desktop.

Select an option

Save bugcloud/3985732 to your computer and use it in GitHub Desktop.
Load local html,javascript,css in iOS
@interface ViewController : UIViewController {
UIWebView *mainWebView;
}
@property(nonatomic, retain) UIWebView *mainWebView;
@end
#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