如何在UIWebView中搜索文本?

时间:2016-10-25 16:00:20

标签: ios swift uiwebview document

我正在使用Firebase Storage下载word文档,因此我可以使用UIWebView在我的应用程序中显示它。这是显示文档的代码:

@IBOutlet var Web_View: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    let Bulletin = FIRStorage.storage()

    let Today = NSDateFormatter()
    Today.dateStyle = .LongStyle
    Today.timeStyle = .NoStyle

    let Ref_Bulletin = Bulletin.referenceForURL("gs://app.appspot.com/Bulletin/\(Today.stringFromDate(NSDate())).docx")

    Ref_Bulletin.downloadURLWithCompletion { (URL, error) -> Void in
        if (error != nil) {
            print("Error Loading Today's Bulletin")
        } else {
            self.Web_View.loadRequest(NSURLRequest(URL: URL!))
            self.Web_View.backgroundColor = UIColor.brownColor()
        }
    }
}

现在我需要做的是搜索本文档中的某些单词,但我不知道该怎么做。

更新

我现在将文件保存到本地存储:

var Bulletin = FIRStorage.storage()

override func viewDidLoad() {
    super.viewDidLoad()

    let Date = NSDateFormatter()
    Date.dateStyle = .LongStyle
    Date.timeStyle = .NoStyle

    let Ref_Bulletin = Bulletin.referenceForURL("gs://app.appspot.com/Bulletin/\(Date.stringFromDate(NSDate())).docx")
    let localURL: NSURL! = NSURL(string: "file:///tmp/Bulletin/Today.docx")

    var error:NSError?

    let downloadTask = Ref_Bulletin.writeToFile(localURL) { (URL, error) -> Void in
            if (error != nil) {
                print("Error - " + error.debugDescription)
            } else {
                self.Web_View.loadRequest(NSURLRequest(URL: localURL))
            }
        }

        downloadTask.observeStatus(.Progress) { snapshot in
            if let progress = snapshot.progress {
                let percentComplete = 100 * Double(progress.completedUnitCount) / Double(progress.totalUnitCount)
                self.Progress_Label.text = String(percentComplete.roundToPlaces(0)).stringByReplacingOccurrencesOfString(".0", withString: "") + "%"
            }
    }

}

请注意,我目前遇到此代码的问题,但应该在模拟器上运行。

1 个答案:

答案 0 :(得分:1)

首先将您的网址转换为字符串,然后使用该字符串来查找要查找的特定字词

Ref_Bulletin.downloadURLWithCompletion { (URL, error) -> Void in
    if (error != nil) {
        print("Error Loading Today's Bulletin")
    } else {
        let string = try String(contentsOfURL: URL!)
        someRegexFunction(string)
        self.Web_View.loadRequest(NSURLRequest(URL: URL!))
        self.Web_View.backgroundColor = UIColor.brownColor()
    }
}


func someRegexFunction(string:String) {}