I'm playing around with Swift to learn it and right now I'm having trouble getting the text from a UISearchBar
Right now my code looks like this:
import UIKit
class SecondViewController: UIViewController {
#IBOutlet var myWebView : UIWebView
#IBOutlet var adressbar: UISearchBar
override func viewDidLoad() {
super.viewDidLoad()
adressbar.showsScopeBar = true
var url = NSURL(string: "http://google.com")
var request = NSURLRequest(URL: url)
myWebView.scalesPageToFit = true
myWebView.loadRequest(request)
}
func searchBarSearchButtonClicked( searchBar: UISearchBar!) {
var url = NSURL(string: adressbar.text)
var request = NSURLRequest(URL: url)
myWebView.scalesPageToFit = true
myWebView.loadRequest(request)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
In viewDidLoad you must set the delegate of the search bar to be receiving searchBarSearchButtonClicked from it:
import UIKit
class SecondViewController: UIViewController, UISearchBarDelegate
{
#IBOutlet var myWebView : UIWebView
#IBOutlet var adressbar: UISearchBar
override func viewDidLoad()
{
super.viewDidLoad()
adressbar.showsScopeBar = true
adressbar.delegate = self
var url = NSURL(string: "http://google.com")
var request = NSURLRequest(URL: url)
myWebView.scalesPageToFit = true
myWebView.loadRequest(request)
}
func searchBarSearchButtonClicked( searchBar: UISearchBar!)
{
var url = NSURL(string: searchBar.text)
var request = NSURLRequest(URL: url)
myWebView.scalesPageToFit = true
myWebView.loadRequest(request)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
}
Simple (non-specific) answer for those arriving here via search:
If your outlet name for your UISearchBar is 'searchBar'...
#IBOutlet weak var searchBar: UISearchBar!
Then the text entered by the user in UISearchBar is called...
searchBar.text
Two common things Swift coders forget:
1. Make sure to add UISearchBarDelegate class...
ViewController: UIViewController, UISearchBarDelegate
Don't forget to identify your view class as the delegate...
Inside viewDidLoad you could write...
searchBar.delegate = self
If you forget #1 above you won't be able to auto-complete the methods you'll need to implement like...
func searchBarSearchButtonClicked(_ seachBar: UISearchBar) { your code }
... which is given to you by the delegate protocol.
1.First thing is to conform to the UISearchbarDelegate.
2.Set the search bar delegate to self.
You can find more on this here Swift iOS tutorial : Implementing search using UISearchBar and UISearchBarDelegate
Related
I am trying to get an animated gif to play in the background, and have some labels and buttons appear on top. This code runs and shows the animated gif, however it does not show the buttons or label text.
import Foundation
import UIKit
import WebKit
class LoginViewController: UIViewController {
#IBOutlet var containerView: UIView! = nil
var webViewBG: WKWebView?
#IBOutlet weak var loginButton: UIButton!
#IBOutlet weak var signUpButton: UIButton!
override func loadView() {
super.loadView()
self.webViewBG = WKWebView()
self.view = self.webViewBG
}
override func viewDidLoad() {
super.viewDidLoad()
let htmlPath = Bundle.main.path(forResource: "WebViewContent", ofType: "html")
let htmlURL = URL(fileURLWithPath: htmlPath!)
let html = try? Data(contentsOf: htmlURL)
var req = NSURLRequest(url:htmlURL)
self.webViewBG!.load(req as URLRequest)
webViewBG?.isUserInteractionEnabled = false;
self.loginButton.layer.borderColor = UIColor.white.cgColor
self.loginButton.layer.borderWidth = 2
self.signUpButton.layer.borderColor = UIColor.white.cgColor
self.signUpButton.layer.borderWidth = 2
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override var preferredStatusBarStyle : UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}
}
It looks like this line overrides your view controller's view where the buttons are:
self.view = self.webViewBG
Try
self.view.addSubview(self.webViewBG)
You might also need to tweak the z position so that the webView is located 'behind' the buttons, like this:
self.webViewBG.layer.zPosition = 0.5
Higher zPosition means the view stands out more 'towards' the user
Using swift3 with Xcode8
Below is mu view controller.swift
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var YahooWebview: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
YahooWebview.delegate = self
super.viewDidLoad()
let YURL = URL(string: "http://www.yahoo.com")
let YURLRequest = URLRequest(url: YURL!)
YahooWebview.loadRequest(YURLRequest)
}
func webViewDidStartLoad(YahooWebview: UIWebView) {
print("show indicator")
activity.startAnimating()
}
func webViewDidFinishLoad(YahooWebview: UIWebView) {
activity.stopAnimating()
}
}
From my log in Xcode, it print "show indicator" then stop and then show something like the picture below.
Seems like something wrong with code below
activity.startAnimating()
Can anyone help?
Try below code for load site in web view
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var YahooWebview: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
let YURL = URL(string: "http://www.yahoo.com")
YahooWebview.delegate = self
let YURLRequest = URLRequest(url: YURL!)
YahooWebview.loadRequest(YURLRequest)
}
func webViewDidStartLoad(_ webView: UIWebView) {
activity.startAnimating()
}
func webViewDidFinishLoad(_ webView: UIWebView) {
activity.stopAnimating()
}
}
How i can login many accounts of same site in different webView. For example i have Tab Bar Controller That contains three View Controllers and each View Controllers contain webView. And for example i embed stackoverflow url for webView in every class. How user can login to different accounts at the same time using these three webView? I've tried this but i just login one user at a time. Can any one please tell me how this is possible in (Swift or Objective-C)??
Thanks
class FirstViewController: UIViewController , UIWebViewDelegate{
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
webView.delegate = self
let requestURL = NSURL(string: "http://stackoverflow.com")
let request = NSURLRequest(URL: requestURL!)
activityIndicator.hidesWhenStopped = true
activityIndicator.startAnimating()
webView.loadRequest(request)
}
func webViewDidFinishLoad(webView: UIWebView) {
activityIndicator.stopAnimating()
}
}
class SecondViewController: UIViewController, UIWebViewDelegate{
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
webView.delegate = self
let requestURL = NSURL(string: "http://stackoverflow.com")
let request = NSURLRequest(URL: requestURL!)
activityIndicator.hidesWhenStopped = true
activityIndicator.startAnimating()
webView.loadRequest(request)
}
func webViewDidFinishLoad(webView: UIWebView) {
activityIndicator.stopAnimating()
}
}
I have loaded a webpage to xcode WebView. But only the upper portion of the page is loaded. I can not scroll down to the bottom of the page. Same fact for pdf. Only upper 2 pages can be scrolled. What can i do ?Here is my code.
Thanks in advance.
import UIKit
class ViewController: UIViewController {
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
var URL = NSURL(string: "http://www.archetapp.com")
webView.loadRequest(NSURLRequest(URL: URL!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//for pdf
import UIKit
class ViewController: UIViewController {
#IBOutlet var webViews: UIWebView!
var path = ""
override func viewDidLoad() {
super.viewDidLoad()
path = NSBundle.mainBundle().pathForResource("ibook", ofType: "pdf")!
let url = NSURL.fileURLWithPath(path)
webViews.scalesPageToFit = true
webViews.scrollView.scrollEnabled = true
webViews.userInteractionEnabled = true
self.webViews.loadRequest(NSURLRequest(URL: url!))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Try this!
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var webView : UIWebView
override func viewDidLoad() {
super.viewDidLoad()
//load initial
path = NSBundle.mainBundle().pathForResource("ibook", ofType: "pdf")!
let url = NSURL.fileURLWithPath(path)
var req = NSURLRequest(URL : url)
webView.delegate = self // <---
webView.loadRequest(req)
}
func webViewDidStartLoad(webView : UIWebView) {
//UIApplication.sharedApplication().networkActivityIndicatorVisible = true
println("webViewDidStartLoad")
}
func webViewDidFinishLoad(webView : UIWebView) {
//UIApplication.sharedApplication().networkActivityIndicatorVisible = false
webViews.scalesPageToFit = true
webViews.scrollView.scrollEnabled = true
webViews.userInteractionEnabled = true
println("webViewDidFinishLoad")
}
}
I had the same issue just today and at least in my case it was caused by having the "Scale Pages to Fit" option checked in the WebView properties. I'm assuming Carlos' reply regarding the zoom scale fixes it anyways but I didn't need the option enabled in the first place so that was my easy fix.
How do I dissmiss keyboard when I touch outside UISearchBar using swift code. It works when I'm on the google search page but when I'm inside of a website and I touch the uisearchbar then when I tap outside of it the keyboard does not disappear.
Here my code so far.
import UIKit
import WebKit
class ViewController: UIViewController, UISearchBarDelegate {
#IBOutlet weak var searchBar: UISearchBar!
#IBOutlet weak var webView: UIWebView!
func searchBarSearchButtonClicked(searchBar: UISearchBar!) {
searchBar.resignFirstResponder()
var text = searchBar.text
text = text.stringByReplacingOccurrencesOfString(" ", withString: "+");
var url = NSURL(string: "http://google.com/search?q=".stringByAppendingString(text));
var req = NSURLRequest(URL:url)
self.webView!.loadRequest(req)
}
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "http://google.com")
let request = NSURLRequest(URL: url)
webView.loadRequest(request)
self.searchBar.delegate = self
}
}
You can use touch event to dissmiss keyboard
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
self.view.endEditing(true)
}