Testing file existence using NSURL

NSURL does have this method: – (BOOL)checkResourceIsReachableAndReturnError:(NSError **)error Which “Returns whether the resource pointed to by a file URL can be reached.” NSURL *theURL = [NSURL fileURLWithPath:@”/Users/elisevanlooij/nonexistingfile.php” isDirectory:NO]; NSError *err; if ([theURL checkResourceIsReachableAndReturnError:&err] == NO) [[NSAlert alertWithError:err] runModal];

Can I somehow do a synchronous HTTP request via NSURLSession in Swift

You can use this NSURLSession extension to add a synchronous method: extension NSURLSession { func synchronousDataTaskWithURL(url: NSURL) -> (NSData?, NSURLResponse?, NSError?) { var data: NSData?, response: NSURLResponse?, error: NSError? let semaphore = dispatch_semaphore_create(0) dataTaskWithURL(url) { data = $0; response = $1; error = $2 dispatch_semaphore_signal(semaphore) }.resume() dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) return (data, response, error) } } Update … Read more

What’s the difference between passing false and true to ‘resolvingAgainstBaseURL’ when initialize a NSURLComponents instance?

It makes only a difference if you create the URL components from an NSURL which was created relative to another NSURL: let baseURL = NSURL(string: “http://server/foo/”)! let url = NSURL(string: “bar/file.html”, relativeToURL: baseURL)! print(url.absoluteString) // “http://server/foo/bar/file.html” With resolvingAgainstBaseURL == false, the URL components represent only the relative part of the URL: let comp1 = NSURLComponents(URL: … Read more

How to fix “NSURLErrorDomain error code -999” in iOS

The error has been documented on the Mac Developer Library(iOS docs) The concerned segment from the documentation will be: URL Loading System Error Codes These values are returned as the error code property of an NSError object with the domain “NSURLErrorDomain”. enum { NSURLErrorUnknown = -1, NSURLErrorCancelled = -999, NSURLErrorBadURL = -1000, NSURLErrorTimedOut = -1001, … Read more

How can I build a URL with query parameters containing multiple values for the same key in Swift?

All you need is URLComponents (or NSURLComponents in Obj-C). The basic idea is to create a bunch of query items for your id’s. Here’s code you can paste into a playground: import Foundation import XCPlayground let queryItems = [URLQueryItem(name: “id”, value: “1”), URLQueryItem(name: “id”, value: “2”)] var urlComps = URLComponents(string: “www.apple.com/help”)! urlComps.queryItems = queryItems let … Read more

Parse NSURL query property

You can use queryItems in URLComponents. When you get this property’s value, the NSURLComponents class parses the query string and returns an array of NSURLQueryItem objects, each of which represents a single key-value pair, in the order in which they appear in the original query string. Swift let url = “http://example.com?param1=value1&param2=param2” let queryItems = URLComponents(string: … Read more

openURL: deprecated in iOS 10

Write like this. Handle completionHandler UIApplication *application = [UIApplication sharedApplication]; NSURL *URL = [NSURL URLWithString:@”http://www.google.com”]; [application openURL:URL options:@{} completionHandler:^(BOOL success) { if (success) { NSLog(@”Opened url”); } }]; Without handling completionHandler [application openURL:URL options:@{} completionHandler:nil]; Swift Equivalent:- open(_:options:completionHandler:) UIApplication.shared.open(url)