NSURLSession/NSURLConnection HTTP load failed on Swift 4 - ios

I have xcode 9.1 with swift 4 and i'm unable to authenticate using webview anymore. here is my code :
import UIKit
protocol AuthViewControllerDelegate{
func getAccessToken(_ code: String)
}
class AuthViewController: UIViewController, NSURLConnectionDelegate {
#IBOutlet weak var navItem: UINavigationItem!
#IBOutlet weak var webView: UIWebView!
var delegate: AuthViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
var link: "https://home.nest.com/login/oauth2?client_id=…"
if let encodeURL = link.URLEncodedString(), let url = URL(string: encodeURL) {
webView.loadRequest(URLRequest(url: url))
}
}
override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent
}
#IBAction func CancelPressed(_ sender: UIBarButtonItem) {
self.dismiss(animated: true, completion: nil)
}
func webView(_ webView: UIWebView, shouldStartLoadWithRequest request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if let url = request.url?.absoluteString, url.contains("code=") {
if let code = getQueryStringParameter(url,param: "code"){
self.dismiss(animated: true, completion: { () -> Void in
UIApplication.shared.isNetworkActivityIndicatorVisible = false
self.delegate?.getAccessToken(code)
})
}
}
return true
}
func webViewDidStartLoad(_ webView: UIWebView){
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func webViewDidFinishLoad(_ webView: UIWebView){
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
func getQueryStringParameter(_ url: String?, param: String) -> String? {
if let url = url, let urlComponents = URLComponents(string: url), let queryItems = (urlComponents.queryItems) {
return queryItems.filter({ (item) in item.name == param }).first?.value!
}
return nil
}
}
And here is the error i'm getting in the log :
2017-11-07 20:49:30.836087+0000 TestApp[1851:1259046] TIC TCP Conn
Failed [32:0x1c0365280]: 3:-9800 Err(-9800)
2017-11-07 20:49:30.836472+0000 TestApp[1851:1259046]
NSURLSession/NSURLConnection HTTP load failed
(kCFStreamErrorDomainSSL, -9800)
2017-11-07 20:49:30.836578+0000 TestApp[1851:1259046] Task
<0A675AA1-7110-4FCC-99B2-054380D22F01>.<0> HTTP load failed (error
code: -1200 [3:-9800])
2017-11-07 20:49:30.837548+0000 TestApp[1851:1258708] NSURLConnection
finished with error - code -1200
And i already have this into my .plist file :
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
NB: The same code was working perfectly with swift 3.2

Check if your htpps:// is right, had the same problem, fixed after changing url.

Adding NSAllowsArbitraryLoads into .plist file resolves the error
NSURLConnection error - code - 1200
where my development language is Objective C.

Related

webViewDidFinishLoad() does not appear to be running in Swift

I have a view controller for a webview, setup through KINWebBrowser.
I've tried several ways to get these methods to execute:
func webViewDidStartLoad(webView: UIWebView) {
print("Strat Loading")
}
func webViewDidFinishLoad(webView: UIWebView) {
print("Finish Loading")
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
print(error?.localizedDescription)
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
return true
}
override func webViewDidFinishLoad(_ webView: UIWebView) {
super.webViewDidFinishLoad(webView)
print("Finish Loading 2")
}
But nothing seems to be working.
My class definition seems normal: class WebBrowserViewController: KINWebBrowserViewController, NavigationProtocol {
But no matter what I do - override func or not, no logger or print statement I put in these functions seems to be executed.
What am I doing wrong? I am trying to create an event that will listen to when all of the web content is loaded so I can stop having a loading spinner appear.
EDIT: Adding delegate information:
When I have tried to add a delegate to viewDidLoad():
webView.delegate = self
I have gotten the following error:
Ambiguous reference to member 'webView(_:decidePolicyFor:decisionHandler:)'
This is my viewDidLoad() without the delegate setting:
override func viewDidLoad() {
super.viewDidLoad()
pointsNavigationItem = addPointsButtonToNavigation()
self.showsURLInNavigationBar = false
self.showsPageTitleInNavigationBar = false;
self.tintColor = UIColor.navText
self.barTintColor = .navBackground
}
I'm not sure that KinWebBrowser does delegation the same way?
Added the KINWebBrowserDelegate to the class definition and added the following methods:
func webBrowser(_ webBrowser: KINWebBrowserViewController!, didFailToLoad URL: URL!, error: Error!) {
print("DEBUG 5")
}
func webBrowserViewControllerWillDismiss(_ viewController: KINWebBrowserViewController!) {
print("DEBUG 1")
}
func webBrowser(_ webBrowser: KINWebBrowserViewController!, didStartLoading URL: URL!) {
print("DEBUG 2")
}
func webBrowser(_ webBrowser: KINWebBrowserViewController!, didFinishLoading URL: URL!) {
print("DEBUG 3")
}
func didChangeValue<Value>(for keyPath: KeyPath<WebBrowserViewController, Value>) {
print("DEBUG 4")
}
None of the debugs are getting called in the log.
Those are the UIWebView delegate methods, however from the KINWebBrowser page you should probably be using the KINWebBrowserDelegate protocol
(void)webBrowser:(KINWebBrowserViewController *)webBrowser didStartLoadingURL:(NSURL *)URL;
(void)webBrowser:(KINWebBrowserViewController *)webBrowser didFinishLoadingURL:(NSURL *)URL;
(void)webBrowser:(KINWebBrowserViewController *)webBrowser didFailToLoadURL:(NSURL *)URL withError:(NSError *)error;

UIWebview reload doesn't work

I use UIWebView to load the remote url, the page is blank because the network is unavailable. After network is available ,i reload the UIWebView, but the page is still blank.
How can i solve it?
Try this
func reloadRequest() {
if let url = URL(string: "https://stackoverflow.com") {
let request = URLRequest(url: url);
self.loadRequest(request);
}
}
Load web view with request again, like:
if let url = URL(string: "https://www.google.com") {
let request = URLRequest(url: url)
wvWebView.loadRequest(request)
}
Here is sample code.
#IBOutlet var wvWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
wvWebView.delegate = self
reloadWebview()
}
// call this function to reload web view.
func reloadWebview(){
if let url = URL(string: "https://www.google.com") {
let request = URLRequest(url: url)
wvWebView.loadRequest(request)
}
}
// Webview Delegates
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
print("\n\n**** webView:shouldStartLoadWithRequest: \(request.url) \(navigationType)")
return true
}
func webViewDidStartLoad(_ webView: UIWebView) {
print("webViewDidStartLoad")
}
func webViewDidFinishLoad(_ webView: UIWebView) {
print("webViewDidFinishLoad")
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
print("error description - \(error.localizedDescription)")
}

UIWebView and JavaScriptInterface in Swift

How can I to create the JavascriptInterface channel from my web site to my UIWebView?
Example in Android:
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
And from this JavascriptInterface I would like to draw the methods, as for example:
func webViewDidStartLoad(webView: UIWebView)
or
myActivityIndicator.startAnimating()
How can I do?
For WKWebView: source here
JavaScript
function callNativeApp () {
try {
webkit.messageHandlers.callbackHandler.postMessage("Hello from JavaScript");
} catch(err) {
console.log('The native context does not exist yet');
}
}
Swift
import WebKit
class ViewController: UIViewController, WKScriptMessageHandler {
#IBOutlet var containerView: UIView? = nil
var webView: WKWebView?
override func loadView() {
super.loadView()
let contentController = WKUserContentController()
contentController.addScriptMessageHandler(self, name: "callbackHandler")
let config = WKWebViewConfiguration()
config.userContentController = contentController
self.webView = WKWebView( frame: self.containerView!.bounds, configuration: config)
self.view = self.webView
}
override func viewDidLoad() {
super.viewDidLoad()
//I use the file html in local
let path = NSBundle.mainBundle().pathForResource("index", ofType: "html")
let url = NSURL(fileURLWithPath: path!)
let req = NSURLRequest(URL: url)
self.webView!.loadRequest(req)
}
func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {// edit: changed fun to func
if (message.name == "callbackHandler"){
print("\(message.body)")
}
}
}
For UIWebView: source here
JavaScript in HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
(function(){
$(window).load(function(){
$('.clickMe').on('click', function(){
window.location = "foobar://fizz?Hello_from_javaScript";
});
});
})(jQuery);
</script>
Swift
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var Web: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let path = NSBundle.mainBundle().pathForResource("index", ofType: "html")
let url = NSURL(fileURLWithPath: path!)
let req = NSURLRequest(URL: url)
Web.delegate = self
Web.loadRequest(req)
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.URL?.query != nil {
print("\(request.URL!.query!)")
}
return true
}
}
Here's a quick example:
Register a URL Scheme such as foobar in Xcode
Handle this URL Scheme in your web view
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if request.URL?.query?.containsString("show_activity_indicator=true") {
myActivityIndicator.startAnimating()
}
}
Finally, call this from your JavaScript
// Show your activity indicator from JavaScript.
window.location = "foobar://fizz?show_activity_indicator=true"
Note: See my question here for more information on web view communication in iOS.

UIWebView does load URLs different that mobile Safari?

I use a UIWebView and want to load the following urls:
http://feratel.lueneburger-heide.de/lhg/de/accommodation/detail/LUH/8634e147-e13d-40f5-8954-2ac40cfea2a7/romantik_hotel_bergström?customHeader=true
http://feratel.lueneburger-heide.de/lhg/de/accommodation/detail/LUH/8af6d1fd-af6d-4765-8025-9eb8fa05ea42/hotel%20undeloher%20hof?customHeader=true
When you load the above urls in mobile Safari and in a UIWebView you get different results. Not in terms of rendering the page, but the final page which is shown to the user is a different one.
Here is the code I use with iOS7:
class ViewController: UIViewController, UIWebViewDelegate, NSURLConnectionDataDelegate {
#IBOutlet weak var webView: UIWebView!
var request: NSURLRequest!
var urlString: String!
private var isDone: Bool = false
private var failedRequest: NSURLRequest!
override func viewDidLoad() {
super.viewDidLoad()
var userAgent = ["UserAgent": "mozilla/5.0 (iphone; cpu iphone os 7_0_2 like mac os x) applewebkit/537.51.1 (khtml, like gecko) version/7.0 mobile/11a501 safari/9537.53"]
NSUserDefaults.standardUserDefaults().registerDefaults(userAgent as [NSObject : AnyObject])
urlString = "https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655"
request = NSURLRequest(URL: NSURL(string: urlString)!)
webView.delegate = self
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.webView.loadRequest(self.request)
}
// MARK: UIWebViewDelegate
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
println("shouldStartLoadWithRequest()")
if !isDone {
isDone = false
println("shouldStartLoadWithRequest() 111")
failedRequest = request
webView.stopLoading()
var connection = NSURLConnection(request: request, delegate: self, startImmediately: true)
connection!.start()
// NSURLConnection(request: request, delegate: self)
return false
}
println("shouldStartLoadWithRequest() -----------------------")
return true
}
func webViewDidStartLoad(webView: UIWebView) {
println("webViewDidStartLoad()")
}
func webViewDidFinishLoad(aWebView: UIWebView) {
println("webViewDidFinishLoad()")
}
func webView(webView: UIWebView, didFailLoadWithError error: NSError) {
println("webView(): didFailLoadWithError(): \(error)")
}
// MARK: NSURLConnectionDataDelegate
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {
println("connection willSendRequestForAuthenticationChallenge")
if challenge.previousFailureCount == 0 {
self.isDone = true
println("x1")
let credential: NSURLCredential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
challenge.sender.useCredential(credential, forAuthenticationChallenge: challenge)
}
else {
println("x2")
challenge.sender.cancelAuthenticationChallenge(challenge)
}
}
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) {
println("connection didReceiveResponse")
self.isDone = true
connection.cancel()
self.webView.loadRequest(self.failedRequest)
}
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool {
println("connection canAuthenticateAgainstProtectionSpace")
return protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust
}
}
How can I force a UIWebView to load URLs like mobile Safari does?
Edit: Here is how it looks when I use the following url:
http://feratel.lueneburger-heide.de/lhg/de/accommodation/detail/LUH/8af6d1fd-af6d-4765-8025-9eb8fa05ea42/hotel%20undeloher%20hof?customHeader=true
in UIWebView:
and here is how it looks in mobile Safari:
This worked for me :
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://feratel.lueneburger-heide.de/lhg/de/accommodation/detail/LUH/8634e147-e13d-40f5-8954-2ac40cfea2a7/romantik_hotel_bergstrom?customHeader=true"]]];
I need to replace the 'ö' with 'o' character in the URL

Back button not working for my manually loaded UIWebView for external website

Technologies: Swift, iOS8.1.3, XCode 6, Maverick
I've added a "rewind" back button to my UIWebview via the main.storyboard and connected it here on my view with a backButton func:
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet weak var webView: UIWebView!
var detailItem: NSURL?
var grabData = false
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
self.navigationController?.toolbarHidden = false;
}
#IBAction func backButton(sender: AnyObject) {
self.webView.goBack()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if let url = self.detailItem {
webView.loadRequest(NSURLRequest(URL: url))
}
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request:
NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if grabData {
grabData = false
return true
} else {
grabData = true
manuallyLoadPage(request)
return false
}
}
func manuallyLoadPage(request: NSURLRequest) {
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request) {
(data, response, error) invar html = NSString(data: data, encoding: NSUTF8StringEncoding) as String
html = html.stringByReplacingOccurrencesOfString("</head>", withString: "<styles><link rel='stylesheet' type='text/css' href='link-to-styles.css' media='all'></styles></head>", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
self.webView.loadHTMLString(html, baseURL: response.URL!)
}
task.resume()
}
My back button doesn't do anything when clicked and I want it to show the previous webpage. I think it has something to do with how I am intercepting the session and manually loading the WebView. Maybe it doesn't know what the original base url is? Something else?

Resources