One feature that I really wanted to add to Refining Fire was the ability to read the entire chapter for a randomly chosen Bible verse. So for instance, if the verse was Psalm 23.4, a user could read all of Psalm 23. I decided to use a UIWebView to display the chapter content from a popular Bible-reading website.
Sometimes the website would take a moment to load and at first glance, it seemed like the feature was broken. I knew I needed to add some kind of spinner or loading bar but wasn’t sure how to tie a UIActivityIndicatorView to the loading of a web page. After searching Stack Overflow, I found the simplest solution. Here’s what it looks like in Swift after declaring a UIActivityIndicatorView called “spinner” and a UIWebView called “webView”:
override func viewDidLoad() {
super.viewDidLoad()
spinner.hidesWhenStopped = true
webView.delegate = self
let url = NSURL(string: urlString)
let urlRequest = NSURLRequest(URL: url!)
webView.loadRequest(urlRequest)
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
spinner.startAnimating()
return true
}
func webViewDidFinishLoad(webView: UIWebView) {
spinner.stopAnimating()
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError) {
spinner.stopAnimating()
}
Don’t forget to add UIWebViewDelegate to the class definition!
Note: This is the fifth entry in a series of posts about writing my first iOS app. The app is currently in review, and until it is rejected or approved, I plan to write something every day about what I’ve learned.