I'm looking for a Swift 5 version of something like the fileExists function (below). For context I have an app that is heavily dependent on interaction with a remote server. For resilience we have created a second remote server and want the app to check if the first one is available, and if not, use the second one. I have found possible solutions using URLSession.shared.dataTask but they all just display a message rather than return an alternative url to use. Any suggestions would be most welcome.
func fileExists(url : NSURL!) -> Bool {
let req = NSMutableURLRequest(URL: url)
req.HTTPMethod = "HEAD"
req.timeoutInterval = 1.0 // Adjust to your needs
var response : NSURLResponse?
NSURLConnection.sendSynchronousRequest(req, returningResponse: &response, error: nil)
return ((response as? NSHTTPURLResponse)?.statusCode ?? -1) == 200
}
Related
I have submitted an app to iTunes and Apple rejected it because of a crash.
I symbolicated and analyzed the crashreport and saw that it crash at a json call.
I try to reproduce it and I found that it just happens when I turn off my wlan.
Does Apple test apps offline?
How can I handle this error? And make my jsoncall better.
This is my method:
var session = NSURLSession.sharedSession();
var uri = "/GetNews";
let request : NSMutableURLRequest = CreateRequest(uri, HTTPmethod: "GET");
let task : NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil;
let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: error) as? Dictionary<String, AnyObject>;
let resp : NewsResponse = NewsResponse(jsonData: jsonResult!);
completionHandler?(resp);
});
task.resume();
It crashs at let resp..., because jsonResult is nil and I use !
Of course Apple tests apps offline, and you should too. You should plan on every call that requires an internet connection failing, and you should handle every error appropriately.
The appropriately part is up to your app. For example, some apps (like Facebook) let you read posts you've already downloaded, and queue up posts you write to be sent when you get an internet connection. Some apps just don't work at all without an internet connection and it doesn't make sense for them to do anything but put up an error message (like, for example, a iTunes radio).
If your app is a news reader, perhaps the best thing to do is use a cache and let them read news they've downloaded in the past. A simple, unobtrusive message letting them know they're offline and new articles will be downloaded once they're back on would suffice; a crash, though, is very bad in terms of usability and utility.
TO FIX CRASH
As Eric stated, you should use "safe unwrapping", better known as optional binding. Try this:
var session = NSURLSession.sharedSession();
var uri = "/GetNews";
let request : NSMutableURLRequest = CreateRequest(uri, HTTPmethod: "GET");
let task : NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
var error: AutoreleasingUnsafeMutablePointer<NSError?> = nil;
let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: error) as? Dictionary<String, AnyObject>;
//if non-nil, assigns jsonResult to nonNilJsonResult
if let nonNilJsonResult = jsonResult {
let resp : NewsResponse = NewsResponse(jsonData: nonNilJsonResult!);
completionHandler?(resp);
}
});
task.resume();
I've created a script/api which is suppose to add a record to my database when running a specific url. However i'm not sure how to run this url. I do not expect anything back just to run this url? how can i do this?
var identifier = UIDevice.currentDevice().identifierForVendor.UUIDString
var addViewUrl = "http://url/addview.php?type=ios&identifier=\(identifier)&newsid=\(newsObject?.id)"
Based on my comment:
You should get a response and check for errors.
Also there is always the possibility to call a URL asynchronously to avoid blocking the GUI if the request takes a long time.
This can be made using delegate patterns or with completions handlers like in Objective-C.
Example:
var url = NSURL.URLWithString(addViewUrl)// Creating URL
var request = NSURLRequest(URL: url)// Creating Http Request
var queue: NSOperationQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{(response:NSURLResponse!, responseData:NSData!, error: NSError!) -> Void in
if error != nil
{
println(error.description)
}
else
{
var responseStr:NSString = NSString(data:responseData, encoding:NSUTF8StringEncoding)
//Everything went fine
}
})
I need to check if a website is reachable before loading it. I am new in iOS developement but this is the method I've implemented to discover the response.
var url = NSURL(string: "http://www.apple.com")
var task = NSURLSession.sharedSession().dataTaskWithURL(url!) {
data, response, error in
println(data)
var httpResponse = response as? NSHTTPURLResponse
println(httpResponse)
}
task.resume()
It works! But the problem is that the response comes from the cache... So the result is that:
If I am checking if a file exists and at that moment I am checking it exists -> for the application it will always exist because it is stored in the cache... So if I remove the file and then I make the request... it will always give me response 200 and not 404.
Infact if I insert this line of code (it deletes the cache!) before making the request then it works like it should work and it always check for real if the website or the file exists!
NSURLCache.sharedURLCache().removeAllCachedResponses()
So... how can I solve this problem in Swift?...thank you very much
You can set a no cache policy by using a new url session instance.
Create a property and set a new NSURLSession instance to it.
var urlSession : NSURLSession!
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.requestCachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData;
self.urlSession = NSURLSession(configuration: configuration)
Use this URLSession property to get your data.
var url = NSURL(string: "http://www.apple.com")
var task = self.urlSession.dataTaskWithURL(url!) {
data, response, error in
// Your code
}
task.resume()
in my app I'm using JSON and I made a session recently so if I would like to make some http request to get data for a specific user, the user must log in before (also used by http request).
in the safari when I entering the url's of login and then the url of receive data, it does that as needed.
but in my app, I first call login and then the url for getting data, but it's probably starting a new session in every url request which leads me to get an error and not receive the data.
my url request function is:
static func urlRequest (adress: String, sessionEnded: (NSDictionary->Void)?){
println(adress)
var urli = NSURL(string: adress)
var request = NSURLRequest(URL: urli!)
var rVal = "";
self.task = NSURLSession.sharedSession().dataTaskWithURL(urli!) {(data, response, error) in
var parseError: NSError?
let parsedObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(data,
options: NSJSONReadingOptions.AllowFragments,
error:&parseError)
let po = parsedObject as NSDictionary
if let a = sessionEnded{
sessionEnded!(po)
}
}
task!.resume()
}
thanks in advance!!
You have shared only half of the puzzle with us, the client code. We can't comment on why the app isn't working with a clearer picture of what the server API. For example, once you "log in", how do subsequent queries confirm that the request is coming from valid session. Furthermore, you report that "every url request which leads me to get an error". Well, what error do you receive? You have to be far more specific regarding the precise errors/crashes you are receiving. BTW, are you logging on to some service with a well-defined API or are you writing that code yourself, too?
Having said that, I might suggest a few refinements to this method:
The sessionEnded (which I've renamed completionHandler to conform to informal standard naming conventions), probably should return an optional NSError object, too, so the caller can detect if there was an error.
Your unwrapping of the sessionEnded completion handler can be simplified to use ?.
When you parse the object, you should feel free to perform the optional cast, too.
You probably want to detect a network error (in which case data would be nil) and return the network NSError object.
Minor point, but I'd probably also rename the function to conform to Cocoa naming conventions, using a verb to start the name. Perhaps something like performURLRequest.
This is your call, but I'd be inclined to have the method return the NSURLSessionTask, so that the caller could use that task object if it wanted to (e.g. save the task object so that it could cancel it later if it wanted to).
Thus, that yields something like:
func performURLRequest (address: String, completionHandler: ((NSDictionary!, NSError!) -> Void)?) -> NSURLSessionTask {
let url = NSURL(string: address)
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
if data == nil {
sessionEnded?(nil, error)
} else {
var parseError: NSError?
let parsedObject = NSJSONSerialization.JSONObjectWithData(data, options: nil, error:&parseError) as? NSDictionary
completionHandler?(parsedObject, parseError)
}
}
task.resume()
return task
}
And you'd invoke it like:
performURLRequest("http://www.example.com/some/path") { responseDictionary, error in
if responseDictionary == nil {
// handle error, e.g.
println(error)
return
}
// use `responseDictionary` here
}
Hi I'm downloading JSON in my app to the device with NSJSONSerialization and it seems that when I update the JSON file and re-run the download JSON function it doesn't download the new JSON. It's just downloading the non updated JSON file. I've tried running the app on different devices and it works the first time but once I update the JSON again it doesn't download the new JSON. I'm thinking it may be getting cached but I'm not sure.
Here's my code:
func downloadJSON(){
let p = NSURLRequest(URL: NSURL(string: "https://themartini.co/pythonStuff/quotey.json")!)
let sharedSession = NSURLSession.sharedSession()
let dwn = sharedSession.dataTaskWithRequest(p, completionHandler: { (data : NSData!,re : NSURLResponse!, error: NSError!) -> Void in
if error == nil {
let js : AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as NSArray // downloads JSON and puuts it into an array
println(js)
dispatch_async(dispatch_get_main_queue()){
self.quotes = js as NSArray
NSNotificationCenter.defaultCenter().postNotificationName("dataTaskFinished", object: nil)
}
}
})
dwn.resume()
}
Any ideas? Thanks.
You should pass the correct cache policy to your NSURLRequest. There is an alternative constructor that includes this parameter.
let p = NSURLRequest(URL: url,
cachePolicy: ReloadIgnoringLocalCacheData,
timeoutInterval: 15.0)
You can add a cachebuster to your NSURLRequest. (change the url to https://themartini.co/pythonStuff/quotey.json?23421231231
where the string of numbers at the end is random. This will break the cache. Also, instead of using NSURLSession, look into NSURLConnection.SendAsynchroniousRequest