使用swift从ios上的文档目录加载html文件到UIWebview

时间:2015-12-15 14:22:50

标签: html ios swift uiwebview ios-simulator

我有一个html文件和html使用的其他文件(css,.png)保存在文档目录中。如何在UIWebView或wkwebview中加载这个html文件 使用swift?我在objective-c中找到了一些例子但在swift中没有。我对Objective-c一无所知..

let path=getCurrenttHtmlStartPage()
var hContent = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding) 
webView!.loadHTMLString(hContent, baseURL: nil)
webView!.hidden=false 

路径是文档文件夹中的路径。 /用户/ pmdevios /库/.../文件/内容/ HTML / index.html中。

通过这种方式,html中的其他文件不显示(图像)所以我想用其他方式来做这个

webView!.loadFileURL(path, allowingReadAccessToURL: path)

3 个答案:

答案 0 :(得分:2)

使用下面的代码就可以了!

#include <algorithm>
#include <iostream>
#include <iterator>
#include <memory>

class Problem {
public:
    template<class AIter, class BIter>
    Problem(AIter abegin, AIter aend, BIter bbegin, BIter bend)
    :   m_width(std::distance(abegin, aend))
    ,   m_height(std::distance(bbegin, bend))
    ,   m_table(new int[(m_width + 1) * (m_height + 1)])
    {
        std::fill(m_table.get(), m_table.get() + (m_width + 1) * (m_height + 1), 0);
        for(size_t i = 0; i < m_width; ++i)
            m_table[i + 1] = *abegin++;
        for(size_t j = 0; j < m_height; ++j)
            m_table[(j + 1) * (m_width + 1)] = *bbegin++;
    }

    bool Solve() { return solve(0, 0); }
    int operator()(size_t i, size_t j) const;

private:
    int a(size_t i) const { return m_table[i + 1]; }
    int b(size_t j) const { return m_table[(j + 1) * (m_width + 1)]; }
    int get(size_t i, size_t j) const { return m_table[(j + 1) * (m_width + 1) + i + 1]; }
    void set(size_t i, size_t j, int value) { m_table[(j + 1) * (m_width + 1) + i + 1] = value; }
    int colSum(size_t i) const;
    int rowSum(size_t j) const;
    bool solve(size_t i, size_t j);

    size_t m_width, m_height;
    std::unique_ptr<int[]> m_table; // (width + 1) x (height + 1)
};

int Problem::colSum(size_t i) const {
    int sum = 0;
    for(size_t j = 0; j < m_height; ++j)
        sum += get(i, j);
    return sum;
}

int Problem::rowSum(size_t j) const {
    int sum = 0;
    for(size_t i = 0; i < m_width; ++i)
        sum += get(i, j);
    return sum;
}

// solves column-wise using backtracking
bool Problem::solve(size_t i, size_t j) {
    size_t width = m_width, height = m_height;

    // past last column?
    if(i >= width) {
        // found solution
        return true;
    }

    // remainder in column and row
    int remColSum = a(i) - colSum(i);
    int remRowSum = b(j) - rowSum(j);
    // early break
    if(remColSum <= 0 || remRowSum <= 0)
        return false;

    // starting at the minimal required value (1 or remColSum if on last row)
    int startValue = j + 1 < height ? 1 : remColSum;
    // remaining row sum cannot support the starting value
    if(remRowSum < startValue)
        return false;
    // end value minimum remaining sum
    int endValue = remColSum < remRowSum ? remColSum : remRowSum;
    // on last element must equal starting value
    if(i + 1 == width && j + 1 == height && startValue != endValue)
        return false;

    // column-wise i.e. next cell is (i, j + 1) wrapped
    int nextI = i + (j + 1) / height;
    int nextJ = (j + 1) % height;
    for(int value = startValue; value <= endValue; ++value) {
        bool valid = true;
        // check row up to i
        for(size_t u = 0; u < i && valid; ++u)
            valid = (get(u, j) != value);
        // check column up to j
        for(size_t v = 0; v < j && valid; ++v)
            valid = (get(i, v) != value);
        if(!valid) {
            // value is invalid in partially filled table
            continue;
        }

        // value produces a valid, partially filled table, now try recursing
        set(i, j, value);
        // upon first solution break
        if(solve(nextI, nextJ))
            return true;
    }

    // upon failure backtrack
    set(i, j, 0);
    return false;
}

int Problem::operator()(size_t i, size_t j) const {
    return get(i, j);
}

int main() {
    int a[] = { 10, 3, 3 };
    int b[] = { 9, 7 };
    size_t width = sizeof(a) / sizeof(*a);
    size_t height = sizeof(b) / sizeof(*b);
    Problem problem(a, a + width, b, b + height);
    if(!problem.Solve()) {
        std::cout << "No solution" << std::endl;
    }

    for(size_t j = 0; j < height; ++j) {
        if(j == 0) {
            std::cout << " ";
            for(size_t i = 0; i < width; ++i)
                std::cout << "  " << a[i];
            std::cout << std::endl;
        }
        std::cout << b[j];
        for(size_t i = 0; i < width; ++i) {
            int value = problem(i, j);
            if(value == 0)
                std::cout << "   ";
            else
                std::cout << "  " << value;
        }
        std::cout << std::endl;
    }
    return 0;
}

folder:表示文档文件夹中路径的字符串 _currentHtmlStartPage:文件名的字符串(例如index.html)

答案 1 :(得分:1)

修改

  1. 使用NSFileManger实例方法URLsForDirectory(inDomains:)

  2. 获取URL
  3. 将文件名(myFile.html)附加到我们从NSFileManager返回的URL

  4. 使用网址初始化NSURLRequest对象。

  5. 使用UIWebViewWKWebView类加载请求。

  6. let fileManager = NSFileManager.defaultManager()
    var URL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    URL = URL.URLByAppendingPathComponent("myFile.html")
    
    let request = NSURLRequest(URL: fileURL)
    webView.loadRequest(request)
    

答案 2 :(得分:0)

我的头顶:

if let fileURL = NSBundle.mainBundle().URLForResource("myFile", withExtension: "html") {
    let request = NSURLRequest(URL: fileURL)
    webView.loadRequest(request)
}

你的html和css文件中需要一个平面目录结构,因为这是他们在应用程序中的最终结果。