My UIsearch bar does not work/send request to other sites - ios

I'm still new to this but I'm confused on why my search bar does not work any time i put in a url address. I'm trying to make a video downloader app.
import UIKit
import AFNetworking
import Alamofire
class ViewController: UIViewController {
#IBOutlet var Webview: UIWebView!
#IBOutlet var SearchBar: UISearchBar!
override func viewDidLoad() {
if let url = URL(string: "https://www.google.com"){
let requestObj = URLRequest(url: url)
Webview.loadRequest(requestObj)
}
SearchBar.text = "http://"
}
func searchBarSearchButtonClicked(searchbar: UISearchBar) {
searchbar.resignFirstResponder()
let text = SearchBar.text
let url = NSURL(string: text!)
let request:URLRequest = URLRequest(url: url! as URL)
//let request = NSURLRequest(URL: url! as URL)
Webview.loadRequest(request)
}
}

Add UISearchBarDelegate protocol methods to fire a search bar protocol method;
class ViewController: UIViewController, UISearchBarDelegate, UIWebViewDelegate {
override func viewDidLoad() {
SearchBar.delegate = self
webview.delegate = self // connect your webview in storyboard
}
// MARK: - UISearchView Delegates
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
}
func searchBarSearchButtonClicked(searchbar: UISearchBar) {
// call your function here
let searchText = searchbar.text!
let url = URL (string: searchText)
let requestObj = URLRequest(url: url!)
webView.loadRequest(requestObj)
}
}

Related

How to Handle Image uploading in WKWebview?

I have use webkit to show url on my app.
when I click on image button(green button in ), it shows
camera or photo library.
issue is, when click Use photo, the webkit
refresh automatically instead showing on page
I have already give photo usage, media, camera permission in plist file
my code is
import UIKit
import WebKit
import Alamofire
class ContinueViewController: UIViewController, WKUIDelegate {
#IBOutlet weak var activityView: UIActivityIndicatorView!
#IBOutlet weak var webview: WKWebView!
func getPostString(params:[String:Any]) -> String{
var data = [String]()
for(key, value) in params
{
data.append(key + "=\(value)")
}
return data.map { String($0) }.joined(separator: "&")
}
var url: URL?
var traderCategoryId = 0, tradeSkillId = 0
override func viewDidLoad() {
super.viewDidLoad()
webview.navigationDelegate = self
self.navigationController?.navigationBar.isHidden = false
webview.uiDelegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
activityView.startAnimating()
guard let url = self.url else { return }
var req = URLRequest(url: url)
let params = ["id" : id,"trader_skills" : tradeSkillId]
let postString = self.getPostString(params: params)
req.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
req.httpMethod = "POST"
req.httpBody = postString.data(using: .utf8)
self.webview.load(req)
}
}
extension ContinueViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
activityView.stopAnimating()
}
}
extension UIImagePickerController {
open override func viewDidLoad() {
super.viewDidLoad()
// self.modalPresentationStyle = .fullScreen
}
}
issue is, when click Use photo, the webkit refresh automatically instead showing on page
Because you self.webview.load(req) in
override func viewWillAppear(_ animated: Bool) {
shows camera or photo library, then return to this page
func viewWillAppear called again
self.webview.load(req) called again
do it simple ,
put self.webview.load(req) the relevant in
override func viewDidLoad() {

Two video players appear in UIWebview

Why when I play the video, there are two overlapping video players. Can someone tell me why and how to solve it?
import UIKit
class ViewController: UIViewController,UIWebViewDelegate,UIScrollViewDelegate {
#IBOutlet weak var webview: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
if let mediaURL:URL = URL(string: "https://twitter.com/") {
let request:URLRequest = URLRequest(url: mediaURL)
webview.allowsInlineMediaPlayback = false
DispatchQueue.main.async {
self.webview.loadRequest(request)
}
}
}
func webViewDidFinishLoad(_ webView: UIWebView) {
}
}
This is the video image I took

Login details are not saving in UIWebView in Xcode 9.4?

I want to ask that I am making a UIWebView App in Xcode 9.4?
I didn't add any line of code in AppDelegate. I want to ask that how to implement save session time to keep user logged in on any website. Even if a user closes the app from task manager also...
This is my code in View Controller
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activity: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
let swipeLeftRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(recognizer:)))
let swipeRightRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(recognizer:)))
swipeLeftRecognizer.direction = .left
swipeRightRecognizer.direction = .right
webView.addGestureRecognizer(swipeLeftRecognizer)
webView.addGestureRecognizer(swipeRightRecognizer)
loadAddress()
}
#objc private func handleSwipe(recognizer: UISwipeGestureRecognizer) {
if (recognizer.direction == .left) {
if webView.canGoForward {
webView.goForward()
}
}
if (recognizer.direction == .right) {
if webView.canGoBack {
webView.goBack()
}
}
}
func loadAddress() {
let requestURL = NSURL(string: "https://uims.cuchd.in/uims/")
let request = NSURLRequest(url: requestURL! as URL)
webView.loadRequest(request as URLRequest)
}
func webViewDidStartLoad(_ :UIWebView) {
activity.startAnimating()
NSLog("website is loading")
}
func webViewDidFinishLoad(_ :UIWebView){
activity.stopAnimating()
NSLog("website finished loading")
}
}
Please anyone, help me
Thanks in advance
Set httpShouldHandleCookies = true
let requestURL = NSURL(string: "https://uims.cuchd.in/uims/")
let request = NSMutableURLRequest(url: requestURL! as URL)
request.httpShouldHandleCookies = true
Even you should use URL and URLRequest directly instead of converting NSURLRequest to URLRequest & NSURL to URL
let url = URL(string: "https://uims.cuchd.in/uims/")
var request = URLRequest(url: url!)
request.httpShouldHandleCookies = true

Web view: Returning nil at loadRequest while true in URLRequest

Web view: Returning nil at loadRequest while true in URLRequest
I don't know where is wrong.
import UIKit
class DetailViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var designatedWebView: UIWebView!
var webview: UIWebView!
var testString: String!
override func viewDidLoad() {
super.viewDidLoad()
designatedWebView = webview
testString = "http://www.apple.com"
let baseURL = URL(string: testString)
let baseURLRequest = URLRequest(url: baseURL!) //this prints true
designatedWebView.loadRequest(baseURLRequest) //while this prints nil
}
}
Replace your code with this. This is the normal and easiest way to load a website in UIWebView.
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let url = URL(string: "http://www.apple.com")
let request = URLRequest(url: url!)
webView.loadRequest(request)
webView.scalesPageToFit = true
}
}
If you are not connecting the webView from your Main.storyboard, then add this line of code with the above code in your viewDidLoad() function.
webView.delegate = self

Search Bar & Web View

I'm trying to build a simple web browser. The web browser itself works pretty well but when I try to search a url in the search bar I can't return on the keyboard to open the website. How can I fix this?
#IBOutlet weak var searchBar: UISearchBar!
#IBOutlet weak var webView: UIWebView!
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
let text = searchBar.text
let urlSB = NSURL(string: text!)
let reqSB = NSURLRequest(url:urlSB! as URL)
self.webView!.loadRequest(reqSB as URLRequest)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL(string: "https://www.google.com")
let req = NSURLRequest(url:url! as URL)
self.webView!.loadRequest(req as URLRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
I write a demo for you:
The result:
Step 1:
Step 2: Type in the url you want to access.
Step 3: The result, the webView load the url you typed in the searchBar:
The code is below:
import UIKit
class ViewController: UIViewController, UISearchBarDelegate {
#IBOutlet weak var searchBar: UISearchBar!
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://www.baidu.com")
let req = NSURLRequest(url:url! as URL)
self.webView!.loadRequest(req as URLRequest)
searchBar.delegate = self
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
let text = searchBar.text
let urlSB = NSURL(string: text!)
let reqSB = NSURLRequest(url:urlSB! as URL)
self.webView!.loadRequest(reqSB as URLRequest)
}
// MARK: - searchbar delegate
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
let urlStr = "https://" + searchBar.text!
let url = NSURL(string: urlStr)
let req = NSURLRequest(url:url! as URL)
self.webView!.loadRequest(req as URLRequest)
}
If your webView did not load the url, and report:
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
You can go to info.plist to open the ATS like below, or search the SO to find the detail method:
You need to implement the delegate methods for UISearchBar and handle everything from there. Also don't forget to set the delegate to the viewcontroller.

Resources