Questions tagged [nsurl]

NSURL is a class from Foundation.framework in Apple MacOS and iOS. The NSURL class provides the ability to manipulate URLs and the resources they refer to. An NSURL object represents a URL that can potentially contain the location of a resource on a remote server, the path of a local file on disk, or even an arbitrary piece of encoded data. It is available in iOS 2.0 and later

The NSURL class provides a way to manipulate URLs and the resources they reference. NSURL objects understand URLs as specified in RFCs 1808, 1738, and 2732. The litmus test for conformance to RFC 1808 is as recommended in RFC 1808—whether the first two characters of resourceSpecifier are slashes (//).

An NSURL object is composed of two parts — a potentially nil base URL and a string that is resolved relative to the base URL. An NSURL object whose string is fully resolved without a base is considered absolute; all others are considered relative.

The NSURL class fails to create a new NSURL object if the path being passed is not well-formed; the path must comply with RFC 2396.

The NSURL class is toll-free bridged with its Core Foundation counterpart, CFURLRef.

Sample:

For example, when constructing an NSURL object, you might specify file:///path/to/web_root/ as the base URL and folder/file.html as the string part, as follows:

NSURL *baseURL = [NSURL URLWithString:@"file:///path/to/web_root/"];
NSURL *url = [NSURL URLWithString:@"folder/file.html" relativeToURL:baseURL];
NSURL *absURL = [url absoluteURL];
NSLog(@"absURL = %@", absURL);

Resource

1649 questions
514
votes
39 answers

Loading/Downloading image from URL on Swift

I'd like to load an image from a URL in my application, so I first tried with Objective-C and it worked, however, with Swift, I've a compilation error: 'imageWithData' is unavailable: use object construction 'UIImage(data:)' My function:…
QuentR
  • 5,227
  • 3
  • 13
  • 12
468
votes
8 answers

How do I load an HTTP URL with App Transport Security enabled in iOS 9?

So, the new beta SDK of iOS released last night has "App Transport Security" which encourages developers to use https instead of http. In principle, this is a great idea, and I already use https in our staging/production environments. However, I…
Graeme Mathieson
  • 5,108
  • 3
  • 17
  • 9
345
votes
7 answers

Convert an NSURL to an NSString

I have an app where the user can choose an image either from the built-in app images or from the iphone photo library. I use an object Occasion that has an NSString property to save the imagePath. Now in the case of the built-in app images I do get…
Ali
  • 4,205
  • 4
  • 25
  • 40
199
votes
10 answers

Converting URL to String and back again

So I have converted an NSURL to a String. So if I println it looks like file:///Users/... etc. Later I want this back as an NSURL so I try and convert it back as seen below, but I lose two of the forward slashes that appear in the string version…
Gary Simpson
  • 2,677
  • 2
  • 17
  • 18
114
votes
2 answers

Get parts of a NSURL in objective-c

I have an NSString with the value of http://digg.com/news/business/24hr How can I get everything before the 3rd level? http://digg.com/news/
Melina
  • 1,463
  • 4
  • 13
  • 13
113
votes
5 answers

How to convert this var string to URL in Swift

I need url filepath be a URL (NSURL in old versions of Swift). I have this: let paths = NSSearchPathForDirectoriesInDomains( .documentDirectory, .userDomainMask, true) // NSString *documentsDirectory = [paths objectAtIndex:0]; let…
user3745888
  • 6,143
  • 15
  • 48
  • 97
94
votes
8 answers

URLWithString: returns nil

it may be very easy, but I don't seems to find out why is URLWithString: returning nil here. //localisationName is a arbitrary string here NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];…
gcamp
  • 14,622
  • 4
  • 54
  • 85
92
votes
13 answers

Creating URL query parameters from NSDictionary objects in ObjectiveC

With all the URL-handling objects lying around in the standard Cocoa libraries (NSURL, NSMutableURL, NSMutableURLRequest, etc), I know I must be overlooking an easy way to programmatically compose a GET request. Currently I'm manually appending "?"…
user87229
91
votes
6 answers

openURL: deprecated in iOS 10

Apple with iOS 10 has deprecated openURL: for openURL:option:completionHandler If I have: [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.google.com"]]; How it will become? options:<#(nonnull NSDictionary
Joannes
  • 2,569
  • 3
  • 17
  • 39
89
votes
12 answers

How to fix "NSURLErrorDomain error code -999" in iOS

I've been trying to use Corona SDK's Facebook API to post the score on the game I'm developing on facebook. However, I'm having a problem with it. During the first time I try to post to facebook, I get this error after login and user…
user1597438
  • 2,171
  • 5
  • 35
  • 78
88
votes
1 answer

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

I can't understand what's the difference between these two ways of calling: NSURLComponents(URL: url, resolvingAgainstBaseURL: true) and NSURLComponents(URL: url, resolvingAgainstBaseURL: false) And I found the explanation of documentation was…
SomeHowWhite
  • 931
  • 1
  • 6
  • 9
88
votes
8 answers

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

I am using AFNetworking in my iOS app and for all the GET requests it makes, I build the url from a base URL and than add parameters using NSDictionary Key-Value pairs. The problem is that I need same key for different values. Here is an example of…
AbhishekDwivedi
  • 883
  • 1
  • 7
  • 5
88
votes
5 answers

iPhone - NSData from local file's URL

I have a NSURL object which gives me the path of a local file (in documents folder). I want to populate an NSData object with the contents of this file. Tried using dataWithContentsOfURL: but this fails. I know the file exists because the iPhone SDK…
lostInTransit
  • 70,519
  • 61
  • 198
  • 274
86
votes
17 answers

Parse NSURL query property

I have a URL like myApp://action/1?parameter=2&secondparameter=3 With the property query I get following part of my URL parameter=2&secondparameter=3 Is there any way easy to put this in a NSDictionary or an Array? Thx a lot
gabac
  • 2,062
  • 6
  • 21
  • 30
86
votes
8 answers

OSX Swift open URL in default browser

How to open a URL in the system default browser by using Swift as programming language and OSX as platform? I found a lot with UIApplication like: UIApplication.sharedApplication().openURL(NSURL(string: object.url)) but this works just on iOS and…
user2929462
  • 873
  • 1
  • 6
  • 5
1
2 3
99 100