NSUrl FileUrlWith Path is appending " -- file://localhost/" at end - iphone

I am using MPMoviePlayerController to play a video from url.
for this i am getting the link from Xml parser.which is fine.
NSString *path=[[self.items objectAtIndex:videoIndex]objectForKey:#"link"];
i am assigning that path to NSURL fileWithPath as below.
NSURL *mediaUrl = [NSURL fileURLWithPath:path];
While printing the mediaUrl, NSLog is giving "http://example.com -- file://localhost/
"
Why the -- file://localhost/ is appended to the url,Because of this video is not palying.
Any help Please.
Thanks.

Change : NSURL *mediaUrl = [NSURL fileURLWithPath:path];
To : NSURL *mediaUrl = [NSURL URLWithString:path];
As you are calling the fileURLWithPath it's appending file://localhost/ to your URL string.

This is probably a bit old, but NSURL appends the -- file:://localhost if the string that you pass in isn't a valid full path string.
In your case, this is probably because you don't have a string with a "/" at the beginning (ie: "var/test" will have file://localhost appended as you've seen, but if you change it to "/var/test" you'll correctly get an NSURL with "file://localhost/var/test"
If you're trying to do a relative path, you could start with the "~/somelink" and then use stringByExpandingTildeInPath first to get the full path.

Related

NSString won't convert to NSURL (NSURL is null)

I'm trying to convert a NSString (a path to a file in the documents directory) to a NSURL, but the NSURL is always null. Here is my code:
NSURL *urlToPDF = [NSURL URLWithString:appDelegate.pdfString];
NSLog(#"AD: %#", appDelegate.pdfString);
NSLog(#"PDF: %#", urlToPDF);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)urlToPDF);
And here is the log:
2012-03-20 18:31:49.074 The Record[1496:15503] AD: /Users/John/Library/Application Support/iPhone Simulator/5.1/Applications/E1F20602-0658-464D-8DDC-52A842CD8146/Documents/issues/3.1.12/March 1, 2012.pdf
2012-03-20 18:31:49.074 The Record[1496:15503] PDF: (null)
I think part of the problem might be that the NSString contains slashes / and dashes -. What am I doing incorrectly? Thanks.
Why don't you create your file path in this way which is.
NSString *filePath = [[NSBundle mainBundle]pathForResource:#"pdfName" ofType:#"pdf"];
And then create your url with file path like this.
NSURL *url = [NSURL fileURLWithPath:filePath];
The thing is, appDelegate.pdfString isn't a valid URL, it's a path. A file URL looks like:
file://host/path
or for the local host:
file:///path
So you actually want:
NSURL *urlToPDF = [NSURL URLWithString:[NSString stringWithFormat:#"file:///%#", appDelegate.pdfString]];
...except your path has spaces, which need to be URL encoded, so you actually want:
NSURL *urlToPDF = [NSURL URLWithString:[NSString stringWithFormat:#"file:///%#", [appDelegate.pdfString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];

Remove ".." from string representation of URL

When you have a filesystem path, you can have a ".." removed (and the previous path component removed as well) by using the stringByResolvingSymlinksInPath selector. How can I achieve the same thing for a URL? For example I start out with, say:
www.example.com/themes/themeA/../common/assetA.png
Which I need converted to:
www.example.com/themes/common/assetA.png
For a URL use the NSURL method:
- (NSURL *)standardizedURL
Returns a new URL that points to the same resource as the original URL and is an absolute path.
Example:
NSString *s = #"www.example.com/themes/themeA/../common/assetA.png";
NSURL *u = [NSURL URLWithString:s];
NSURL *su = [u standardizedURL];
NSLog(#"su: %#", su);
NSLog output:
su: www.example.com/themes/common/assetA.png
What about the following?
NSString* resolved_url
= [[[NSURL URLWithString: #"www.example.com/themes/themeA/../common/assetA.png"] standardizedURL] absoluteString];
If you want NSURL instead of NSString, remove call to absoluteString.

Escaping special characters (ø, æ) for use inside a url

I try to display an image with a URL source in an iOS application, but it doesn't show up.
The url of the image is live example path.
When escaping this string using the following Objective-C code:
NSString *url= [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)originalpath, NULL, CFSTR("øæ"), kCFStringEncodingUTF8) autorelease];
the result is (with encoding of øæ) : live xml path
All my files where the URLs are stored use text encoding (UTF-8).
How do I escape the URL in a right way, such that the image will be displayed?
don't go for ascii encoding try it
NSString *URLString = [yourImagepath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:URLString];
I hope it will help you out.
I believe you need to escape your url-string, before using it as an url...
NSString *URLString = [yourImagepath stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:URLString];
Had the same problem on iOS with German umlauts. The NSURL category NSURL+IFUnicodeURL solved the problem for me, don't get scared by the amount of C code in there, it's well-hidden :)

NSString to NSURL?

Trying to convert a string to NSURL and this is not happening.
barcodeTextLabel.text = foundCode.barcodeString;
urlToGrab = [NSString stringWithFormat:#"%#", foundCode.barcodeString]; // foundCode.barcodeString is an NSString
urlToGrab shows the following "error invalid CFStringRef"
This is how you create an NSURL from an NSString:
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
You can use following for creating the file path to url.
NSURL *yourURL = [NSURL fileURLWithPath:#"/Users/xyz/Desktop/abc.sqlite"];
If foundCode.barcodeString is the string you want as your URL, then (like the above answer) use the NSURL class method URLWithString:(NSString *).
Your code should look like:
NSURL urlToGrab = [NSURL URLWithString:foundCode.barcodeString];
Where is your error coming into to play? The way your code is, urlToGrab is an instance of NSString. I would imagine you would get an error like you described if you tried to make an HTTP request on an NSString rather than NSURL.
Swapnali patil's answer works, but I will add an explanation.
You will get a nil if the format of NSString doesn't fit file criteria for NSURL (file:///xxx/file.ext).
My needs were with loading a JPG image via URL file path to nsdata; NSURL * u=[[NSURL alloc] initWithString:fpath] returned nil, but NSURL *yourURL = [NSURL fileURLWithPath:fpath] as in mentioned answer worked. A URL for files will be file:///users/xxx/pic.jpg format and give disk access. NSURL * u=[[NSURL alloc] initWithString:(NSString*) ] will also give nil object if nsstring is web URL but if missing http://

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] initWithString:dialThis];
NSLog(#"%#",url);
also tried
NSURL *url = [NSURL URLWithString:dialThis];
but it gives null . what is wrong ?
Thanks..
Your problem is the unescaped spaces in the URL. This, for instance, works:
NSURL *url = [NSURL URLWithString:#"tel://1234567890x101"];
Edit: As does this..
NSURL *url2 = [NSURL URLWithString:[#"tel://1234567890 ext. 101"
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Before passing any string as URL you don't control, you have to encode the whitespace:
NSString *dialThis = [prefix stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// tel://1234567890%20ext.%20101
As a side note, iOS is not going to dial any extension. The user will have to do that manually.
From Apple URL Scheme Reference: Phone Links:
To prevent users from maliciously redirecting phone calls or changing the behavior of a phone or account, the Phone application supports most, but not all, of the special characters in the tel scheme. Specifically, if a URL contains the * or # characters, the Phone application does not attempt to dial the corresponding phone number.
Im not sure the "ext." in phone number can be replce by what value? but you can try like this,
NSString *prefix = [NSString stringWithString: #"tel://1234567890 ext. 101"];
NSString *dialThis = [NSString stringWithFormat:#"%#", prefix];
NSURL *url = [NSURL URLWithString:[dialThis stringByReplacingOccurrencesOfString:#" ext. " withString:#"#"]];
// it might also represent by the pause symbol ','.
you can go to find the ext. is equivalent to what symbol in the phone, then replace it.
but dunno it can be work in actual situation or not....
As with iOS 9.0,
stringByAddingPercentEscapesUsingEncoding:
has been deprecated.
Use the following method for converting String to NSURL.
let URL = "URL GOES HERE"
let urlString = URL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())
If you've got something you think should be a URL string but know nothing about how URL strings are supposed to be constructed, you can use NSURL's URLWithDataRepresentation:relativeToURL: method. It parses the URL string (as bytes in an NSData) and percent-encodes characters as needed. Use the NSUTF8StringEncoding for best results when converting your NSString to NSData.
NSURL *url = [NSURL URLWithDataRepresentation:[#"tel:1234567890 ext. 101" dataUsingEncoding:NSUTF8StringEncoding] relativeToURL:nil];
NSLog(#"%#",url);
creates a URL with the string 1234567890%20ext.%20101
It attempts to do the right thing. However, for best results you should find the specification for the URL scheme you using and follow it's syntax to create your URL string. For the tel scheme, that is https://www.rfc-editor.org/rfc/rfc3966.
P.S. You had "tel://" instead of "tel:" which is incorrect for a tel URL.
Try this one, It works for me....
NSString *prefix = (#"tel://1234567890 ext. 101");
NSString *dialThis = [NSString stringWithFormat:#"%#", prefix];
NSURL *url = [NSURL URLWithString:[queryString stringByReplacingOccurrencesOfString:#" " withString:#"%20"]];
NSLog(#"%#",url);
Make an extension for use in any part of the project as well:
extension String {
var asNSURL: NSURL! {
return NSURL(string: self)
}
}
From now you can use
let myString = "http://www.example.com".asNSURL
or
myString.asNSURL