Two video players appear in UIWebview - ios

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

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

Show loading activity Webkit

I want to show activity when pages are loading.
It's probably simple but I'm a beginner..
Tried a couple of things but can't really tell you what I've tried, I don't understand it...
import UIKit
import WebKit
class SecondViewController: UIViewController {
#IBOutlet weak var myWebView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://www.zzzway.com/takephotoapp.php")
let request = URLRequest(url: url!)
myWebView.load(request)
}
}
I want to get a loading animation or any visual to show the user it's loading.
Use UIActivityIndicatorView as below,
class SecondViewController: UIViewController {
private var activityIndicatorView: UIActivityIndicatorView = {
let activityIndicator = UIActivityIndicatorView()
activityIndicator.hidesWhenStopped = true
activityIndicator.color = UIColor.darkGray
return activityIndicator
}()
#IBOutlet weak var myWebView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.myWebView.navigationDelegate = self
self.activityIndicatorView.center = self.myWebView.center
self.view.addSubview(self.activityIndicatorView)
let url = URL(string: "https://www.zzzway.com/takephotoapp.php")
let request = URLRequest(url: url!)
myWebView.load(request)
}
}
extension SecondViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
self.activityIndicatorView.startAnimating()
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.activityIndicatorView.stopAnimating()
}
}

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

Can't detect webViewDidFinishLoad event

I'm trying to make an IOS app to show a web site in a webview.
That's ok it's working. Now I'm trying to detect the end of the page load.
That's my code :
class ViewController: UIViewController, UIWebViewDelegate{
#IBOutlet weak var my_web_view: UIWebView!
#IBOutlet weak var my_loading_view: UIView!
override func viewDidLoad() {
super.viewDidLoad()
NSLog("----------------> START");
my_web_view.scrollView.bounces = false;
my_web_view.scrollView.isScrollEnabled = true;
let url = URL(string: "https://www.sortirauhavre.com/");
let request = URLRequest(url: url!);
my_web_view.delegate = self;
my_web_view.loadRequest(request);
}
func webViewDidFinishLoad(webView: UIWebView!){
NSLog("----------------> STOP");
}
}
In my console I can read "START" but never "STOP". Xcode don't say me I made an error, do you know what am I doing wrong please ?
yes , the problem you are not connected the delegate of your webview to current class
and call the delegate as
func webViewDidFinishLoad(_ webView: UIWebView){
NSLog("----------------> STOP");
}
you get the output as

iOS Swift : can't Retrieve current url loaded in UIwebView

i am working in webview application iOS swift :
problem i am facing is i want to get each url which webview displays :
upon urls i have to perform some if else checks . but i could not get the urls which webview loads.
webview is displaying fine results and loading urls upon clicking . i want to fetch all urls which webview navigates on..
import Foundation
import UIKit
class seconVC: UIViewController {
var toPass:String!
var touser:String!
var toPassing:String!
#IBOutlet weak var webV: UIWebView!
#IBOutlet weak var L_back: UIBarButtonItem!
#IBOutlet weak var R_frwrd: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var domurl = toPass;
var username = touser;
var passwing = toPassing;
var sendurl = stringA+username+"/"+passwing;
//exchange login with auth/username/password
println(sendurl);
let url = NSURL (string: sendurl);
let requestObj = NSURLRequest(URL: url!);
webV.userInteractionEnabled = true
webV.loadRequest(requestObj);
}
#IBAction func L_back(sender: UIBarButtonItem) {
if (webV.canGoBack){
webV.goBack()
}
}
#IBAction func R_frwrd(sender: UIBarButtonItem) {
if (webV.canGoForward){
webV.goForward()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
all i want is to get string of current url and navigation buttons for webview :please help ? i referred all the internet no solution is working in my case .
There are couple of UIWebViewDelegate such as shouldStartLoadWithRequest or webViewDidStartLoad or webViewDidFinishLoad or didFailLoadWithError that help you to accomplish your goal. If you want to perform operation after view did finished then implement this delegate method
Swift 3
if let currentURL = webView.request?.url?.absoluteString{
print(currentURL)
}
as it mentioned here.
option-1
let currentURL = webV.request.URL.absoluteString
option-2
get URL in delegate method
func webViewDidFinishLoad(webView: UIWebView){
print(WebView.request?.mainDocumentURL)
}
option-3
func webViewDidFinishLoad(webView: UIWebView) {
let currentURL = webView.request().URL()
print("\(currentURL.description)")
}
or use like
func webViewDidFinishLoad(webView: UIWebView){
let currentURL = webView.request?.URL
}
option-4
func webViewDidFinishLoad(webView: UIWebView) {
let currentURL: = webView.stringByEvaluatingJavaScriptFromString("window.location.href")!
print("\(currentURL)")
}
option-5
func webView(webview: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
let currentURL = request.URL.absoluteString
print("\(currentURL)")
}
option-6
func webViewDidFinishLoad(WebVie: UIWebView) {
let currentURL = WebVie.request.mainDocumentURL
print("\(currentURL)")
}
First you have to define the delegate for WebView class. "WebView.delegate = self". Add this code beside one of the function above:
override func viewDidLoad() {
super.viewDidLoad()
myWebView.delegate = self }
and for exmaple:
func webViewDidFinishLoad(_ webView: UIWebView) {
if let currentURL = WebView.request?.url?.absoluteString{
print(currentURL)
}
}

Resources