ios swift nsurl

ios - Cómo obtener fuente HTML de URL con Swift



nsurl (5)

Este es el camino a seguir en Swift 2:

let myURLString = "https://duckduckgo.com/" if let myURL = NSURL(string: myURLString) { do { let myHTMLString = try String(contentsOfURL: myURL, encoding: NSUTF8StringEncoding) print("HTML : /(myHTMLString)") } catch { print("Error : /(error)") } } else { print("Error: /(myURLString) doesn''t URL") }

También como extra relacionado con respuestas anteriores:
Tenga en cuenta que Swift 2 presenta un nuevo enfoque de manejo de errores que produce un código mucho más claro para que lo lean los programadores, elimina las complejidades como pasar a NSErrors y le brinda mayor seguridad al garantizar que detecte todos los errores.

¡Solo usa try! Si está 100% seguro de que la llamada no fallará.

Lectura adicional: https://www.hackingwithswift.com/new-syntax-swift-2-error-handling-try-catch

Necesito ver el HTML de una página dada por una determinada URL. Si tengo esto, ¿cuál es la forma más eficiente y sincrónica de obtener la fuente HTML para esa URL usando Swift? No he podido encontrar una forma concisa en línea que lo devuelva en una variable en lugar de imprimirlo en un Handler de compleción.

Necesito manipular la fuente fuera de cualquier llamada que use la URL. ¿Cómo se hace esto en Swift?


Una respuesta actualizada de @DCMaxx a Swift 2.2:

let myURLString = "http://www.yahoo.com" if let myURL = NSURL(string: myURLString) { var error: NSError? let myHTMLString = try! NSString(contentsOfURL: myURL, encoding: NSUTF8StringEncoding) if let error = error { print("Error : /(error)") } else { print("HTML : /(myHTMLString)") } } else { print("Error: /(myURLString) doesn''t URL") }


ejemplo funcional más compacto

let myURLString = "https://google.com" let myHTMLString = try URL(string: myURLString) .flatMap { try Data(contentsOf: $0) } .flatMap { String(data: $0, encoding: .ascii) }


Descargo de responsabilidad: dado que se están recibiendo muchas vistas, solo quiero recordarles a todos que esta respuesta es sincrónica y bloqueará su aplicación si lo hace en el hilo principal. Siempre debe hacer esto de forma asíncrona (en un subproceso en segundo plano), pero la pregunta se refiere a un método síncrono, por lo que estaría fuera del alcance explicar cómo hacerlo aquí.

Probablemente deberías mirar el método:

+ stringWithContentsOfURL:encoding:error ( docs )

Lo llamaría así en el Objetivo C:

NSString *myURLString = @"http://google.com"; NSURL *myURL = [NSURL URLWithString:myURLString]; NSError *error = nil; NSString *myHTMLString = [NSString stringWithContentsOfURL:myURL encoding: NSUTF8StringEncoding error:&error]; if (error != nil) { NSLog(@"Error : %@", error); } else { NSLog(@"HTML : %@", myHTMLString); }

Así que en Swift 3 y 4, el equivalente sería:

let myURLString = "https://google.com" guard let myURL = URL(string: myURLString) else { print("Error: /(myURLString) doesn''t seem to be a valid URL") return } do { let myHTMLString = try String(contentsOf: myURL, encoding: .ascii) print("HTML : /(myHTMLString)") } catch let error { print("Error: /(error)") }

Es posible que desee adaptar la codificación (consulte las constants ) según la codificación que utilice su página.

Vieja respuesta, Swift 2.2:

let myURLString = "http://google.com" guard let myURL = NSURL(string: myURLString) else { print("Error: /(myURLString) doesn''t seem to be a valid URL") return } do { let myHTMLString = try String(contentsOfURL: myURL) print("HTML : /(myHTMLString)") } catch let error as NSError { print("Error: /(error)") }

Respuesta antigua, Swift 1.2:

let myURLString = "http://google.com" if let myURL = NSURL(string: myURLString) { var error: NSError? let myHTMLString = NSString(contentsOfURL: myURL, encoding: NSUTF8StringEncoding, error: &error) if let error = error { println("Error : /(error)") } else { println("HTML : /(myHTMLString)") } } else { println("Error: /(myURLString) doesn''t seem to be a valid URL") }


Swift 3:

if let url = URL(string: "https://www.google.com/trends/hottrends/atom/hourly") { do { let contents = try String(contentsOf: url) print(contents) } catch { // contents could not be loaded } } else { // the URL was bad! }