With the code below I create a webview navigating to a URL. It works perfectly. But I need this navigation to be through an HTTP proxy.
How do I put proxy in a webview?
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView()
webView.navigationDelegate = self
view = webView
let url = URL(string: "https://www.iphub.info")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
}
}
Unfortunately, there's no simple way to achieve that.
You may intercept deprecated UIWebView with custom NSURLProtocol subclass, which utilizes NSURLSession inside. You'd easily set HTTP proxy configuration to the connectionProxyDictionary property of the NSURLSessionConfigurtion. Here's the example. That would work, but you may see slightly worse performance than with WKWebView.
Alternatively, you may consider using NetworkExtension.framework, if the proxy server supports tunneling. Here's a sample project which utilizes Network Extension.
Related
I am trying to load a webpage with WKWebView on iOS 13 with Swift. It works fine in iOS 12. The problem is the WKWebView shows a white screen on iOS 13. The same url used for both (iOS 12/iOS 13) so I am 100% sure that there is no problem in the URL. Here is my UIViewController where I load the webpage:
import UIKit
import WebKit
class WebViewController: UIViewController , WKNavigationDelegate{
var param : String!
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://google.com")!
webView.load(URLRequest(url: url))
}
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
}
safari inspector result :
for iOS 13 similator is about:blank
for real device iOS 12.4 is www.google.com
One way to ensure you will always have a valid URL before making the request is by using an 'if' instead of force unwrapping.
override func viewDidLoad() {
super.viewDidLoad()
if let url = URL(string: MyData.url){
webView.load(URLRequest(url: url))
}
}
You can also add an else statement on there or use an early return to print something out if you don't go inside.
Other than doing this to check the url is valid you can debug and check the webview to see if that is nil. If this is the case then your solution is to add your code to viewWillAppear instead of viewDidLoad.
I tried it also with iOS 12.2 version on simulator and gives the same problem , so I see that the simulators does not support WKWebView , on the real device it works fine
I am trying to enable zooming/magnification (by pinching) in a very simple WebView in a simple app. (I am pretty new to this).
The Swift code that loads the web page is this and it works fine, I got from here, I think.
import UIKit
import WebKit
class SecondViewController: UIViewController {
#IBOutlet weak var WebViewSchedule: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// load the page
let url = URL(string: "https://mywebsite.com")
let request = URLRequest(url: url!)
WebViewSchedule.load(request)
//how do i make it zoomable ????
}
}
I've looked at a number of answers about this, and they all refer to a property "allowsMagnification". I have tried various ways of using this and got nowhere.
So could some kind person give me a beginners' guide to how to make my iOS Web page zoomable?
I have been trying to figure out how to specify a user agent for my web app in Swift 4 so that the website it opens is in mobile-view. It is defaulting to desktop view instead of mobile. Is there any way to fix this? I am currently learning Swift so I am not that familiar with it yet. I have attached the code below, changing the website for confidentiality reasons.
import UIKit
import WebKit
class ViewController: UIViewController {
var website = "https://www.example.com"
#IBOutlet var webpage: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: website)
let request = URLRequest(url: url!)
webpage.load(request)
}
}
You may change your user agent adding the following HTTPHeaderField:
// valid only for this request
var request = URLRequest(url: URL(string:"https://www.apple.com")!)
request.addValue("myUserAgent", forHTTPHeaderField: "User-Agent")
Another possibility which will completely override the default user agent (might be harmful) is this:
wkWebView.customUserAgent = "myUserAgent"
I am testing out swifts UIWebView, I have placed a simple UIWebview on my storyboard and this is my viewController code
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = URL(string: "https://mywebsitehere.com")
webView.loadRequest(URLRequest(url: url!))
} }
Unfortunately I am getting this Error below, I have added App transport security settings to my app like other answers to a similar question have said to do but it still isn't working...
Can anyone help?
objc[946]: Class PLBuildVersion is implemented in both
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices
(0x124feecc0) and
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices
(0x124e056f0). One of the two will be used. Which one is undefined.
This is bug with xCode while simulating the real iOS device, some time it through such kind of error.
Please check whether your url is active or not.
let url = URL(string: "https://google.com")
webView.loadRequest(URLRequest(url: url!))
I used the same code with google URL and it is working.
I create a app for loading a website. It loads fine but it takes 2 to 3 min every time. So I choose to implement EVURLCache.
I use the pod for EVURLCache. And I import that in MainVC.But I don't know how to setup it.
my Aim is to reduce the load time one time it loaded.
GitHub of EVURLCache : https://github.com/evermeer/EVURLCache
MainVC
import UIKit
import EVURLCache
class MainVC: UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView: UIWebView!
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
activityIndicator.startAnimating()
webView.delegate = self
let BASE_URL = "http://www.xeoscript.com/"
let url = URL(string: BASE_URL)
let request = URLRequest(url: url!)
webView.loadRequest(request)
}
func webViewDidFinishLoad(_ webView: UIWebView) {
activityIndicator.stopAnimating()
}
}
As you can see in the appdelegate of the demo, it's enough to add the following line in your didFinishLaunchingWithOptions.
EVURLCache.activate()
See the demo appdelegate for the available configuration options.
If you then run the app, then all your content will be downloaded to a Cache folder in your documents folder. The next time you start the app the files will be loaded from there. It's also possible to include that cache folder inside your app so that people will have the data available directly after downloading your app. The only thin you have to do for this is copying the content from the Cache folder to a PreCache folder in the root of your application.
See the demo app to see how it is setup.