-1002 NSURLErrorUnsupportedURL / kCFURLErrorUnsupportedURL . In the future, you can either search the Xcode documentation for NSURLErrorDomain , or use the quick open ( shift + command + O ) so that browsers display headers for determining NSURLErrorDomain . Any of these methods would let you find that -1002 in NSURLErrorDomain is equal to NSURLErrorUnsupportedURL .
The reason for this error is that your URL contains some characters that should be skipped. And web browsers will often do the necessary percentage acceleration for you, so it works there.
You can use stringByAddingPercentEscapesUsingEncoding to convert the URL to an acceptable format:
NSString *urlString = @"https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/PrintProgressReport/20152193^HS4"; NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSURLSessionTask *task = [defaultSession dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { ... }];
By the way, when comparing the results of a web browser with Charles, itβs very useful. Run the query from the browser and again from the application and compare the results in Charles. If you compared them, you would see that you need the percentage code not to get into the URL.
By the way, you can also refer to Section 2 of RFC 3986 for a technical description of which characters in URLs should be omitted as a percentage.
source share