H5开发好的网页,想做成ios APP,于是 WKWebView 打开本地文件夹里的html页面。
下面例子里有地址带参数的和不带参数的。
遇到问题:
1、h5项目中用到了 less文件采用了发请求的方式、包括发送的ajax请求都出现了跨域的问题:
[self.webView.configuration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
2、报错:Undefined symbols for architecture x86_64 "_OBJC_CLASS_$_WKWebView",referenced from...
点击项目 - General - Frameworks,Libraries,and Embedded Content 点击 +
选择WebKit.framework
#import "ViewController.h"
#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *bundlePath = [[NSBundle mainBundle]bundlePath];
NSLog(@"bundlePath== : %@", bundlePath);//app打包后的路径 ,文件在app 内部
// /private/var/containers/Bundle/Application/645AC02B-F69F-4C87-B6AE-F404268A207D/webViewDemo.app
//load login.html
self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
// WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
// [config setValue:@(true) forKey:@"allowUniversalAccessFromFileURLs"];
[self.webView.configuration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
// self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.webView];
NSString *basePath = [NSString stringWithFormat:@"%@/www",bundlePath];
NSURL *baseUrl = [NSURL fileURLWithPath:basePath isDirectory:YES];
// 加载html地址带参数 方法一 loadFileURL
// 示例 portStr 固定8000
NSString *portStr = @"8000";//@(portstr.c_str()); 多个参数也可以直接拼在一起
NSString *filePath = [NSString stringWithFormat:@"file://%@/login.html?httpPort=%@", basePath, portStr];
NSURL *fileUrl = [NSURL URLWithString:filePath];
NSLog(@"fileUrl== : %@", fileUrl);
//file:///private/var/containers/Bundle/Application/645AC02B-F69F-4C87-B6AE-F404268A207D/webViewDemo.app/www/login.html?httpPort=8000
[self.webView loadFileURL:fileUrl allowingReadAccessToURL:baseUrl];
// 加载html地址带参数 方法二 loadRequest
// NSString *portStr = @(portstr.c_str());
// NSString *basePath2 = [NSString stringWithFormat:@"%@/www/login.html",bundlePath];
// NSString *urlString2 = [NSString stringWithFormat:@"?%@",portStr];
// [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString2 relativeToURL:[NSURL fileURLWithPath:basePath2]]]];//带参数
// // 加载html地址不带参数 loadHTMLString
// NSString *htmlUrl = [NSString stringWithFormat:@"%@/login.html",basePath];
// NSLog(@"dayinzifuchuanmainbasePath: %@", htmlUrl);
// NSString *indexContent = [NSString stringWithContentsOfFile:htmlUrl encoding:NSUTF8StringEncoding error:nil];
// [self.webView loadHTMLString:indexContent baseURL:baseUrl];
// // 实例化 WKWebView 对象
// self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
//
// //创建请求
// NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// //加载网页
// [self.webView loadRequest:request];
// [self.view addSubview:self.webView];
------UIWebView
// CGFloat width = self.view.frame.size.width;
// CGFloat height = self.view.frame.size.height - 20;
// UIWebView *_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, width, height)];
//
// NSString *urlStr = @"http://www.baidu.com";
// NSURL *url = [NSURL URLWithString:urlStr];
// NSURLRequest *request = [NSURLRequest requestWithURL:url];
// [_webView loadRequest:request];
}
#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) WKWebView *webView;
//@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end
刚使用mac 不习惯:
ctrl + 空格 切换输入法
command + c 复制 command + v 粘贴