swift NSURL gets back an "unable to read data" message even with https:// - swift

I am trying to learn iOS following a course and they ask to do the following:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//var string1 = "http://www.google.com"
//var string1 = "https://www.weather-forecast.com/locations/San-Antonio/forecasts/latest"
//var url = NSURL(string: string1)
var url = NSURL(string: "https://google.com")
print(url)
if url != nil {
let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
var urlError = false
if error == nil {
var urlContent = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(urlContent)
} else {
urlError = true
}
if urlError == true {
self.showError()
}
})
task.resume()
} else {
showError()
}
}
the app doesn't show any web page content and when debugging I find that the object for the url says that it is "unable to read data"
I have tried with http and https. I have tried with different web sites.
I have tried the address in the safari of the simulator and it loads.
Can someone tell me why is this not working
Thanks in advance.
gariva

You're using wrong encoding. The webpage you're trying to fetch (http://www.google.com/) uses ISO-8859-1.
I was able to reproduce your issue. Fetch worked when I changed encoding. Try this:
var urlContent = NSString(data: data!, encoding: NSISOLatin1StringEncoding)

For display web page you should use UIWebView element. Something like this:
let url = NSURL(string: "https://google.com")
let webView = UIWebView(frame: self.view.frame)
self.view.addSubview(webView)
webView.loadRequest(NSURLRequest(URL: url!))

Related

Swift URL returns nil when the url contains an internationalized domain name (IDN)

I was wondering why this piece of code always exit with 1:
import Foundation
// an idn domain:
let uLabel = "համընդհանուր-ընկալում-թեստ.հայ"
let urlStr = "https://" + uLabel
guard let url = URL(string: urlStr) else { exit(1) }
exit(0)
Since Apple's browser Safari does support well IDN domains, I was surprised their URL library does not... I tried to urlencode the string beforehand, but it is not helping.
======EDIT======
After fixing the piece above upon Matt's suggestion, I faced another problem during fetching the website data:
import Foundation
let uLabel = "համընդհանուր-ընկալում-թեստ.հայ"
let scheme = "https"
var comps = URLComponents()
comps.scheme = scheme
comps.host = uLabel
guard let url = comps.url else { exit(1) }
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let rawContent = data else { exit(1) }
guard let content = String(data: rawContent, encoding: String.Encoding.utf8) else { exit(1) }
if content.contains("UASG Testbed Landing Page") {
// successfully fetch content of the page
exit(0)
} else {
// error during fetching
exit(1)
}
}
task.resume()
RunLoop.main.run()
The program still exits with 1. It seems the domain is not converted to an A-LABEL as it is on Safari, as the error suggests (the certificate is valid, the error is misleading):
NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “համընդհանուր-ընկալում-թեստ.հայ” which could put your confidential information at risk., NSErrorFailingURLKey=https://%d5%b0%d5%a1%d5%b4%d5%a8%d5%b6%d5%a4%d5%b0%d5%a1%d5%b6%d5%b8%d6%82%d6%80-%d5%a8%d5%b6%d5%af%d5%a1%d5%ac%d5%b8%d6%82%d5%b4-%d5%a9%d5%a5%d5%bd%d5%bf.%d5%b0%d5%a1%d5%b5/,
I don't know why you're having trouble, but rule number one is never never never call URL(string). Use URLComponents. That's what it's for.
let uLabel = "համընդհանուր-ընկալում-թեստ.հայ"
let scheme = "https"
var comps = URLComponents()
comps.scheme = scheme
comps.host = uLabel
let url = comps.url // works for me
Until, someone can find a way to tell the URL framework to correctly represent the URL "https://համընդհանուր-ընկալում-թեստ.հայ", it is possible to translate the domain part to an A-LABEL before passing it to the URL constructor and elude its wrong internal representation:
import Foundation
import IDNA
// an idn domain:
let uLabel = "համընդհանուր-ընկալում-թեստ.հայ"
guard let aLabel = uLabel.idnaEncoded else { exit(1) }
let supportedUrl = "https://" + aLabel
guard let url = URL(string: supportedUrl) else { exit(1) }
let task = URLSession.shared.dataTask(with: url) { data, response, error in
guard let rawContent = data else { exit(1) }
guard let content = String(data: rawContent, encoding: String.Encoding.utf8) else { exit(1) }
if content.contains("UASG Testbed Landing Page") {
// successfully fetch content of the page
exit(0)
} else {
// error during fetching
exit(1)
}
}
task.resume()
RunLoop.main.run()
This piece of code does exit 0. The IDNA library is there (ensure you take the master branch, because released versions are still on IDNA2003):
https://github.com/Wevah/IDNA-Cocoa

Parse HTML with Swiftsoup (Swift)?

I'm trying to parse some websites with Swiftsoup, let's say one of the websites is from Medium. How can I extract the body of the website and load the body to another UIViewController like what Instapaper does?
Here is the code I use to extract the title:
import SwiftSoup
class WebViewController: UIViewController, UIWebViewDelegate {
...
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://medium.com/#timjwise/stop-lying-to-yourself-when-you-snub-panhandlers-its-not-for-their-own-good-199d0aa7a513")
let request = URLRequest(url: url!)
webView.loadRequest(request)
guard let myURL = url else {
print("Error: \(String(describing: url)) doesn't seem to be a valid URL")
return
}
let html = try! String(contentsOf: myURL, encoding: .utf8)
do {
let doc: Document = try SwiftSoup.parseBodyFragment(html)
let headerTitle = try doc.title()
print("Header title: \(headerTitle)")
} catch Exception.Error(let type, let message) {
print("Message: \(message)")
} catch {
print("error")
}
}
}
But I got no luck to extract the body of the website or any other websites, any way to get it work? CSS or JavaScript (I know nothing about CSS or Javascript)?
Use function body https://github.com/scinfu/SwiftSoup#parsing-a-body-fragment
Try this:
let html = try! String(contentsOf: myURL, encoding: .utf8)
do {
let doc: Document = try SwiftSoup.parseBodyFragment(html)
let headerTitle = try doc.title()
// my body
let body = doc.body()
// elements to remove, in this case images
let undesiredElements: Elements? = try body?.select("img[src]")
//remove
undesiredElements?.remove()
print("Header title: \(headerTitle)")
} catch Exception.Error(let type, let message) {
print("Message: \(message)")
} catch {
print("error")
}

How can I make a call to load HTML content from a website?

I am trying to get HTML content from a website so I can display data within an app. How can I make a call for the HTML code in Swift 3?
Are you trying to actually load a webpage on the screen or just retrieve the HTML source as a String/Data object?
If you just need the HTML as a response object you can do something like the following:
if let url = URL(string: "http://www.example-url.com") {
let request = URLRequest(url: url)
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if error != nil {
print(error!)
} else {
if let unwrappedData = data {
let dataString = String(data: unwrappedData, encoding: .utf8)
// …handle string here…
}
}
}
task.resume()
}
Use UIWebView.
e.g.
let webView = UIWebView()
let url = URL(string: "http://yourwebsite")
let request = URLRequest(url: url)
webView.loadRequest(request)

Alamofire says "Invalid URL" when I add russian letters to the request [duplicate]

My application is using an NSURL like this:
var url = NSURL(string: "http://www.geonames.org/search.html?q=Aïn+Béïda+Algeria&country=")
When I tried to make a task for getting data from this NSURL like this:
let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
if error == nil {
var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding)
println("urlContent \(urlContent!)")
} else {
println("error mode")
}
but I got error when trying to got data from that address, although when I using safari go to link: "http://www.geonames.org/search.html?q=Aïn+Béïda+Algeria&country=" I can see the data. How can I fix that?
Swift 2
let original = "http://www.geonames.org/search.html?q=Aïn+Béïda+Algeria&country="
if let encodedString = original.stringByAddingPercentEncodingWithAllowedCharacters(
NSCharacterSet.URLFragmentAllowedCharacterSet()),
url = NSURL(string: encodedString)
{
print(url)
}
Encoded URL is now:
"http://www.geonames.org/search.html?q=A%C3%AFn+B%C3%A9%C3%AFda+Algeria&country="
and is compatible with NSURLSession.
Swift 3
let original = "http://www.geonames.org/search.html?q=Aïn+Béïda+Algeria&country="
if let encoded = original.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),
let url = URL(string: encoded)
{
print(url)
}

How to use special character in NSURL?

My application is using an NSURL like this:
var url = NSURL(string: "http://www.geonames.org/search.html?q=Aïn+Béïda+Algeria&country=")
When I tried to make a task for getting data from this NSURL like this:
let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
if error == nil {
var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding)
println("urlContent \(urlContent!)")
} else {
println("error mode")
}
but I got error when trying to got data from that address, although when I using safari go to link: "http://www.geonames.org/search.html?q=Aïn+Béïda+Algeria&country=" I can see the data. How can I fix that?
Swift 2
let original = "http://www.geonames.org/search.html?q=Aïn+Béïda+Algeria&country="
if let encodedString = original.stringByAddingPercentEncodingWithAllowedCharacters(
NSCharacterSet.URLFragmentAllowedCharacterSet()),
url = NSURL(string: encodedString)
{
print(url)
}
Encoded URL is now:
"http://www.geonames.org/search.html?q=A%C3%AFn+B%C3%A9%C3%AFda+Algeria&country="
and is compatible with NSURLSession.
Swift 3
let original = "http://www.geonames.org/search.html?q=Aïn+Béïda+Algeria&country="
if let encoded = original.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),
let url = URL(string: encoded)
{
print(url)
}