how to open url with dynamic query string in WKWebView - ios

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.

Related

Play wkwebview audio trough ear speaker only

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)
}
}

WebKit View not opening certain URLs

I want the webkit view to display my domain webpage but it won't work. It works for regular known websites but not for my own domain. I am using the code below. Also I added this to the pList file but no joy. Thanks in advance.
pList
NSAppTransportSecurity
NSAllowsArbitraryLoads
Code:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView()
webView.navigationDelegate = self
view = webView
webView.frame = view.bounds
webView.navigationDelegate = self
let url = URL(string: "https://www.MYDOMAIN.co.uk")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
}
}
Try for this
class YourCalssviewController : UIViewController ,WKNavigationDelegate ,WKUIDelegate{ }
Write in viewDidLoad()
let url = NSURL(string: "https://www.MYDOMAIN.co.uk")
let request = NSURLRequest(url: url! as URL)
self.webView.load(request as URLRequest)
your code is working fine, the problem here with the URL itself, if you tried an other URL like "https://www.youtube.com/" it will work fine

How to load excel / pdf server url in WKWebView Swift4

I wanted to load the excel sheet url/pdf url (document url) in WKWebView. As I am trying to open that url from browser then it directly downloads that document. So I am confused whether 1] I have to directly load the url into WKWebView or 2] first download those files and then load that local path in WKWebView.
If I go with the 2nd option then will my document download every time?
Following is the code which I have tried but it shows nothing
import UIKit
import WebKit
class OpenReportsFullScreenVC: UIViewController,WKUIDelegate {
var webView : WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
//first way :- webView.load(URLRequest(url: URL(string: "http://ipaddress/projects/ourivf/newourivf/assets/uploads/patient_image/attorney_details - Copy.xls")!))
// second way
let filePathURLData = "http://ipaddress/projects/ourivf/newourivf/assets/uploads/patient_image/attorney_details - Copy.xls"
let fileURL = URL(fileURLWithPath: filePathURLData )
webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL)
}
override func loadView() {
//initialise webview
let webViewConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webViewConfiguration)
webView.uiDelegate = self
view = webView
}
}
Try with this code
import UIKit
import WebKit
class ViewController: UIViewController , **WKNavigationDelegate**{
var webView : WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let myBlog = "your URl"
let url = NSURL(string: myBlog)!
let request = NSURLRequest(url: url as URL)
webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
webView.load(request as URLRequest)
self.view.addSubview(webView)
self.view.sendSubview(toBack: webView)
}
}
File path cannot have blank characters like in your example.

iOS WKWebView Swift javascript enable

I'm trying to develop a simple app to browse my website. However my website contains some javascript and it doesn't successfully show my website.
In the past develop with android the same app and had to activate like this:
webSettings.setJavaScriptEnabled(true);
This currently my code I just have missing the option to enable javascript if somebody can help will really appreciate
import WebKit
class ViewController: UIViewController {
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://132.148.136.31:8082")
let urlRequest = URLRequest(url: url!)
webView.load(urlRequest)
}
}
Working with WKWebView is better to create it from code. And you can set javaScriptEnabled to configuration:
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
let webview = WKWebView(frame: .zero, configuration: configuration)
Update. This is WKWebView in view controller class:
import UIKit
import WebKit
class ViewController: UIViewController {
private var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
webView = WKWebView(frame: view.bounds, configuration: configuration)
view.addSubview(webView)
}
}
For those who are building for iOS 14 with Swift 5 and Xcode 12, it has changed to the following ...
webView.configuration.defaultWebpagePreferences.allowsContentJavaScript = true
You are using WKWebView storyboard view so you can directly access the configuration. So use preferences under your WKWebView configuration.
import WebKit
class ViewController: UIViewController {
#IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://132.148.136.31:8082")
let urlRequest = URLRequest(url: url!)
// enable JS
webView.configuration.preferences.javaScriptEnabled = true
webView.load(urlRequest)
}
}
The above answer is right you need to set this value as true to enable JavaScript.
webView.configuration.preferences.javaScriptEnabled = true
But if the JS scripts are from a non-HTTPS source then you need to allow "App Transport Security's Arbitrary Loads".
Here is how to fix that:
Navigate to Project Settings/Target/[your app's target]/Info.
Check whether there is a dictionary called App Transport Security Settings. If not, create it.
Inside it, create a boolean value called Allow Arbitrary Loads (if it's not there yet) and set it to YES.
IOS 15.5
add this
webView.configuration.defaultWebpagePreferences.allowsContentJavaScript = true
try this code :
override func viewDidLoad() {
super.viewDidLoad()
configureWebView()
currentWebView.load(requestURL)
}
private func configureWebView() {
webView.navigationDelegate = self
webView.uiDelegate = self
webView.allowsBackForwardNavigationGestures = true
/// javaScript 사용 여부
if #available(iOS 14, *) {
let preferences = WKWebpagePreferences()
preferences.allowsContentJavaScript = true
webView.configuration.defaultWebpagePreferences = preferences
}
else {
webView.configuration.preferences.javaScriptEnabled = true
/// user interaction없이 윈도우 창을 열 수 있는지 여부를 나타냄. iOS에서는 기본값이 false이다.
webView.configuration.preferences.javaScriptCanOpenWindowsAutomatically = true
}
}
func createNewWebView(_ config: WKWebViewConfiguration) -> WKWebView {
/// autoLayout 되어 있는 webView와 frame을 같게 한다.
let newWebView = WKWebView(frame: webView.frame,
configuration: config)
newWebView.navigationDelegate = self
newWebView.uiDelegate = self
newWebView.allowsBackForwardNavigationGestures = true
view.addSubview(newWebView)
popupWebViews.append(newWebView)
return newWebView
}

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.

Resources