我正在使用Firebase存储下载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: "") + "%"
            }
    }

}

请不要说我目前遇到这个代码的问题,但这应该在模拟器上工作。

最佳答案

首先将url转换为字符串,然后对该字符串进行正则化以查找要查找的特定单词

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) {}

关于ios - 如何在UIWebView中搜索文本?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40244603/

10-16 14:07