crash uiwebview without http or https in swift [duplicate] - ios

This question already has answers here:
Add http:// to NSURL if it's not there
(5 answers)
Closed 5 years ago.
I want show the URL in Web View
here is my code
let urlString:String = "https://www.apple.com"
let url:URL = URL(string: urlString)!
let urlRequest:URLRequest = URLRequest(url: url)
webView.load(urlRequest)
urlTextField.text = urlString
if user forget to write http or https the app crashed how can I resolve this error

Just use starts(with:) on string to detect if the url string starts with http/https, and if not, add the "http://" yourself (also, use safe if let instead of force unwrap):
var urlString: String = "www.apple.com"
if !urlString.starts(with: "http://") && !urlString.starts(with: "https://") {
urlString = "http://\(urlString)"
}
if let url: URL = URL(string: urlString) {
let urlRequest: URLRequest = URLRequest(url: url)
webView.load(urlRequest)
urlTextField.text = urlString
}

Related

URLRequest to Alamofire URLConvertible

I have found the same question but not worked for me. thats why I'm posting this again.
The following code gives me an error.
Could not cast value of type 'Foundation.URLRequest' to 'Alamofire.URLConvertible'
the code sample:
let url = (wURL).replacingOccurrences(of: "(f_id)", with: String(conID))
let _url = URL(string: url)
var urlRequest = URLRequest(url: _url!)
urlRequest.timeoutInterval = TimeInterval(exactly: 30)!
let finalUrl = urlRequest as! URLConvertible
What am I missing?
You can't do it, because for URLRequest there is special protocol URLRequestConvertible.
let realURL: URL = URL(string: "https://google.com")!
let url: Alamofire.URLConvertible = realURL
let urlRequest: Alamofire.URLRequestConvertible = URLRequest(url: realURL)
AF.request(urlRequest).responseJSON {
print($0)
}

Add parameters to URL (resource from path, WKWebView)

I am using Swift 4 & WKWebView. The HTML pages are included in the app (not downloaded from server).
For configuration purpose I would like to add parameters to the URL (e.g. ...index.html?debug=true).
Currently I am using the following approach to load the page:
let indexHTMLPath = Bundle.main.path(forResource: "f7Shoma/index", ofType: "html")
let url = URL(fileURLWithPath: indexHTMLPath!)
let request = URLRequest(url: url)
...
appWebView!.load(request)
How can parameters be added/passed to the page?
You can use URLComponents to create a URL with a query component:
var components = URLComponents(string: indexHTMLPath)
components?.queryItems = [URLQueryItem(name: "debug", value: "true")]
if let result = components?.url {
let request = URLRequest(url: url)
appWebView!.load(request)
}

How to make CONNECT call in iOS?

NSURLSession supports POST and GET; does it also support CONNECT?
If so, what is the exact syntax?
Thanks!
Use NSMutableURLRequest to set whatever HTTP method you want
let session = NSURLSession()
guard let url = NSURL(string: "http://www.google.com") else {
fatalError("Error creating NSURL for http://www.google.com")
}
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "CONNECT"
let task = session.dataTaskWithRequest(request)
task.resume()

How to save UIWebView content for offline display?

I'm using Xcode 7.2 and Swift to create an iOS app, on this app I display the content of my website, however if I was offline the content will not be shown. So I want to cache the webpage and display for offline.
After I declared everything I'm using the following code :
var URLPATH="http://google.com"
let requestURL = NSURL(string: URLPATH)
let request = NSURLRequest(URL: requestURL!)
WB.loadRequest(request)
HTML to Data
if let url = URL(string: urlString) {
person.setValue(try? Data(contentsOf: url), forKey: "content_article")
}
Data to WebView
if let savedObject = fetchedObjects?.first,
let data = savedObject.content_article as? Data,
let baseStringUrl = savedObject.content_url,
let baseURL = URL(string: baseStringUrl) {
webView.load(
data, mimeType: "text/html",
textEncodingName: "",
baseURL: baseURL
)
}

UIWebView cache not working

I am trying to cache my UIWebView for when I have no network connection but it doesn't work. I tried looking into HanekeSwift but that only works with JSON requests as far as I could find.
This is what I currently have:
override func viewDidLoad() {
super.viewDidLoad()
let id = NSUserDefaults.standardUserDefaults().objectForKey("id") as! String
let url = NSURL (string: "http://****.php?id=\(id)");
let requestObj = NSURLRequest(URL: url!);
subtitleLabel.loadRequest(requestObj);
}
My question was answered here:
Getting error with cachePolicy
Thanks to KudoCC for pointing me in the right direction I got this as my final code:
var url = NSURL(string: "****.php?page=over_polen")
let requestObj = NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: 5);
webViewLabel.loadRequest(requestObj);

Resources