【问题标题】:Display local HTML page in UIWebView在 UIWebView 中显示本地 HTML 页面
【发布时间】:2015-12-01 10:11:33
【问题描述】:

我想在我的UIWebView 中显示一个本地 HTML 文件。我可以展示它,但没有任何样式或图像。意味着相对路径不起作用,但我必须使用相对路径,因为会有更多文件。这是我的WebViewController

class WebViewController: UIViewController {
    @IBOutlet weak var web1: UIWebView!

    var fileName : String!

    override func viewDidLoad() {
        if let file = self.fileName{
            print("got: "+file)
        }

        let url = NSBundle.mainBundle().URLForResource(self.fileName, withExtension:"html")
        let request = NSURLRequest(URL: url!)
        web1.loadRequest(request)

        //this does not work, webview gets blank (white)
//        let path: String? = NSBundle.mainBundle().pathForResource("AceVetBolus", ofType: "html", inDirectory: "HTMLFiles")
//        if let unwrappedPath = path {
//            let requestURL = NSURL(string: unwrappedPath)
//            let request = NSURLRequest(URL: requestURL!)
//            
//            web1.loadRequest(request)
//        }
    }
}

这是我的文件夹结构:

这是我的html:

<!DOCTYPE html>
<html lang="en-US">
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>

<link rel="stylesheet" type="text/css" href="./css/product.css" />

</head>
<body>

<h2> Ace-Vet Bolus</h2> 
<div class="title">Composition:</div>
<p> Each Bolus contains Paracetamol BP 2000 mg. </p> 

<div class="title">Indication:</div>
<img src="./images/chicken_icon.png"/>
<p>Recovery from fever, pain (headache, earache, body ache, neuralgia,
 pain due to intestinal inflammation, rheumatoid fever, post vaccination pain, 
 post delivery pain, post operative pain) and tissue swollen resulting from trauma,
 injury, burn or any other infectious diseases of both Animal  & Poultry.</p> 

<div class="title">Dosage and administration:</div>
<p>Animal:  1 bolus / 130-140 kg body weight (15 mg/kg body weight), 3 times daily.
Poultry: 1 bolus should be mixed with 10 litre drinking water & administered 2 - 3 times daily.
Or as directed by the registered Veterinary physician.</p> 

<div class="title">Contraindication:</div>
<p>Ace-Vet<sup>&reg;</sup> Bolus should be used with caution in those animals which are renally or hepatically impaired.</p> 

<div class="title">Use in pregnancy and lactation:</div>
<p>Ace-Vet<sup>&reg;</sup> Bolus is safe in all stages of pregnancy and lactation.</p> 

<div class="title">Side effects:</div>
<p>Side effects of Ace-Vet<sup>&reg;</sup> Bolus are significantly mild, 
though hematological reactions have been reported. Pancreatitis,
 skin rashes and other allergic reactions occur occasionally.</p> 

<div class="title">Storage:</div>
<p>Protected from light, store in a cool and dry place. Keep out of reach of children.</p> 

<div class="title">Pack Size:</div>
<p>10x4 Bolus.</p> 

</body>

</html>

【问题讨论】:

  • 能否请您发布您的 HTML 代码。
  • HTML 没问题。在浏览器中正常运行。你还需要它吗?
  • 是的,如果可能的话,你能在这里发帖吗....因为我试图在我身边产生同样的问题

标签: ios swift uiwebview


【解决方案1】:

您的问题是应用程序包中没有文件夹imagecss,您只是复制了文件,没有文件夹结构

试试这个:

  1. 点击HTML Files群组并按删除,选择Remove references

  2. 转到 Finder 文件夹 HTML Files 并将整个文件夹拖到
    项目,选择Create folde references

  3. 一个蓝色文件夹应该出现在您的项目中

  4. 如果您在Build Phases>Copy Bundle Resources 下检查您的目标,您会看到整个文件夹结构现在已添加到您的包中

【讨论】:

    【解决方案2】:

    ./css/,./images/文件夹不用识别,直接用文件名即product.csschicken_icon.png

    只需尝试遵循 HTML。

    <!DOCTYPE html>
    <html lang="en-US">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <head>
    
    <link rel="stylesheet" type="text/css" href="product.css" />
    
    </head>
    <body>
    
    <h2> Ace-Vet Bolus</h2> 
    <div class="title">Composition:</div>
    <p> Each Bolus contains Paracetamol BP 2000 mg. </p> 
    
    <div class="title">Indication:</div>
    <img src="chicken_icon.png"/>
    <p>Recovery from fever, pain (headache, earache, body ache, neuralgia,
     pain due to intestinal inflammation, rheumatoid fever, post vaccination pain, 
     post delivery pain, post operative pain) and tissue swollen resulting from trauma,
     injury, burn or any other infectious diseases of both Animal  & Poultry.</p> 
    
    <div class="title">Dosage and administration:</div>
    <p>Animal:  1 bolus / 130-140 kg body weight (15 mg/kg body weight), 3 times daily.
    Poultry: 1 bolus should be mixed with 10 litre drinking water & administered 2 - 3 times daily.
    Or as directed by the registered Veterinary physician.</p> 
    
    <div class="title">Contraindication:</div>
    <p>Ace-Vet<sup>&reg;</sup> Bolus should be used with caution in those animals which are renally or hepatically impaired.</p> 
    
    <div class="title">Use in pregnancy and lactation:</div>
    <p>Ace-Vet<sup>&reg;</sup> Bolus is safe in all stages of pregnancy and lactation.</p> 
    
    <div class="title">Side effects:</div>
    <p>Side effects of Ace-Vet<sup>&reg;</sup> Bolus are significantly mild, 
    though hematological reactions have been reported. Pancreatitis,
     skin rashes and other allergic reactions occur occasionally.</p> 
    
    <div class="title">Storage:</div>
    <p>Protected from light, store in a cool and dry place. Keep out of reach of children.</p> 
    
    <div class="title">Pack Size:</div>
    <p>10x4 Bolus.</p> 
    
    </body>
    
    </html>
    

    已编辑

    • 删除 HTML 根文件夹引用。
    • 将您的html 文件夹拖到您的项目中。
    • 关注图片

    现在使用您的代码。

        override func viewDidLoad() {
                super.viewDidLoad()
                // Do any additional setup after loading the view, typically from a nib.
               if let file = self.fileName{
                print("got: "+file)
            }
    
            let url = NSBundle.mainBundle().URLForResource(self.fileName, withExtension:"html")
            let request = NSURLRequest(URL: url!)
            web1.loadRequest(request)
    }
    

    【讨论】:

    • 我必须使用相对路径
    • 不加载css或图片的
    • 为此您需要将物理文件插入捆绑包而不是references
    【解决方案3】:
      @IBOutlet weak var webView: UIWebView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            //load a file
            var testHTML = NSBundle.mainBundle().pathForResource("privacy", ofType: "html")
            var contents = NSString(contentsOfFile: testHTML!, encoding: NSUTF8StringEncoding, error: nil)
        var baseUrl = NSURL(fileURLWithPath: testHTML!) //for load css file
    
            webView.loadHTMLString(contents, baseURL: baseUrl)
    
        }
    

    【讨论】:

    • NSString(contentsOfFile: testHTML!, encoding: NSUTF8StringEncoding, error: nil) 给我do not match any available overloads
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多