How to use openURL for making a phone call in Swift?

I am pretty sure you want: UIApplication.sharedApplication().openURL(NSURL(string: “tel://9809088798”)!) (note that in your question text, you put tel//:, not tel://). NSURL’s string init expects a well-formed URL. It will not turn a bunch of numbers into a telephone number. You sometimes see phone-number detection in UIWebView, but that’s being done at a higher level than NSURL. … Read more

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

UILabel and NSLinkAttributeName: Link is not clickable

I can answer my own question now: I am using UITextView instead of UILabel now. I have formatted the UITextView to look and behave like my labels and added: UITextView *textView = [[UITextView alloc] init]; textView.scrollEnabled = NO; textView.editable = NO; textView.textContainer.lineFragmentPadding = 0; textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0); textView.delegate = self; Don’t forget … 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

iOS: Issue with ampersand in the URL string

Or even shorter: @implementation NSString (Escaping) – (NSString*)stringWithPercentEscape { return [(NSString *) CFURLCreateStringByAddingPercentEscapes( NULL, (CFStringRef)[[self mutableCopy] autorelease], NULL, CFSTR(“=,!$&'()*+;@?\n\”<>#\t :/”), kCFStringEncodingUTF8) autorelease]; } @end And here it is again as an ARC conform inline function helper: #if __has_feature(objc_arc) static inline NSString *hxURLEscape(NSString *v) { static CFStringRef _hxURLEscapeChars = CFSTR(“=,!$&'()*+;@?\r\n\”<>#\t :/”); return ((__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes( NULL, … Read more

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

NSURL path vs absoluteString

Question 1: What is the actual difference between these methods? Let’s analyze this writing 6 lines of code – 3 for a local and 3 for http URL – and playing around with them a little bit. Let’s create an NSURL using the file:// scheme. If you ask yourself why there are 3 / after … Read more

How to use special character in NSURL?

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