美文网首页
项目中遇到哪些的问题

项目中遇到哪些的问题

作者: 代码小白丶 | 来源:发表于2016-05-09 14:45 被阅读264次

@文/Jay神(简书作者)


1.WKWebView本地读取html时出现的问题

在iOS9.0之前当使用loadRequest来读取本地的HTML时,WKWebView是无法读取成功的。

后台会会打印出如下的提示:
Could not create a sandbox extension for /

原因是WKWebView是不允许通过loadRequest的方法来加载本地根目录的HTML文件
而在iOS9的SDK中加入了以下方法来加载本地的HTML文件:

[WKWebView loadFileURL:allowingReadAccessToURL:]

但是在iOS9以下的版本是没提供这个便利的方法的。以下为解决方案的思路,就是在iOS9以下版本时,先将本地HTML文件的数据copy到temp目录中,然后再使用loadRequest来加载。但是如果在HTML中加入了其他资源文件,例如js,css,image等必须一同copy到temp中。

解决方案如下

1.Objective-C:

//将文件copy到tmp目录
- (NSURL *)fileURLForBuggyWKWebView8:(NSURL *)fileURL {
    NSError *error = nil;
    if (!fileURL.fileURL || ![fileURL checkResourceIsReachableAndReturnError:&error]) {
        return nil;
    }
    // Create "/temp/www" directory
    NSFileManager *fileManager= [NSFileManager defaultManager];
    NSURL *temDirURL = [[NSURL fileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:@"www"];
    [fileManager createDirectoryAtURL:temDirURL withIntermediateDirectories:YES attributes:nil error:&error];

    NSURL *dstURL = [temDirURL URLByAppendingPathComponent:fileURL.lastPathComponent];
    // Now copy given file to the temp directory
    [fileManager removeItemAtURL:dstURL error:&error];
    [fileManager copyItemAtURL:fileURL toURL:dstURL error:&error];
    // Files in "/temp/www" load flawlesly :)
    return dstURL;
}

//调用逻辑
 NSString *path = [[NSBundle mainBundle] pathForResource:@"indexoff" ofType:@"html"];
    if(path){
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
            // iOS9. One year later things are OK.
            NSURL *fileURL = [NSURL fileURLWithPath:path];
            [self.webView loadFileURL:fileURL allowingReadAccessToURL:fileURL];
        } else {
            // iOS8. Things can be workaround-ed
            //   Brave people can do just this
            //   fileURL = try! pathForBuggyWKWebView8(fileURL)
            //   webView.loadRequest(NSURLRequest(URL: fileURL))

            NSURL *fileURL = [self.fileHelper fileURLForBuggyWKWebView8:[NSURL fileURLWithPath:path]];
            NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
            [self.webView loadRequest:request];
        }
    }

2.Swift

//将文件copy到tmp目录
func fileURLForBuggyWKWebView8(fileURL: NSURL) throws -> NSURL {
    // Some safety checks
    var error:NSError? = nil;
    if (!fileURL.fileURL || !fileURL.checkResourceIsReachableAndReturnError(&error)) {
        throw error ?? NSError(
            domain: "BuggyWKWebViewDomain",
            code: 1001,
            userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("URL must be a file URL.", comment:"")])
    }

    // Create "/temp/www" directory
    let fm = NSFileManager.defaultManager()
    let tmpDirURL = NSURL.fileURLWithPath(NSTemporaryDirectory()).URLByAppendingPathComponent("www")
    try! fm.createDirectoryAtURL(tmpDirURL, withIntermediateDirectories: true, attributes: nil)

    // Now copy given file to the temp directory
    let dstURL = tmpDirURL.URLByAppendingPathComponent(fileURL.lastPathComponent!)
    let _ = try? fileMgr.removeItemAtURL(dstURL)
    try! fm.copyItemAtURL(fileURL, toURL: dstURL)

    // Files in "/temp/www" load flawlesly :)
    return dstURL
}

//方法调用

    var filePath = NSBundle.mainBundle().pathForResource("file", ofType: "pdf")

    if #available(iOS 9.0, *) {
        // iOS9. One year later things are OK.
        webView.loadFileURL(fileURL, allowingReadAccessToURL: fileURL)
    } else {
        // iOS8. Things can be workaround-ed
        //   Brave people can do just this
        //   fileURL = try! pathForBuggyWKWebView8(fileURL)
        //   webView.loadRequest(NSURLRequest(URL: fileURL))
        do {
            fileURL = try fileURLForBuggyWKWebView8(fileURL)
            webView.loadRequest(NSURLRequest(URL: fileURL))
        } catch let error as NSError {
            print("Error: " + error.debugDescription)
        }
    }

相关文章

  • 项目中遇到哪些的问题

    @文/Jay神(简书作者) 1.WKWebView本地读取html时出现的问题 在iOS9.0之前当使用loadR...

  • vue2项目中遇到的问题汇总

    华为内置浏览器打不开vue2页面如何调试: 用iE浏览器的 edge版本,检查报错,一个错都不能有,全部清除报错 ...

  • 项目中遇到的问题

    mySql常用类型: int:整型 默认长度11 10位长度 double:浮点型,例如double(5,2)表...

  • 项目中遇到的问题

  • 项目中遇到的问题

    1.判断是安卓还是IOS 2.怎么渲染数据 3.子组件怎么获取子组件的属性 1.图二是在子组件上怎么接受父组件传来...

  • 项目中遇到的问题

    1. vue-cli 文件的作用 index.html 和App.vue都是指同一个页面,App.vue中是组价...

  • 项目中遇到的问题

    数据解析问题: 由于后台返回的数据是一串字符串,而不是JSON格式.所以需要我们自己处理.数据格式是这样的:key...

  • 项目中遇到的问题

    1.页面之间进行跳转后回到原始页面,页面布局整体下移64px/44px个高度? 查阅了一些资料后,说要设置这个属性...

  • 项目中遇到的问题

    dyld: Library not loaded: @rpath/Alamofire.framework/Alam...

  • 项目中遇到的问题

    1.iOS中,延时调用不会因为对象的销毁而失效,如果不作取消处理,可能会造成程序功能混乱。 2.在接收到新聊天消息...

网友评论

      本文标题:项目中遇到哪些的问题

      本文链接:https://www.haomeiwen.com/subject/hbcerttx.html