What is difference between URLWithString and fileURLWithPath of NSURL?

+URLWithString: produces an NSURL that represents the string as given. So the string might be @”http://www.google.com” and the URL represents http://www.google.com. +fileURLWithPath: takes a path, not a URL, and produces an NSURL that represents the path using a file:// URL. So if you give it /foo/bar/baz the URL would represent file:///foo/bar/baz. You can of course … Read more

How To Get HTML source from URL with Swift

Disclaimer : Since this is getting quite a lot of views, I just want to remind everyone that this answer here is synchronous, and will block your app if you do it on the main thread. You should always do this asynchronously (in a background thread), but the question asked for a synchronous method, so … Read more

URLWithString: returns nil

You need to escape the non-ASCII characters in your hardcoded URL as well: //localisationName is a arbitrary string here NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString* stringURL = [NSString stringWithFormat:@”http://maps.google.com/maps/geo?q=%@,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false”, webName]; NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL* url = [NSURL URLWithString:webStringURL]; You can probably remove the escaping of the localisationName since it will be handled by … 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

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)

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