Play wkwebview audio trough ear speaker only - ios

I am trying to develop an application that uses wkwebview and WebRTC to make voice calls, the app loads a wkwebview with the web page that uses webrtc and when it plays the audio of the calls it does so through the main speaker of the Iphone instead of playing the audio through the earspeaker as a normal call would.
I think it recognizes the audio as multimedia and not as the audio of a call.
How can I fix this issue?
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
#IBOutlet weak var showWebview: WKWebView!
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://mywebsite.com)
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}

Related

how to open url with dynamic query string in WKWebView

I have a button on my website which has href https://live.example.net/?dynamic_encypted_query-string, So I wanted to open this url in my webview of ios app. Currently I'm building a web app through capacitor framework where I wrote a code to my WebViewController.swift file
class WebViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://live.example.net/?*")
let myRequest = URLRequest(url: url!)
webView.load(myRequest)
// Do any additional setup after loading the view.
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
}
This opens the domain(https://live.example.net) in the web view but i wanted to open it with the query string data.
let url = URL(string: "https://live.example.net/?*")
I think this doesn't work to fetch the query string data as well.

Error when tapping on anything inside loaded page in WKWebview

I have an issue with WKWebview. The pages load without any problems, but whenever I tap on any button on website, slide photos or accept cookies policy I get the following error:
2019-10-14 11:27:02.933458+0200 AppTitle[2592:447958] [general] Connection to daemon was invalidated
Does anyone know what may be the issue or what am I missing? This error causes further problems inside the app. Here is my code:
import UIKit
import WebKit
import TinyConstraints
class DetailsVC: UIViewController, WKNavigationDelegate, WKUIDelegate {
let myUrl = URL(string: "https://google.pl/")
var webView: WKWebView = {
let webview = WKWebView()
return webview
}()
func addWebView() {
view.addSubview(webView)
webView.edgesToSuperview(insets: .top(40) + .right(0) + .left(0), usingSafeArea: true)
webView.navigationDelegate = self
webView.uiDelegate = self
webView.load(URLRequest(url: self.myUrl!))
}
override func viewDidLoad() {
super.viewDidLoad()
// Add webview
addWebView()
}
}

WebView is not loading for specific URL in Xcode 9 simulator

I am trying to load a URL using webView in swift 4. I tried https://www.google.co.in and it works fine. and the mobile site for the specific URL works fine with android.
But when I tried this in the simulator, it is loading forever.
my code is below:
import UIKit
class WebViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView: UIWebView!
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
if let url = URL(string: "https://www.myurl.com") {
let request = URLRequest(url: url)
webView.loadRequest(request)
}
}
public func webViewDidStartLoad(_ webView: UIWebView)
{
activityIndicator.center = CGPoint(x: self.view.bounds.size.width/2, y: self.view.bounds.size.height/2)
self.view.addSubview(activityIndicator)
activityIndicator.color = UIColor.red
self.activityIndicator.startAnimating()
}
public func webViewDidFinishLoad(_ webView: UIWebView)
{
self.activityIndicator.stopAnimating()
}
}
I don't think that my code is wrong. but I was wondering if I can do anything to make it work on simulator.
Thanks
I just made a small project with your code and no problem in the simulator, https://www.google.co.in loaded just fine.
I noticed you mention you are using Swift 4, I think you should consider using WebKit View (WKWebView) since UIWebView is deprecated. In Apple's documentation you can see how to implement it, it's pretty straight forward.
If your certificate is not trusted you must add to Info.plist App Transport Security Settings, Allow Arbitrary Loads to YES. (This is not recommended).
The code is really simple, give it a try:
import UIKit
import WebKit
class WebViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
#IBOutlet weak var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let myURL = URL(string: "https://www.myurl.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: #escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
if let serverTrust = challenge.protectionSpace.serverTrust {
completionHandler(.useCredential, URLCredential(trust: serverTrust))
}
}
}

WKWebView show white screen when loading HTTPS URL of Angular App

apologies for the simple question:
I am following Apple's guide on WKWebView to the dot. Same code and everything.
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://amerifit.afvusa.com/MenuMain.aspx?a=NTgzMjEyMTU3")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}}
Source
This is the URL I am trying to link to: https://amerifit.afvusa.com/MenuMain.aspx?a=NTgzMjEyMTU3
Except that I am getting a white screen when the viewcontroller tries to load the page.
Any reasons why? The code works fine for Google's homepage and this URL is secure so that isn't the issue.
Also, the website is not mine and I cannot edit it's files.

Separate cookie storage for two (UIWebView or WKWebView)

I want to 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 can just login one user at a time.
I have found that i need to create separate cookie for every UIWebView, but mostly answers are in objective-c and not the proper answer i want. For example (First Second Third)
Can any one please tell me how i can do it?
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()
}
}
Thanks
You can do this with WKWebView by using different instances of WKWebSiteDataStore:
let configuration1 = WKWebViewConfiguration()
configuration1.websiteDataStore = WKWebsiteDataStore.nonPersistent()
self.webView1 = WKWebView(frame: CGRect.zero, configuration: configuration1)
self.view.addSubview(self.webView1)
let configuration2 = WKWebViewConfiguration()
configuration2.websiteDataStore = WKWebsiteDataStore.nonPersistent()
self.webView2 = WKWebView(frame: CGRect.zero, configuration: configuration2)
Unfortunately, you will loose webView data (such as cookies, cache, etc) after app restart, because non-persistent WKWebsiteDataStore can't be saved to disk (you could notice that WKWebsiteDataStore implements NSCoding, but it doesn't work for non-persistent stores).

Resources