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
34
votes
1 answer

UILabel and NSLinkAttributeName: Link is not clickable

I want to use attributed strings with NSLinkAttributeName to create clickable links inside a UILabel instance within my iOS 7 project, which is now finally possible without using external libraries. NSURL *url = [NSURL…
Habizzle
  • 2,971
  • 3
  • 20
  • 23
33
votes
3 answers

Initializer for conditional binding must have Optional type, not 'String'

Here is a fun issue I'm running into after updating to Swift 2.0 The error is on the if let url = URL.absoluteString line func myFormatCompanyMessageText(attributedString: NSMutableAttributedString) -> NSMutableAttributedString { // Define text…
mosaic6
  • 923
  • 3
  • 11
  • 18
32
votes
2 answers

Can I create an NSURL that refers to in-memory NSData?

The docs for NSURL state that: 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. I have a blob of…
Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
32
votes
4 answers

Retrieve filename from NSURL

I have a URL http://www.hdwallpapers.in/walls/honda_v4_concept_widescreen_bike-wide.jpg I want to extract the file name which is "honda_v4_concept_widescreen_bike-wide.jpg" How can I can do this?
farisolusa
  • 366
  • 1
  • 3
  • 10
31
votes
11 answers

Url minus query string in Objective-C

What's the best way to get an url minus its query string in Objective-C? An example: Input: http://www.example.com/folder/page.htm?param1=value1¶m2=value2 Output: http://www.example.com/folder/page.htm Is there a NSURL method to do this that…
hpique
  • 119,096
  • 131
  • 338
  • 476
31
votes
5 answers

How To Get HTML source from URL with Swift

I need to look at the HTML of a page given by a certain URL. If I have this, what is the most efficient and synchronous way to get the HTML source for that URL using Swift? I haven't been able to find a concise way online that returns it into a…
Alex Beals
  • 1,965
  • 4
  • 18
  • 26
30
votes
1 answer

What is the correct way to handle stale NSURL bookmarks?

When resolving an NSURL from a security scoped bookmark, if the user has renamed or moved that file or folder, the bookmark will be stale. Apple's document says this regarding staleness: isStale On return, if YES, the bookmark data is stale. Your…
George
  • 1,940
  • 18
  • 27
29
votes
4 answers

Encode '+' using URLComponents in Swift

This is how I add query params to a base URL: let baseURL: URL = ... let queryParams: [AnyHashable: Any] = ... var components = URLComponents(url: baseURL, resolvingAgainstBaseURL: false) components?.queryItems = queryParams.map { URLQueryItem(name:…
Artem Stepanenko
  • 3,423
  • 6
  • 29
  • 51
27
votes
3 answers

Append NSString to NSURL?

I have an NSURL, a file path, and I want to add an NSString to the end of it (the file name) how can I do this? But after this is don't I want the entire thing to be an NSURL. Thanks.
Josh Kahane
  • 16,765
  • 45
  • 140
  • 253
27
votes
3 answers

What is the Swift 3 equivalent of NSURL.URLByAppendingPathComponent()?

I'm following a basic tutorial on building a simple iOS app in Swift. It is written in Swift 2.x, and I work with XCode 8 Beta and Swift 3. The tutorial suggests using NSFileManager to find a data directory. Class names have changed, so the…
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
27
votes
8 answers

Getting URL of UIImage selected from UIImagePickerController

I'm trying to provide a user selection of an image from Photo Library. Thus I'm using UIImagePickerController for selecting. But here comes an issue with getting its initial URL in the file system (I need this for CKAsset). My code. func…
mozharovsky
  • 1,255
  • 2
  • 18
  • 25
26
votes
4 answers

How to send a Get request in iOS?

I am making a library to get response from a particular URL with specified data and method type. For this, I am making a request with url. But when I set its method type, it shows an exception of unrecognized selector send in [NSURLRequest…
Sanchit Paurush
  • 6,114
  • 17
  • 68
  • 107
26
votes
3 answers

How can I retrieve local files with NSURL?

I'm here with a question that probably has a really simple answer that I am overlooking... how can I retrieve local files with NSURL? I have this here: override func viewDidLoad() { super.viewDidLoad() var urlpath =…
Philip Ulrich
  • 263
  • 1
  • 3
  • 6
25
votes
2 answers

Convert file path to file url[NSUrl]

I'm try to file upload operation using AFNetworking multi-part form data. I'm getting following error. I could find out what is error. [NSURL URLWithString:filePath] Also used [[NSURL URLWithString:filePath] filePathURL], But doesn't help me. When I…
Mani
  • 17,549
  • 13
  • 79
  • 100
24
votes
3 answers

NSURL URLWithString:relativeToURL: is clipping relative url

I'm trying to implement an iOS app, which uses RestKit. In all examples I've seen so far the following code is used to create the URLs: NSURL *baseURL = [NSURL URLWithString:@"https://api.service.com/v1"]; NSURL *relativeURL = [NSURL…
Pipo
  • 417
  • 1
  • 4
  • 10