NSURL and NSString converting issue - iphone

When trying to load a request I do like this:
NSString *urlStrings = [NSLocalizedStringFromTable(#"kPicturesURL", #"urls", nil) stringByAppendingPathComponent:#"Articles/a.pdf"];
NSURL *url = [NSURL URLWithString:urlStrings];
Inside of "urls" I have this key:
/* Service pictures directory */
"kPicturesURL" = "http://192.168.2.104/myApp/Pictures";
And I get this result: (The first line is "urlStrings" and the second is the "url")
(NSString *) $6 = 0x092a8f60 http:/192.168.2.104/myApp/Pictures/Articles/a.pdf
2012-11-22 09:18:20.093 NPE[7680:c07] Couldn't issue file extension for path: /192.168.2.104/myApp/Pictures/Articles/a.pdf
I've tried those questions:
NSString and NSUrl not converting properly
NSURL not getting allocatd with NSString
Pass NSString into NSURL URLWithString ?
NSString to NSURL ?
NSString to NSURL
Non of those worked, what seems to be the problem?
Thanks!

Maybe instead of:
http:/192.168.2.104/myApp/Pictures/Articles/a.pdf
This:
http://192.168.2.104/myApp/Pictures/Articles/a.pdf

So, using one of the following did worked eventually.
This one:
NSString *stringURL = [url absoluteString];

Related

Remove last portion of the NSURL: iOS

I am trying to remove just the last part of the url, Its a FTP URL.
Suppose, I have a URL like: > ftp://ftp.abc.com/public_html/somefolder/. After removing the last portion I should have it as: ftp://ftp.abc.com/public_html/.
I have tried using stringByDeletingLastPathComponenet and URLByDeletingLastPathComponent, but they dont remove the last portion correctly. They change the entire looks of the url.
for instance, after using the above said methods, here is the URL format i get ftp:/ftp.abc.com/public_html/. It removes one "/" in "ftp://", which is crashing my program.
How is it possible to removve just the last part without disturbing the rest of the URL ?
UPDATE:
NSURL * stringUrl = [NSURL URLWithString:string];
NSURL * urlByRemovingLastComponent = [stringUrl URLByDeletingLastPathComponent];
NSLog(#"%#", urlByRemovingLastComponent);
Using above code, I get the output as :- ftp:/ftp.abc.com/public_html/
Hmm. URLByDeletingLastPathComponent works perfectly given the above input.
NSURL *url = [NSURL URLWithString:#"ftp://ftp.abc.com/public_html/somefolder/"];
NSLog(#"%#", [url URLByDeletingLastPathComponent]);
returns
ftp://ftp.abc.com/public_html/
Do you have some sample code that is yielding improper results?
Max
Now try
NSString* filePath = #"ftp://ftp.abc.com/public_html/somefolder/.";
NSArray* pathComponents = [filePath pathComponents];
NSLog(#"\n\npath=%#",pathComponents);
if ([pathComponents count] > 2) {
NSArray* lastTwoArray = [pathComponents subarrayWithRange:NSMakeRange([pathComponents count]-2,2)];
NSString* lastTwoPath = [NSString pathWithComponents:lastTwoArray];
NSLog(#"\n\nlastTwoArray=%#",lastTwoPath);
NSArray *listItems = [filePath componentsSeparatedByString:lastTwoPath];
NSLog(#"\n\nlist item 0=%#",[listItems objectAtIndex:0]);
}
output
path=(
"ftp:",
"ftp.abc.com",
"public_html",
somefolder,
"."
)
lastTwoArray =somefolder/.
list item 0 =ftp://ftp.abc.com/public_html/
An example of how to extract the last part of NSURL. In this case the location of the file. Sqlite core data
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"CoreAPI.sqlite"];
NSString *localPath = [storeURL absoluteString];
NSArray* pathComponents = [localPath pathComponents];
NSLog(#"%#",[pathComponents objectAtIndex:6]);
NSString * nombre = [NSString stringWithFormat:#"%#", [pathComponents objectAtIndex:6]];
This code returns me the name of the file CoreAPI.sqlite

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]]];

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://

How to concatenate an nsstring url

How can I concatenate an NSString URL? For example:
http://isomewebsite.com/username=[someuservariable]&password=[somevariable]
Try with below code.
NSString* myURLString = [NSString stringWithFormat:#"somewebsite.com/username=%#&password=%#", myUserName,myPassword];
NSURL *url = [NSURL URLWithString:myURLString];
Here is the blog tutorial will help you know more about Handling url authentication challenges accessing password protected servers
You can use the method stringWithFormat of NSString:
NSString *url = [NSString stringWithFormat:#"somewebsite.com/username=%#&password=%#", someuservariable, some variable];
Try this excellent post - URL encoding an NSString on iOS

NSURL declaration /allocation showing error

I Have tried
NSString *alertTxt =textfieldName.text;
NSLog(#"Name: %#",alertTxt);
NSURL *urlAddress = [[NSURL alloc ] initWithString: #"%#/login/index.php",alertTxt];
error : too many argument to function initstring ..
the user will giv the url address to the alertText field and i hav to embed in to the webkit may i know the process how to make it !!!
Where im making mistake kindly help me out in this
Thanks
-initWithString: can only take a complete string, not a format string. To use -initWithString: you need to complete the string first.
NSString* urlString = [NSString stringWithFormat:#"%#/login/index.php", alertTxt];
NSURL* urlAddress = [[NSURL alloc] initWithString:urlString];
BTW, it may be more efficient to use -stringByAppendingString:.
NSString* urlString = [alertTxt stringByAppendingString:#"/login/index.php"];