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
81
votes
6 answers

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

Can I somehow do a synchronous HTTP request via NSURLSession in Swift? I can do an asynchronous request via the following code: if let url = NSURL(string: "https://2ch.hk/b/threads.json") { let task =…
FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
76
votes
3 answers

NSURL path vs absoluteString

I've seen many questions on SO concerning converting between NSURL and NSString. They all involve using either NSString *path = [myURL absoluteString]; or NSString *path = [myURL path];. What is the actual difference between these methods? Is…
GeneralMike
  • 2,951
  • 3
  • 28
  • 56
75
votes
6 answers

Testing file existence using NSURL

Snow Leopard introduced many new methods to use NSURL objects to refer to files, not pathnames or Core Services' FSRefs. However, there's one task I can't find a URL-based method for: Testing whether a file exists. I'm looking for a URL-based…
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
66
votes
8 answers

Objective-C: How to add query parameter to NSURL?

Let's say I have an NSURL? Whether or not it already has an empty query string, how do I add one or more parameters to the query of the NSURL? I.e., does anyone know of an implementation of this function? - (NSURL…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
60
votes
12 answers

Replace occurrences of space in URL

I have a URL in an iPhone application to work with. But the problem is that it has some spaces in the URL. I want to replace the spaces with '%20'. I know that there are the stringByReplacingOccurencesOfString and…
Joy
  • 1,609
  • 3
  • 16
  • 28
55
votes
9 answers

iOS: load an image from url

I need to load an image from a url and set it inside an UIImageView; the problem is that I don't know the exact size of the image, then how can I show the image correctly?
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
51
votes
5 answers

NSURL URLWithString: is null with non-english accented characters

I have the following string... NSString *googleSearchString = @"http://www.google.com/search?q=lyrics+%22Tænder+På+Dig%22+%22Jakob+Sveistrup%22"; Notice that it has some accented characters. When I try to turn that into a url the returned url is…
regulus6633
  • 18,848
  • 5
  • 41
  • 49
50
votes
2 answers

URLComponents.url is nil

I'm trying to use URLComponents to compose a URL as that appears to be what it's for. However, when I then access the url property of the components it is nil. Example... var urlComponents = URLComponents(string:…
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
46
votes
13 answers

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

I have converted the code for making a phone call from Objective-C to Swift, but in Objective-C, we can set the type of the URL that we like to open (e.g. telephone, SMS, web) like…
user3739367
  • 4,161
  • 6
  • 20
  • 18
42
votes
6 answers

Convert String to NSURL is return nil in swift

I am trying to convert a String to NSURL and my code for that is Below: var url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=\(self.latitude),\(self.longitude)&destinations=\(self.stringForDistance)&language=en-US" …
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
42
votes
13 answers

NSURL pull out a single value for a key in a parameter string

I have an NSURL: serverCall?x=a&y=b&z=c What is the quickest and most efficient way to get the value of y? Thanks
Dave
  • 433
  • 1
  • 5
  • 7
40
votes
3 answers

NSData & NSURL - url with space having problem

I have following code in my application. NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]]; pathOfThumbNail has following path http://70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg When I open above path in…
sagarkothari
  • 24,520
  • 50
  • 165
  • 235
38
votes
1 answer

How to convert NSURL to String in Swift

How do you convert an NSURL to a String in Swift? The following: var directoryURL: NSURL var urlString: String = nsurlObject as String Throws the error: Cannot convert value of type 'NSURL' to type 'String' in coercion
JBaczuk
  • 13,886
  • 10
  • 58
  • 86
36
votes
7 answers

NSURL with string

I have problem with NSURL. I am trying to create NSURL with string code NSString *prefix = (@"tel://1234567890 ext. 101"); NSString *dialThis = [NSString stringWithFormat:@"%@", prefix]; NSURL *url = [[NSURL alloc]…
Maulik
  • 19,348
  • 14
  • 82
  • 137
35
votes
7 answers

Swift: save video from NSURL to user camera roll

I have a variable videoURL of type NSURL. If I call println(videoURL) it would return something like this: http://files.parsetfss.com/d540f71f-video.mp4 I have a button set up that should take this videoURL and save the video to the user's camera…
George Poulos
  • 539
  • 1
  • 6
  • 17