Swift 3.0
guard let url = URL(string: "http://www.example.com") else {
return
}
do {
let html = try String(contentsOf: url)
}
catch {
//handle `error` here
}
Swift 2.3
guard let url = NSURL(string: "http://www.example.com") else {
return
}
do {
let html = try String(contentsOfURL: url)
}
catch {
//handle `error` here
}
Objective-C
NSString *url = @"http://www.example.com";
NSURL *urlRequest = [NSURL URLWithString:url];
NSError *err = nil;
NSString *html = [NSString stringWithContentsOfURL:urlRequest encoding:NSUTF8StringEncoding error:&err];
if(err)
{
//Handle
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…