Why does this:
NSString *url = [NSString stringWithFormat:#"http://www.example.com/profile/%#/?s_iphone=true", author];
NSLog(#"url: %#", url);
Output this:
http://www.example.com/profile/AuthorName
/?s_iphone=true
Needless to say the url won't load b/c there is a newline added to the string by itself. I've tried removing whitesapces/newlines and still had the same output. Its driving me crazy.
Matt
Try this -
NSString *url = [NSString stringWithFormat:#"http://www.example.com/profile/%#/?s_iphone=true", [author stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
NSLog(#"url: %#", url);
It will fix any new line characters at the beginning and end of your author string.
Author definitely has a new line in it. Try this:
NSLog(#"The author is %# on a new line?", author);
Related
for(int i= 0 ;i<[urlsArrray count]; i++)
{
NSString *urlString = [urlsArrray objectAtIndex:i];
NSString *escapedUrlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:escapedUrlString];
NSString *urlstring1 = [url absoluteString];
NSArray *parts = [urlstring1 componentsSeparatedByString:#"/"];
NSString *fileName = [parts objectAtIndex:[parts count]-1];
NSMutableString *tempString = [NSMutableString stringWithString:fileName];
// [tempString replaceCharactersInRange:[tempString rangeOfString:#"%20"] withString:#" "];
NSLog(#"file name in temp string: %# word name: %#", tempString, wordNameDB);
NSRange match = [tempString rangeOfString:wordNameDB];
if(match.location != NSNotFound)
{
NSLog(#"match found at %u", match.location);
isAvailable = YES;
break;
}
Hi friends, now my problem is i am getting file name from server..., if file name is having any spaces then it replace '%20' ( i.e ex: "hello world" is actual name but i am getting file name like: "hello%20world") .
1. I am not sure all file names having spaces.
2. And also i am not sure a file may have only one space
so first i have to check the file is having spaces or not, if have then i want to replace all "%20" with #" " string. Please give me any suggestions or code snippets.
OR " THERE IA ANY OTHER WAY TO READ FILE NAMES WITHOUT GETTING '%20' IN THE PLACE OF SPACE(#" ")..... thank you
If you have your file name stored in fileName param, you can use the following:
fileName = [fileName stringByReplacingOccurrencesOfString:#"%20" withString:#" "];
The above code will replace all "%20" with " ". If there are no "%20" in the fileName, you will get back the same string.
Correction:
I was confused with stringByAddingPercentEscapesUsingEncoding mentioned in code and thought you have already used stringByReplacingPercentEscapesUsingEncoding. If you are not using stringByReplacingPercentEscapesUsingEncoding method, you should use that in this case. The above code is useful, only if that is not able to remove any particular string which you want to replace.
What you need is replacing the escape charcters, according to the encoding.
Use this and all your spaces and other URL encoded characters will be converted to what you need.
[#"yourString" stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
THERE IA ANY OTHER WAY TO READ FILE NAMES WITHOUT GETTING '%20' IN THE PLACE OF SPACE(#" ")
Yes, use this:
NSString *newString = [yourstring stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Use this to remove spaces ..
urlString = [urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
You seem to already have a valid NSURL object representing the file. Getting the filename from a URL is easy:
...
NSURL *url = [NSURL URLWithString:escapedUrlString];
NSString *path = [url path];
NSString *filename = [path lastPathComponent];
No fiddling with unescaping percent escapes, URL parsing, and other error prone stuff.
How to replace the following correctly?
NSString *stringURL = [NSString stringWithFormat:#"http://192.168.1.183:8001/GetDocument.aspx?id=%# & user=admin_document",self.index];
NSURL *targetURL = [NSURL URLWithString:stringURL];
I want to replace %# with self.index.
Your problem is more likely the whitespace. Try removing it:
NSString *stringURL = [NSString stringWithFormat:#"http://192.168.1.183:8001/GetDocument.aspx?id=%#&user=admin_document", self.index];
NSLog(#"URL:%#", stringURL); //You can print out to check
NSURL *targetURL = [NSURL URLWithString:stringURL];
Remove the whitespaces in the link.
If self.index is not an NSString object, then the %# format specifier will not work. I suspect with a name like index it is probably an NSInteger or similar, in which case you want to use %d instead.
Have a look at this Apple documentation for all the possible format specifiers and the types they correspond to. The compiler should pick you up on this though and suggest the correct specifier.
just parse and use it if it is integer then you use like this
int a=10;
NSString *stringURL = [NSString stringWithFormat:#"this is my number %d",a];
and if you want to use string then you did like this
NSString stringName=#"john";
NSString *stringURL = [NSString stringWithFormat:#"this is my name %#",stringName];
I am making request on server , having spaces in URL
http://xxxxxxxx.com/api/api.php?func=showAllwedsdwewsdsd¶ms[]=Saudi%20Arab¶ms[]=all
I was getting the error Bad URL so ,
I used
downloadURL = [downloadURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
but due this I am getting strange URL as
http://sdsdsdsdsdsd.com/api/api.php?func=showAllsdsd¶ms5262721536=Saudi 0X0P+0rabia¶ms5 8288=All
I am also using
downloadURL= [downloadURL stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
but again strange url like
http://xxxxxxxxx.com/api/api.php?func=showsdsdsd¶ms[]=Saudi 0X1.240DC0D824P-807rabia¶ms[]=All
Please help how can I solve this issue
Edit Pasting Big portion of code
PropertyXMLDownloader *download=[[PropertyXMLDownloader alloc]init];
[download setDelegate:self];
firstAppDelegate *del=(firstAppDelegate *)[[UIApplication sharedApplication]delegate];
NSLog(#"Country is %#",del.country);
NSLog(#"State is %#",del.state);
// del.country=[del.country stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSString *downloadURL=[NSString stringWithFormat:#"http://xxxxxxxx.com/api/api.php?func=showAll¶ms[]=Saudi Arabia¶ms[]=%#",#"all"];
// downloadURL= [downloadURL stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
//downloadURL = [downloadURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#" "];
downloadURL = [[downloadURL componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString:#"%20"];
NSLog(downloadURL);
[download startDownloading:downloadURL];
try this.
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#" "];
downloadURL = [[downloadURL componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString:#"%20"];
Perhaps the %20 is being seen as a data argument, like %# or %g. Try defining the NSString using
NSString *urlString = [NSString stringWithFormat:#"http://xxxxxxxx.com/api/api.php?func=showAllwedsdwewsdsd¶ms[]=Saudi%20Arab¶ms[]=all"];
and you'll see a warning. 'Escaping' the percent sign by adding another in front of it:
NSString *urlString = [NSString stringWithFormat:#"http://xxxxxxxx.com/api/api.php?func=showAllwedsdwewsdsd¶ms[]=Saudi**%**%20Arab¶ms[]=all"];
and the warning goes away.
Your problem is this:
NSLog(downloadURL);
Try replacing it by:
NSLog(#"%#", downloadURL);
and everything will work.
You can use stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding and forget about all the workarounds.
(Explanation: since downloadURL contains % signs, it will not play well with NSLog, which expect a format string as its first argument, where % signs identify placeholders to be replaced).
I have a text with http:// in NSString. I want to get that http link from the NSString. How can i get the link/url from the string? Eg: 'Stack over flow is very useful link for the beginners https://stackoverflow.com/'. I want to get the 'https://stackoverflow.com/' from the text. How can i do this? Thanks in advance.
I am not sure what you exactly mean by link but if you want to convert your NSString to NSURL than you can do the following:
NSString *urlString = #"http://somepage.com";
NSURL *url = [NSURL URLWithString:urlString];
EDIT
This is how to get all URLs in a given NSString:
NSString *str = #"This is a grate website http://xxx.xxx/xxx you must check it out";
NSArray *arrString = [str componentsSeparatedByString:#" "];
for(int i=0; i<arrString.count;i++){
if([[arrString objectAtIndex:i] rangeOfString:#"http://"].location != NSNotFound)
NSLog(#"%#", [arrString objectAtIndex:i]);
}
Rather than splitting the string into an array and messing about that way, you can just search for the substring beginning with #"http://":
NSString *str = #"Stack over flow is very useful link for the beginners http://stackoverflow.com/";
// get the range of the substring starting with #"http://"
NSRange rng = [str rangeOfString:#"http://" options:NSCaseInsensitiveSearch];
// Set up the NSURL variable to hold the created URL
NSURL *newURL = nil;
// Make sure that we actually have found the substring
if (rng.location == NSNotFound) {
NSLog(#"URL not found");
// newURL is initialised to nil already so nothing more to do.
} else {
// Get the substring from the start of the found substring to the end.
NSString *urlString = [str substringFromIndex:rng.location];
// Turn the string into an URL and put it into the declared variable
newURL = [NSURL URLWithString:urlString];
}
try this :
nsstring *str = #"Stack over flow is very useful link for the beginners http://stackoverflow.com/";
nsstring *http = #"http";
nsarray *arrURL = [str componentsSeparatedByString:#"http"];
this will give two objects in the nsarray. 1st object will be having:Stack over flow is very useful link for the beginners and 2nd will be : ://stackoverflow.com/ (i guess)
then you can do like:
NSString *u = [arrURL lastObject];
then do like:
nsstring *http = [http stringByAppendingFormat:#"%#",u];
Quite a lengthy,but i think that would work for you. Hope that helps you.
I have an NSString containing a url and when I allocate NSURL with the NSString, NSURL outputs (null). It's because there are some illegal characters in the url, which NSURL can't read without encoding the NSString containing the url.
NSString *u = [incomingUrlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:u];
NSLog(#"INCOMINGURLSTRING: %#" , u);
NSLog(#"URL: %#" , url);
Output is:
INCOMINGURLSTRING: /url/path/fileName_blå.pdf
URL: (null)
incomingUrlString contains the Norwegian letter "å", which I think is the reason for the NSURL being (null)
I also tried this:
NSString *trimmedString = [file stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)trimmedString, NULL, (CFStringRef)#"!*'\"();:#&=+$,/?%#[]% ", kCFStringEncodingUTF8);
NSLog(#"TRIMMEDSTRING: %#" , trimmedString);
NSLog(#"ENCODEDSTRING: %#" , [encodedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]);
NSURL *url = [NSURL URLWithString:encodedString];
NSLog(#"URL: %#" , url);
Here the output is:
TRIMMEDSTRING: /url/path/fileName_blå.pdf
ENCODEDSTRING: /url/path/fileName_blå.pdf
URL: %2Furl%2FPath%2FfileName_bl%C3%A5.pdf
My goal is to load the URL into a UIWebView. It works for all the other incoming urls except for this one, they all look the same except for the filename. This is the only one containg an illegal character. But I have to find a way to encode this, because there will be more files containg either "æ", "ø" or "å" in the future.
I know the output does not look correct according to url standards, which I did on purpose. I can't show the correct url with http://blah blah because of security reasons.
Can anyone help?
The method you're using for percent-encoding the characters in the string also escapes legal URL characters. This would be appropriate if you were encoding a URL parameter, in this case though it would be better to simply use stringByAddingPercentEscapesUsingEncoding: because it leaves the characters that are part of the URL's structure (':', '/', etc.) intact:
NSString *u = #"http://example/path/fileName_blå.pdf";
u = [u stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:u];
NSLog(#"%#", url); // http://example.com/path/fileName_bl%C3%A5.pdf
If you have an URL that is a file path you must use + (id)fileURLWithPath:(NSString *)path. For the URLWithString: method the String must contain a scheme like file:// or http://.
stringByAddingPercentEscapesUsingEncoding is deprecated.
The new way (iOS 7+) to do it is:
NSString *encoded = [raw stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLPathAllowedCharacterSet];
File path is defined by https://www.rfc-editor.org/rfc/rfc8089.
The key part is to allow characters . and / and disallow %. CharacterSet.urlPathAllowed fits the requirements.
Output with your example:
incomingString: /url/path/fileName_blå.pdf
encodedString: /url/path/fileName_bl%C3%A5.pdf
URL: /url/path/fileName_bl%C3%A5.pdf
I found also that for some North European characters, NSISOLatin1StringEncoding fits better.
- (void) testEncoding {
NSString * urlString = #"http://example/path/fileName_blå.pdf";
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
NSURL * url = [NSURL URLWithString:urlString];
NSLog(#"URL: %#", url);
}