您的位置:首页 > 移动开发 > IOS开发

iOS WebView加载本地html文件

2016-08-03 14:13 661 查看
#import "ViewController.h"

@interface
ViewController ()

@property (nonatomic,
strong) UIWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super
viewDidLoad];

    

        self.webView = [[UIWebView
alloc] initWithFrame:[UIScreen
mainScreen].bounds];

    

//    NSURL *url = [[NSBundle mainBundle] URLForResource:@"html" withExtension:@"html"];

//    NSURLRequest *request = [NSURLRequest requestWithURL:url];

//    [self.webView loadRequest:request];

    

//    NSString *path = [[NSBundle mainBundle] pathForResource:@"html.html" ofType:nil];

//    NSData *data = [NSData dataWithContentsOfFile:path];

//    [self.webView loadData:data MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil];

    

    //加载本地的html文件

    NSString *resourcePath = [[NSBundle
mainBundle] resourcePath];

    NSString *filePath  = [resourcePath
stringByAppendingPathComponent:@"html.html"];

    NSString *htmlstring =[[NSString
alloc] initWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:nil];

    [self.webView
loadHTMLString:htmlstring 
baseURL:nil];

    

    [self.view
addSubview:self.webView];

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS webview html