加载本地文件代码:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect bounds = [[UIScreen mainScreen]bounds];
UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];
webView.scalesPageToFit = YES;//自动对页面进行缩放以适应屏幕
[self.view addSubview:webView];
NSURL *url =[NSURL URLWithString:[[NSBundle mainBundle]pathForResource:@"2-2" ofType:@"png" inDirectory:@"assets"]];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
说明:
1.拖动文件到Xcode,提示两个选择,“create groups”和“create folder references”,默认情况下为第一种,即所有加入到项目的文件都会在mainBundle根路径下,即不管加入项目的文件的目录结构如何,在APP中都可以通过mainBundlePath/filename来访问到;如果采用第二种方式,则就会保留相对路径,需要通过mainBundlePath/path/filename来访问。通过这两种方式到项目的文件夹显示具有不同的颜色。 选择“create groups”,文件夹颜色为黄色。
html里有一个路径的问题( link href= “”),在Xcode中, Xcode不能找到像url("…/images/photo.png")这种路径,所以应该在Xcode中调整下html文件访问本页面图片和css样式文件的路径。
加载网页
CGRect bounds = [[UIScreen mainScreen]applicationFrame];
UIWebView* webview = [[UIWebView alloc]initWithFrame:bounds];
[_container addSubview:webview];
NSURL* url =[NSURL URLWithString:@"https://www.baidu.com"];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
参考文章:
https://www.jianshu.com/p/8f3c47c24e29
https://www.jianshu.com/p/173b7f371bf7