我正在将一个巨大的angularJS应用程序移植到iOS 9,并希望从WKWebView(从UIWebView迁移)中受益。该应用程序是本地自包含的,因此使用file:// protocol。
为app主程序包提供所有文件不幸的是,听起来WKWebView最初破坏了iOS 8.x上的file://协议,但是当我看到全新的iOS 9 loadFileURL(basePath:,allowReadAccessToURL :) API时,会有一些亮点。
let readAccessPath = NSURL(string:"app", relativeToURL:bundleURL)?.absoluteURL
webView.loadFileURL(basePath!, allowingReadAccessToURL:readAccessPath!)
唉,当我将allowsReadAccessToURL设置为我的bundle(app /)中的根文件夹时,我只获得了#34;索引文件",没有加载异步文件。
任何对此问题都有经验的人?
[更新]我看到我的初始问题描述不够准确。我确实在运行HTML。但我的异步angularJS调用实际上被WebKit框架中的安全监视程序阻止。
答案 0 :(得分:6)
虽然我没有快速回答(我的意思是快速修复)但我确实有一个解决方案。
这涉及放弃file://协议并切换到http:// over localhost。
简短回答
以下是步骤:
1) - 在您自己的应用中安装本地Web服务器;
2) - 将本地Web服务器设置为从您选择的给定端口的localhost提供服务;
3) - 在给定正确的mime类型的情况下,设置实际为您的应用资源提供文件的委托;
4) - 授权绕过iOS9 ATS处理http(而不仅仅是https)。
瞧!
详细解答
1)在您自己的应用中安装本地Web服务器;
从Github repo:https://github.com/swisspol/GCDWebServer
安装GCDWebServer2)设置本地Web服务器,以便从 的给定端口的localhost提供服务
鉴于您的angularjs或HTML应用程序文件位于资源文件夹中的“app”文件夹中。
在你的vc ViewDidLoad中:
@implementation ViewController
GCDWebServer* _webServer;
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView = [[WKWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:self.webView];
self.webView.navigationDelegate = self;
NSURL *bundleURL = [NSBundle mainBundle].bundleURL;
NSURL *basePath = nil;
// Init WebServer
[self initWebServer:[[NSURL URLWithString:@"app" relativeToURL:bundleURL] absoluteURL]];
basePath = [NSURL URLWithString:@"http://localhost:8080/page.html#/home" relativeToURL:nil];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:basePath];
[self.webView loadRequest:request];
}
3)在给定正确的mime类型的情况下,设置实际为您的应用资源提供文件的代理;
-(void)initWebServer:(NSURL *)basePath {
// Create server
_webServer = [[GCDWebServer alloc] init];
#define GCDWebServer_DEBUG 0
#define GCDWebServer_VERBOSE 1
#define GCDWebServer_INFO 2
#define GCDWebServer_WARNING 3
#define GCDWebServer_ERROR 4
#define GCDWebServer_EXCEPTION 5
[GCDWebServer setLogLevel:GCDWebServer_ERROR];
// Add a handler to respond to GET requests on any URL
[_webServer addDefaultHandlerForMethod:@"GET"
requestClass:[GCDWebServerRequest class]
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
//NSLog([NSString stringWithFormat:@"WS: loading %@", request]);
NSString * page = request.URL.lastPathComponent;
NSString * path = request.URL.path;
NSString * file = path;
//NSLog(@"WS: loading %@", file);
NSString * fullPath = [NSString stringWithFormat:@"%@%@", basePath, path];
NSString * sFullPath = [fullPath substringFromIndex:7];
BOOL isText = NO;
if([page.lastPathComponent hasSuffix:@"html"]) {
isText = YES;
}
if (isText) {
NSError * error = nil;
NSString * html = [NSString stringWithContentsOfFile:sFullPath encoding:NSUTF8StringEncoding error: &error];
return [GCDWebServerDataResponse responseWithHTML:html];
}
else {
NSData * data = [NSData dataWithContentsOfFile:sFullPath];
if (data !=nil) {
NSString * type = @"image/jpeg";
if ([page.lastPathComponent hasSuffix:@"jpg"]) type = @"image/jpeg";
else if ([page.lastPathComponent hasSuffix:@"png"]) type = @"image/png";
else if ([page.lastPathComponent hasSuffix:@"css"]) type = @"text/css";
else if ([page.lastPathComponent hasSuffix:@"js" ]) type = @"text/javascript";
return [GCDWebServerDataResponse responseWithData:data contentType:type];
}
else {
return [GCDWebServerDataResponse responseWithHTML:[NSString stringWithFormat:@"<html><body><p>404 : unknown file %@ World</p></body></html>", sFullPath]];
//return [GCDWebServerDataResponse responseWithHTML:@"<html><body><p>Hello World</p></body></html>"];
}
}
}];
// Start server on port 8080
[_webServer startWithPort:8080 bonjourName:nil];
NSLog(@"Visiting %@", _webServer.serverURL);
}
4)授权绕过iOS9 ATS处理http(而不仅仅是https)
在Xcode的info.plist文件中,您必须在键值内添加名为“App Transport Security Settings”的字典,如下所示:
NSAllowsArbitraryLoads = true
希望它有所帮助。 任何偶然发现更简单的人都欢迎回答!
答案 1 :(得分:2)
正如我所说,你正在制作移动应用程序并在之后进行编译......我有类似的问题。但对我来说,最终构建是由adobe build.phonegap服务完成的。我唯一要做的就是在config.xml中添加cordova-whitelist-plugin
< gap:plugin name="cordova-plugin-whitelist" source="npm" version="1.0.0" />
而不是添加访问此类链接的权限
<allow-intent href="file:///*/*" />
也进入config.xml
对不起,如果我理解你错了。
答案 2 :(得分:1)
我建议使用GCDWebServer,您可以将http://localhost
作为本地网络服务器运行。
Swift 3.0
添加GCDWebServer pod pod "GCDWebServer", "~> 3.0"
单击并将AngularJS Web文件夹拖到Xcode中,然后在出现提示时选择“如果需要复制项目”和“创建文件夹引用”
在控制器中使用此代码运行localhost Web服务器
class MainViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler, WKUIDelegate, GIDSignInUIDelegate {
var webView : WKWebView!
var webServer:GCDWebServer?
override func viewDidLoad() {
super.viewDidLoad()
self.webView.load(URLRequest(url: loadDefaultIndexFile()!))
}
private func loadDefaultIndexFile() -> URL? {
self.webServer = GCDWebServer()
let mainBundle = Bundle.main
// The path to my index.html is www/index.html. If using a default public folder then it could be public/index.html
let folderPath = mainBundle.path(forResource: "www", ofType: nil)
self.webServer?.addGETHandler(forBasePath: "/", directoryPath: folderPath!, indexFilename: "index.html", cacheAge: 0, allowRangeRequests: true)
do {
try self.webServer?.start(options: [
"Port": 3000,
"BindToLocalhost": true
]);
}catch{
print(error)
}
// Path should be http://localhost:3000/index.html
return self.webServer?.serverURL
}
}