How to load a url in swift [duplicate] - swift

I am following this post for getting JSON data and for some reason this function:
func getJSON(urlToRequest: String) -> NSData {
return NSData(contentsOfURL: NSURL(string: urlToRequest)!)!
}
returns nil as I get the error:
fatal error: unexpectedly found nil while unwrapping an Optional value
I have broken up the function:
func getJSON(urlToRequest: String) -> NSData {
let url : NSURL! = NSURL(string: urlToRequest)
let data : NSData! = NSData(contentsOfURL: url)
return data
}
And according to my debugger:
Printing description of url:
www.reddit.com/r/earthporn/.json
Printing description of data:
(NSData!) data = nil
Pasting in the URL to Chrome doesn't give a blank page. It gives a massive glob of JSON, so I am confused why data is nil?

You forgot to add the url scheme. NSURL needs the full address. You should also use if let to safely unwrap your optional data:
func getJSON(urlToRequest: String) -> NSData? {
if let url = NSURL(string: urlToRequest) {
if let data = NSData(contentsOfURL: url) {
return data
}
}
return nil
}
if let myData = getJSON("http://www.reddit.com/r/earthporn/.json") {
println("there is data") // "there is data in myData"
}

Related

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)
}

Swift Alamofire Unexpectedly found nil - Facebook API

I'm running the same exact function for Instagram and Youtube API with Alamofire and SwiftyJSON; however, with Facebook's API I get fatal error: unexpectedly found nil while unwrapping an Optional value.
var posts : [JSON] = []
func loadFBFeed() {
let url = "https://graph.facebook.com/mpstnews/posts/?access_token=\(apiKey)&date_format=U&fields=comments.limit(0).summary(1),likes.limit(0).summary(1),from,picture,message,story,name,link,created_time,full_picture&limit=5"
Alamofire.request(.GET, url).responseJSON { (request, response, json) -> Void in
switch json {
case .Success(let data):
let jsonObj = JSON(data)
if let postInfo = jsonObj["data"].arrayValue as [JSON]? {
self.posts = postInfo
self.tableView.reloadData()
}
case .Failure(_, let error):
print("Request failed with error: \(error)")
}
}
}
URL was using characters that Alamofire does not like, had to add a line to resolve...
var urlString = "https://graph.facebook.com/mpstnews/posts/?access_token=\(apiKey)&date_format=U&fields=comments.limit(0).summary(1),likes.limit(0).summary(1),from,picture,message,story,name,link,created_time,full_picture&limit=5"
urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
This generally happens when your URL is not valid. Most likely one of the values you are inserting into the URL is bad causing the URLStringConvertible logic to crash.

JSON is not convertible to void (Openweather map API)

I am calling Openweather map API using Swift and from the response I need to return a particular value as string.
However when I try to return the value error comes as JSON is not convertible to string.
func callWeatherServ(name:String, completion:(Dictionary<String,AnyObject>) -> Void)
{
var baseUrl: String = "http://api.openweathermap.org/data/2.5/weather"
var url: String = "\(baseUrl)?q=\(name)"
let finalUrl: NSURL = NSURL(string: url)!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(finalUrl, completionHandler: {data, response, error -> Void in
if error != nil
{
// If there is an error in the web request, print it to the console
println(error.localizedDescription)
}
var err: NSError?
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as! NSDictionary
if err != nil
{
// If there is an error parsing JSON, print it to the console
println("JSON Error \(err!.localizedDescription)")
}
let json = JSON(jsonResult)
println("response is \(json) ")
var weathername = json["weather"][0]["main"]
if (weathername != nil)
{
return weathername
}
})
task.resume()
}
I get that since we have used closure whose return type void so we should use completion handler. But I am not aware how we can do that.
Also how we can call the function if we pass completion handler as parameter?
If you want to keep using SwiftyJSON as in your example, here's how to do it:
change the type of the completion handler from a dictionary to the JSON type used by SwiftyJSON.
then wrap the value you want to "return" in the handler.
then call your method as in my example, with a trailing closure
Swift 2
func callWeatherServ(name:String, completion:(object: JSON) -> Void) {
let baseUrl: String = "http://api.openweathermap.org/data/2.5/weather"
let url: String = "\(baseUrl)?q=\(name)"
if let finalUrl = NSURL(string: url) {
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(finalUrl, completionHandler: {data, response, error -> Void in
if let error = error {
print(error.localizedDescription)
} else {
if let data = data {
let json = JSON(data: data)
print("response is \(json) ")
completion(object: json["weather"][0]["main"])
} else {
print("No data")
}
}
})
task.resume()
}
}
Call the method:
callWeatherServ("paris") { (object) in
// here you get back your JSON object
print(object)
}
Note that you were parsing your data twice, with NSJSONSerialization and with SwiftyJSON, so I've removed the unnecessary NSJSONSerialization part.
Original Swift 1 version
func callWeatherServ(name:String, completion:(object: JSON) -> Void)
{
var baseUrl: String = "http://api.openweathermap.org/data/2.5/weather"
var url: String = "\(baseUrl)?q=\(name)"
let finalUrl: NSURL = NSURL(string: url)!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(finalUrl, completionHandler: {data, response, error -> Void in
if error != nil
{
// If there is an error in the web request, print it to the console
println(error.localizedDescription)
}
var err: NSError?
let json = JSON(data: data, options: NSJSONReadingOptions.allZeros, error: &err)
println("response is \(json) ")
var weathername = json["weather"][0]["main"]
if (weathername != nil)
{
completion(object: weathername)
}
})
task.resume()
}
Call the method:
callWeatherServ("paris", completion: { (object) -> Void in
println(object) // "Clear"
})
Implement completion handler from where you are calling this method and use the string at that place only no need to return the string.
You can directly use it from the completion handle by implemet it in caller function

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)
}

Getting the JSON data from cloudant

I have a database created in Cloudant and I am trying to parse that data in my Swift project. I have imported the Cloudant libraries, but I am not able to parse the data.
Here is the code that I Have been using till now
var url = "https://username:password#cloudant.com/dashboard.html#/database/db_2/login_details"
var request = NSMutableURLRequest()
request.URL = NSURL(string: url)
request.HTTPMethod = "GET"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil
let jsonResult: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary
if jsonResult != nil {
if let item = json[0] {
if let student_name = item["Student_name"] {
if let pasword = student_name["password"] {
println("Login Successfull")
}
}
}
}
}
Can somebody tell me where am I going wrong?
You should use stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) to convert any special characters your link may have. Also NSURL may return nil so you need to use if let to handle it as follow :
if let link = "https://username:password#cloudant.com/dashboard.html#/database/db_2/login_details".stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
if let checkedUrl = NSURL(string: link) {
println("valid Url")
// your code
} else {
println("invalid Url")
}
}