-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebViewController.swift
77 lines (48 loc) · 1.69 KB
/
WebViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import UIKit
class WebViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var infoLabel: UILabel!
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func didGobutton(sender: AnyObject) {
if textField.text == "" {
return
}
guard let text: String = textField.text else {
return
}
infoLabel.hidden = true
if text.rangeOfString(".") != nil {
var finalString :String = text.lowercaseString
if finalString.rangeOfString("http://") == nil {
finalString = "http://\(finalString)"
}
self.loadURL(finalString)
} else {
let searchString = text.stringByReplacingOccurrencesOfString(" ", withString: "+")
let finalString = "https://www.google.com/search?q=\(searchString)"
self.loadURL(finalString)
}
}
func loadURL(str:String) {
let url = NSURL(string: str)!
let request = NSURLRequest(URL: url)
webView.loadRequest(request)
}
@IBAction func goBack(sender: AnyObject) {
webView.goBack()
}
@IBAction func goForward(sender: AnyObject) {
webView.goForward()
}
}