iOS 本地加载js文件

时间:2021-04-08 22:15:01
 #import "RootViewController.h"

 @interface RootViewController ()<UIWebViewDelegate>

 @property (nonatomic, strong)UIWebView *webView;

 @end

 @implementation RootViewController

 - (void)dealloc
{
self.webView = nil;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
self.webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view addSubview:self.webView];
// cli_dwr.html文件名
[self loadDocument:@"cli_dwr.html"];
}
// 加载本地使用说明文件
- (void)loadDocument:(NSString *)docName
{
NSString *mainBundleDirectory = [[NSBundle mainBundle] bundlePath];
NSString *path = [mainBundleDirectory stringByAppendingPathComponent:docName]; NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
self.webView.scalesPageToFit = YES;
[self.webView loadRequest:request];
} @end