Append NSString to NSURL? - objective-c

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.

I think it's good solution:
NSURL *bUrl = [aUrl URLByAppendingPathComponent:#"newString"];
In Swift you could do the following,
var bURL = aURL.URLByAppendingPathComponent( "newString" )
You can also state whether the URL is a directory,
var bURL = aURL.URLByAppendingPathComponent( "newString", isDirectory: true )

I think it's as simple as:
NSString *s = [aUrl.path stringByAppendingString:#"newString"];

If you have a file NSURL to a directory and you want to end up with a NSString containing the NSURL's path with a file name appended to it, use this:
NSURL *url = [NSURL fileURLWithPath:#"/System" isDirectory:YES];
NSString *filename = #"foo";
NSString *result = [url.path stringByAppendingPathComponent:filename];
You can also use URLByAppendingPathComponent but that adds an extra step which creates an extra NSURL object that isn't needed.
NSURL *url = [NSURL fileURLWithPath:#"/System" isDirectory:YES];
NSString *filename = #"foo";
NSURL *newURL = [url URLByAppendingPathComponent:filename];
NSString *result = newURL.path;

Related

replace in NSURL

How can I do a string replace in NSURL?
I tried stringByReplacingOccurrencesOfString but it works with NSString.
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
I want to replace image url
http://example.com/image1.jpg
to
http://example.com/img1.jpg
NSURL has an absoluteString method that you could use like so
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSString *urlString = [imageURL.absoluteString stringByReplacingOccurrencesOfString:#"image" withString:#"img"];
imageURL = [NSURL URLWithString:urlString];
You could also directly operate on the NSString from the dataList as well:
NSString *urlString = [dataList[indexPath.item][#"image"] stringByReplacingOccurrencesOfString:#"image" withString:#"img"];
imageURL = [NSURL URLWithString:urlString];
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSString *urlString = [imageURL.absoluteString stringByReplacingOccurrencesOfString:#"image" withString:#"img"];
NSString *webStringURL = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
imageURL = [NSURL URLWithString:webStringURL];
Hope This will help you.
You are going to replace the last path component of the URL so use the dedicated API of NSURL:
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSURL *newURL = [[imageURL URLByDeletingLastPathComponent] URLByAppendingPathComponent:#"img.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL: newURL];
stringByReplacingOccurrencesOfString could cause unexpected behavior.

NSURL unused variable

I have generated a PDF file within my application which is saved to the Documents directory. I am trying to fetch the pdf to display within a UIWebView but i have an unused NSURL variable. Could this be the reason why the file is not loading properly? Here is my code which is attached to an IBAction button
The only way to get rid of that warning is to NSLog that NSURL. But I want to display that saved file.
This is the code I am using.
-(IBAction)pdf:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathExtension:#"client.pdf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSString *urlString = [url absoluteString];
NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *webURL = [NSURL URLWithString:encodedString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:request];
NSLog(#"%#", webURL);
}
I figured it out. I needed to change my filePath NSString from stringByAppendingPathExtension to stringByAppendingPathcomponent. I can now view my PDF file in my UIWebView.

Contents from url to string

NSString *urlAddress = [NSString stringWithFormat:#"http://www.domain.com?input=%#",[alertView textFieldAtIndex:0].text];
NSURL *myUrl = [NSURL URLWithString:[urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *openURL = [NSString stringWithContentsOfURL:myUrl encoding:NSUTF8StringEncoding error:NULL];
NSLog(#"%#",openURL);
openUrl is always returning (null), probably because of the encoding, but I don't know how to fix it.
Double check these three things:
Check that the URL which you are pointing to is a valid URL. The null value could be a result of an invalid URL.
Catch the NSError which the stringWithContentsOfURL throws. That will give you some insight on what is wrong.
Like you suspect, try changing the string encoding.
NSError *error;
NSString *urlString = [NSString stringWithFormat:#"http://www.domain.com?input=%#", stringVariable];
NSURL *urlAdress = [NSURL URLWithString:urlString];
NSString *urlContent = [[NSString alloc] initWithContentsOfURL:urlAdress encoding:NSUTF8StringEncoding error:&error];
NSLog(#"%#", urlContent);
This works fine, the problem was an failing internet connection.

How to append a variable to a string in xcode

Im trying to to append a variable to a string but its showing an error. Im missing something ridiculously easy here but my mind is shot.
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSURL *urla = [NSURL URLWithString:#"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=",eID];
You can't concat 2 string variables just by putting a comma between them. Try this instead:
NSURL *urla = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=%#",eID]];
If all you are doing is appending, you have several options:
A. Use NSString & stringWithFormat:
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSString *urlString = [NSString stringWithFormat:#"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=%#", eID];
NSURL *urla = [NSURL URLWithString:urlString];
B. Use NSString & stringByAppendingString:
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSString *baseUrl = #"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=";
NSString *urlString = [baseUrl stringByAppendingString:eID];
NSURL *urla = [NSURL URLWithString:urlString];
C. Use NSMutableString & appendString:
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSString *baseUrl = #"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=";
NSMutableString *urlString = [NSMutableString stringWithString:baseUrl];
[urlString appendString:eID];
NSURL *urla = [NSURL URLWithString:urlString];
Try like this:-
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSString *url=#"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=";
NSURL *urla = [NSURL URLWithString:[url stringByAppendingString:eID];

NSURL returns Nil Value

Here Below is my code
NSString *string = [NSString stringWithFormat:#" http://abc.com /Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#& ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];
Here in string there is value but url returns nil.
Can Anyone tell why this happened.
Thanks ....
"This won't work, so here's what I did instead"
NSString *string = [NSString stringWithFormat:#"http://abc.com/Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#&ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];
NSURL will return nil for URLs that contain illegal chars, like spaces.
Before using your string with [NSURL URLWithString:] make sure to escape all the disallowed chars by using [NSString stringByAddingPercentEscapesUsingEncoding:].
Here is the class reference for NSString.
Hi All Now My Problem Is solved here I did a mistake left a blank space before "http".
The correct code is
NSString *string = [NSString stringWithFormat:#"http://myserver.com/Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#&ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];