Changing the requests from HTTPS to HTTP from WKWebview? - ios

My requirement is to redirecting the HTTPS request to HTTP from WKWebView.
But the request initiated inside the webview(Ajax call) are not being captured.
After that, the requests being made from that webpage are not captured.
Please share your ideas. Much appreciated.

You would need to implement a WebPolicyDelegate with a decidePolicyForNavigationAction and then load the page yourself without https. This should work, but I haven't tested it.
Have a look at this post for some insides, on UIWebView it would have worked with shouldStartLoadWithRequest:
Migrating from UIWebView to WKWebView

Use the following steps for UIWevView to load a remote address
Load the url address
let urlString = "http://www.example.com"
webView.loadRequest(NSURLRequest(url: NSURL(string:"url) as! URL) as URLRequest)
Conform UIWebViewDelegate in ViewController
After conforming UIWebViewDelegate, use the following delegates
/* Delegates for WebView */
fun webViewDidStartLoad(_ webView: UIWebView){
// Control loading the url
}
func webViewDidFinishLoad(_ webView: UIWebView){
// statements when loading finishes
}
fun webView(_ webView: UIWebView, didFailLoadWithError error: Error){
// Handle error here
}
If still does not work, add a row for "App Transport Security Settings" in your project Info.plist. Here is screenshot for it settings.

Related

How to set localStorage item in webView before loading a URLRequest?

I have to access a web page that has token on its localStorage
let javascript = "localStorage.setItem('token', 'abc')"
let url = URL(string: "https://test.com/abc")
webView.evaluateJavaScript(javascript) { (_, err) in
print(err?.localizedDescription)
// This will return 'A JavaScript exception occurred'
}
let request = URLRequest(url: url!)
webView.load(request)
I dont think this is possible. You get this error:
Error Domain=WKErrorDomain Code=4
"A JavaScript exception occurred" UserInfo={WKJavaScriptExceptionLineNumber=0,
WKJavaScriptExceptionMessage=SecurityError: The operation is
insecure., WKJavaScriptExceptionColumnNumber=0,
NSLocalizedDescription=A JavaScript exception occurred}
And there nothing you can do about for the WKWebView.
i solve it but i think it's a bad practice and the right way was to be able to send token in request's header
the main problem was that you can't run javascript script that add localStorage item before the web view load so i had to wait utile that page is loaded then then run javascript script that add needed token then reload that page Here is my code but again it's bad practice i think and i believe that front end team should allow me to end that token in request header
fist there was that method with reload the web view only one time
var loaded = false
func load() {
if !loaded {
webView.reload()
}
loaded = true
}
then i had to confirm to WKNavigationDelegate delegate to reload that page when it's loaded and here is my code
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript(javascript) { (_, err) in
self.load()
}
}
}

How do I keep a WKWebView object from crashing?

Scenario
I'm building an iOS application in Swift. One feature is to have a live video feed as the application background. The video feed is originating from a Raspberry Pi on a local network using sudo motion. Motion is successfully hosting the feed on default port 8081.
The Swift app has a WKWebView object with the source pointing to my Raspberry Pi's motion port.
Suspected Issue
The webpage at port 8081 is constantly refreshing to load the most recent frame from the camera.
Problem
When running the app, the feed connects successfully and loads the first frame, and occasionally a second but then cuts off.
On a few occasions I received the following error in the terminal: [ProcessSuspension] 0x282022a80 - ProcessAssertion() Unable to acquire assertion for process with PID 0 leading me to believe that it is a memory management issue related to the constantly refreshing nature of the webpage.
Current Configuration
Currently, my call to .load() the WKWebView object is in ViewController.swift > override func viewDidLoad().
Proposed Resolution
Do I need to build some form of loop structure where I load a frame, pause execution and then call the WKWebView to reload a new frame a few seconds later.
I'm very new to Swift so patience with my question format is highly appreciated.
The WkWebView and motion loading worked in Xcode 9 with iOS 11 versions but doesn't seem to work anymore with iOS 12. You are correct that the webkit is crashing on the second image.
Due to you being new to Swift I would advise reading this link on delegates because this solution I am providing will make more sense to you.
Swift Delegates
In summary, "Delegates are a design pattern that allows one object to send messages to another object when a specific event happens."
With this solution/hack we are going to use several of the WKNavigationDelegates to inform us when the WkWebView is doing specific tasks and inject our solution to the problem. You can find out all of the delegates the WKWebKit has here WKNavigationDelegates.
The following code can be used in a brand new iOS project and replace the code in the ViewController.swift. It requires no interface builder or IBOutlet connections. It will create a single web view on the view and point to the address 192.168.2.75:6789. I have included inline comments to attempt to explain what the code is doing.
We are loading the HTTP Response twice from motion in decidePolicyFor navigationResponse delegate and keeping track with a counter. I have left some print statements so you can see what the response is. The first is a header and the second is the image information.
When our counter gets to 3 items (ie the second image) we are forcing the wkWebView to cancel all navigation (ie stop loading) in the decidePolicyFor navigationResponse delegate. See the line with decisionHandler(.cancel). This is what is stopping the crash.
This leads us to receive the callback from the wkwebview delegate WebView didFail navigation. At this point we want load our motion/pi url again and start the loading process again.
We must then reset our counter so we can repeat this process until someone else comes up with a better solution.
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
// Memeber variables
var m_responseCount = 0; /* Counter to keep track of how many loads the webview has done.
this is a complete hack to get around the webkit crashing on
the second image load */
let m_urlRequest = URLRequest(url: URL(string: "http://192.168.2.75:6789")!) //Enter your pi ip:motionPort
var m_webView:WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
m_webView = WKWebView(frame: self.view.frame) // Create our webview the same size as the viewcontroller
m_webView.navigationDelegate = self // Subscribe to the webview navigation delegate
}
override func viewDidAppear(_ animated: Bool) {
m_webView.load(m_urlRequest) // Load our first request
self.view.addSubview(m_webView) // Add our webview to the view controller view so we can see it
}
// MARK: - WKNavigation Delegates
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
print("decidePolicyFor navigationAction")
print(navigationAction.request) //This is the request to connect to the motion/pi server http::/192.168.2.75:6789
decisionHandler(.allow)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: #escaping (WKNavigationResponsePolicy) -> Void) {
print("decidePolicyFor navigationResponse")
print(navigationResponse.response) // This is HTML from the motion/rpi
/* We only want to load the html header and the first image
Loading the second image is causing the crash
m_responseCount = 0 - Header
m_responseCount = 1 - First Image
m_responseCount >= 2 - Second Image
*/
if(m_responseCount < 2)
{
decisionHandler(.allow)
}
else{
decisionHandler(.cancel) // This leads to webView::didFail Navigation Delegate to be called
}
m_responseCount += 1; // Incriment our counter
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
/*
We have forced this failure in webView decidePolicyFor navigationResponse
by setting decisionHandler(.cancel)
*/
print("didFail navigation")
m_webView.load(m_urlRequest) //Lets load our webview again
m_responseCount = 0 /* We need to reset our counter so we can load the next header and image again
repeating the process forever
*/
}
func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
// If you find your wkwebview is still crashing break here for
// a stack trace
print("webViewWebContentProcessDidTerminate")
}
}
Note: You are also required to add the following to your info.plist file due to the motion/pi server response being http and not https
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
I encourage you to use this basic example and modify it to suit your application requirements. I also encourage you to post any findings of your own because I am having the exact same problem using the exact same hardware as you and this is a hack more than a solution.

Instagram Oauth Redirect on iOS

I'm writing an iOS Application that needs to use Oauth to connect to Instagram. The standard iOS mechanism for o-auth is to provide a redirect URL with a custom scheme. For instance, myapp://oauth/redirect -- And register that in your iOS app. Which is all fine and dandy, except the management portal for Instagram seems to prevent any scheme but http or https when you setup your valid redirect URI's.
Any thoughts on how to make this work? Am I forced to add a server component to capture / redirect?
There are two approaches to doing this without using a custom URL Scheme, each with positives and negatives:
Universal Links - PROS: Redirects to Safari so you don't have to build in a web browser yourself. Safari redirect also allows for using the users iCloud keychain which can make the login experience easier for your user. CONS: Setting it up is kind of a bear, and requires your own server and a published app. Doesn't seem to work in simulator.
Setup and present a UIWebView, and in webView:shouldStartLoadWithRequest:navigationType: catch the access token from the fragment, and prevent loading / redirection. This allows you to use any URL you want as the redirect URL because it doesn't matter :)
Example:
import UIKit
class OAuthVC : UIViewController, UIWebViewDelegate {
#IBOutlet var webView : UIWebView?
public var success : ((URLRequest) -> Void)?
public var presentVC : UIViewController?
func handle(_ url: URL) {
view.tag = 1 // Make sure our view and thus webview is loaded.
webView!.loadRequest(URLRequest(url: url))
}
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.url?.fragment?.contains("access_token") == true {
success!(request)
dismiss(animated: true, completion: nil)
return false
}
return true
}
func webViewDidFinishLoad(_ webView: UIWebView) {
// If we stop on a page before the redirect closes us, user interaction is required. Present.
presentVC?.present(self, animated: true, completion: nil)
}
}
List item

webViewDidFinishLoad not called for non http:// url schemes

I am trying to load a url scheme for Facebook and place that within a UIWebView as follows:
let fb:NSURL! = NSURL(string:"fb://feed");
webView.loadRequest(NSURLRequest(URL:fb));
I have set the webView delegate appropriately and have the following functions defined:
func webViewDidFinishLoad(webView: UIWebView) {
print("hellO");
}
func webViewDidStartLoad(webView: UIWebView) {
print("hello");
}
These aren't called currently. However, when i change the url to something like "www.google.com" or something of the like the statements are printed.
NOTE: The Facebook feed is successfully loading, it just isn't triggering the didStart or didFinish methods.
Is what I'm trying to do even possible for non http:// url schemes?

UIWebViewDelegate: webViewDidFinishLoad not called during in-page navigation

I have an app where users can navigate a pile of locally stored HTML files. I have a UIWebView, configured up correctly with a UIWebViewDelegate. Usually, when the user follows a link, shouldStartLoadWithRequest is called, followed by webViewDidFinishLoad a bit later.
But, if the link is pointing to an anchor on the same page as the one which is currently displayed, only shouldStartLoadWithRequest is called. webViewDidFinishLoad does not fire.
In a sense, I see that this might be expected behaviour, because in-page navigation should not require a page reload. However, I really need a place to hook into the call stack after in-page navigation is complete. The optimal solution would let me know when any sort of navigation has ended, both from another page, in-page and forward/backward actions.
My best hackaround so far has been to call performSelector: withObject: afterDelay: at the end of my shouldStartLoadWithRequest method, but I'm not happy with this.
Does anyone know how I can solve this correctly? Any insight appreciated!
You can try to use NSURLConnectionDataDelegate, it allows you to handle incoming data. Maybe you can determine if the page is loaded manually by adding a sign to your html files.
NSURLConnectionDataDelegate Reference
Edit: gist.github.com/buranmert/7304047 I wrote a piece of code and it worked, that may not be the way you wanted it to work but maybe it helps. Every time user clicks a URL with anchor, it creates another connection and as connection finishes loading web view loads the data, that marks the point where web view finished loading the page. As you use only local html files, I do not think creating connections will create problems
What you are describing is intended behavior. Just as AJAX or resource requests are never passed to the delegate, only root page changes will ever hit webViewDidFinishLoad:. But I have good news, and it doesn't involve saving a bunch of money on car insurance.
Loads performed within an iFrame DO trigger the full delegate methods and this gives you a solution. You can use this mechanism to post a notification to the native code, just as is often done for console.log() as described in this post. Alternatively, Native Bridge would work well to call into your Objective C code from JavaScript.
Just check weather u got the delegate function DidStartLoading if it is called no doubt that DidFinish also should get called
Are you sure your shouldStartLoadWithRequest always returns an YES???
I always add
return YES;
at the end of shouldStartLoadWithRequest implementation.And that works for me.
By returning YES, it denotes that the webview has loaded and would call the webViewDidFinishLoad
if([webView isLoading]==1)
{
//your webview is loading
}
else
{
//your webview has loaded
}
Here is a Swift Implementation of Mert Buran's code incase anybody is looking for it. (Although NSURLConnection is deprecated as of iOS 9)
But it does not solve my problem. When i click on a jquery link that popups a video, it does not fire the webViewDidFinishLoad.
class WebViewController: UIViewController, UIWebViewDelegate, NSURLConnectionDelegate {
var menuURL: String?
var response: NSURLResponse?
var data = NSData()
// MARK: Properties
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
// From Web
let url = NSURL (string: menuURL!)
let urlRequest = NSURLRequest(URL: url!)
let connection = NSURLConnection(request: urlRequest, delegate: self)
self.data = NSData()
connection!.start()
// if this is false, page will be 'zoomed in' to normal size
webView.scalesPageToFit = false
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == .LinkClicked && request.URL!.fragment != nil {
let connection = NSURLConnection(request: request, delegate: self)
connection!.start()
return false
}
return true
}
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
self.response = response
}
func connection(connection: NSURLConnection, didReceiveData data: NSData) {
let oldData = NSMutableData(data: self.data)
oldData.appendData(data)
self.data = oldData
}
func connectionDidFinishLoading(connection: NSURLConnection) {
self.webView.loadData(self.data, MIMEType: self.response!.MIMEType!, textEncodingName: "utf-8", baseURL: self.response!.URL!)
self.data = NSData()
}
}

Resources