Really quick question that is driving me INSANE. I was wondering if someone could tell me why this line is leaking?
NSString *post = [NSString stringWithFormat:#"<someXML><tagWithVar=%#></tagWithVar></someXML>",var];
post = [NSString stringWithFormat:#"xmlValue=%#",(NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)post,
NULL,
(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8 )];
I am just encoding a string into a URL format. From my understanding, stringWithFormat: should return an autoreleased object. Apparently that is not the case. It works, but leaks. Any ideas??
You are using the method CFURLCreateStringByAddingPercentEscapes. If a Core Foundation function has "Create" in its name, it means that you own the returned object. In other words, you'll need to release the CFStringRef returned by CFURLCreateStringByAddingPercentEscapes.
NSString *post = [NSString stringWithFormat:#"...", var];
CFStringRef stringRef = CFURLCreateStringByAddingPercentEscapes(...);
post = [NSString stringWithFormat:#"xmlValue=%#",(NSString *)stringRef];
CFRelease(stringRef);
Related
I'm doing this to encode my URL in this way,
but its not working,
i got the result in NSLog but its the same url nothing is changing.
Please help me to sort this issue.
below is my code :
NSString *unencodedUrlString =
[#"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2,7"
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#" %#", unencodedUrlString);
Thanks in advance
The comma is a legal URL character, therefore stringByAddingPercentEscapesUsingEncoding leaves "2,7" as it is and does not replace it by "2%2C7".
If you want the comma to be replaced by a percent escape (as I understand from your
comment to the question), you can use CFURLCreateStringByAddingPercentEscapes
instead:
NSString *str = #"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2,7";
NSString *encoded = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(__bridge CFStringRef)(str), NULL, CFSTR(","), kCFStringEncodingUTF8));
NSLog(#"%#", encoded);
Output:
http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2%2C7
The fourth parameter CFSTR(",") specifies that the comma should be replaced by
a percent escape even if it is a legal URL character.
Use this
NSString *str = [NSString stringWithFormat:#"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2,7"];
NSString *path = [str stringByReplacingOccurrencesOfString:#"," withString:#"/"];
NSLog(#"%#",path);
This will do nothing but will make , to /.
I have this:
partenaire_lat = 48.8160525;
partenaire_lng = 2.3257800;
And obtain a NSString like this:
NSString *endPoint =[NSString stringWithFormat:#"%#,%#", partenaire_lat, partenaire_lng];
and after using this NSString in some context I get this stupid error:
Variable is not a CFString.
But if I create the NSString like this:
endPoint = #"48.8160525,2.3257800" it then works perfect!
For this error Variable is not a CFString I tried the following:
NSString *endPoint1 =[NSString stringWithFormat:#"%#,%#", partenaire_lat, partenaire_lng];
CFStringRef endPoint =(CFStringRef)endPoint1;
and tried to use endPoint but not working neither this way.Anyone any miraculous idea?Thx
EDIT:partenaire_lat and partenaire_lng are both NSString!!
You have
partenaire_lat = 48.8160525;
partenaire_lng = 2.3257800;
You keep saying that the two variables are NSStrings but you aren't assigning NSStrings to them. You need to assign NSString objects to NSString variables - they aren't created for you automatically.
So the answers which are telling you to use formatted strings are correct. You really should be doing it like this:
partenaire_lat = [[NSString stringWithFormat:#"%f", 48.8160525] retain];
partenaire_lng = [[NSString stringWithFormat:#"%f", 2.3257800] retain];
what are lat and lng? i'm assuming float or double..so you should use [NSString stringWithFormat:#"%f,%f", lat, lng]; (or however you want the floats to be formatted)
You code has several potential problems:
%# format specifier expects object parameter, while it looks like you pass plain float (I may be wrong here as there's not enough context to be sure). Change format to %f to fix your problem if that's really the case:
NSString *endPoint1 =[NSString stringWithFormat:#"%f,%f", partenaire_lat, partenaire_lng];
Your endPoint1 string is autoreleased and may become invalid outside of current scope if you don't retain it. So if you try to use your variable in another method you probably should retain it.
All you need to do
NSString *latStr=[[NSNumber numberWithFloat:partenaire_lat] stringValue];
NSString *lngStr=[[NSNumber numberWithFloat:partenaire_lng] stringValue];
and do whatever you want to do with these two string :)
NSString* urlEncode(NSString * url)
{
string inStr = StringFromNSString(url);
CFStringRef inStringRef = CFStringCreateWithCString( kCFAllocatorDefault, inStr.c_str(), kCFStringEncodingUTF8 );
NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)inStringRef,NULL,(CFStringRef)#"!*’();:#&=+$,/?%#[]",kCFStringEncodingUTF8 );
return encodedString;
}
I am using above method to encode url... even though my app is crashing saying
<body>
<div id="content">
<h1>An Error Was Encountered</h1>
<p>The URI you submitted has disallowed characters.</p> </div>
</body>
</html>
terminate called after throwing an instance of 'std::invalid_argument'
what():
Any idea.. What is wrong with my code?
FYI: It is crashing in this method JSONNode jsonObject0 = libJSON::parse( inResponseData );
UPDATED: The server which i am sending message is UNIX server is it causing problem?
You don't need to create the inStr or inStringRef temporary variables. The types NSString* and CFStringRef are "toll free bridge" types. These types are interchangable with just a simple cast.
You can find more information about toll free bridging here: http://www.mikeash.com/pyblog/friday-qa-2010-01-22-toll-free-bridging-internals.html
That said, you can simplify your method to just the following:
-(NSString*) urlEncode
{
NSString *encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes( NULL, (CFStringRef)self, NULL, (CFStringRef)#"!*'();:#&=+$,/?%#[]", kCFStringEncodingUTF8 );
return [encodedString autorelease];
}
The above is best implemented as an NSString category (note the use of self as the value to encode).
This works fine for me. I use CFSTR instead of (CFStringRef)#"!*’();:#&=+$,/?%#[]"
- (NSString *)encodedURLParameterString {
NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)self,
NULL,
CFSTR(":/=,!$& '()*+;[]##?"),
kCFStringEncodingUTF8);
return [result autorelease];
}
You can try this
NSString *sampleUrl = #"http://www.google.com/search.jsp?params=Java Developer";
NSString* encodedUrl = [sampleUrl stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding];
You need to make sure that you're not URL encoding more than you need. If you're going to be using this encoding string as part of an actual URL, then you should know that there are only portions of the URL that are supposed to be encoded, namely the query string (there are other bits as well, but 95% of the time, this has to do with the query).
In other words, your URL should be:
scheme://host/path?<key>=<value>&<key>=<value>
In this, ONLY the stuff inside the angle brackets (<key> and <value>) should be URL encoded.
It was the problem in UNIX server... It was giving wrong data.
NSString *encodedstring = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFStringRef)yoururlstring,
NULL,
(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8);
this code works perfect
Here is the best way to encode the formatted URLString:
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
Instead of using below as it has some memory management issues.
NSString *encodedstring = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFStringRef)yoururlstring,
NULL,
(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8);
Have a slight problem. Trying to post XML to a server. To do this, I have to encode the XML string in URL format.
NSString *encodedString = [NSString stringWithFormat:#"xmlValue=%#",[post stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]];
That's what I have and it works for all the characters EXCEPT the '='. This does not get converted to %3D. Has anyone else ever had this problem before? Am I specifying the wrong encoding type? I have tried "NSUTF8StringEncoding" as well.
This is a little piece of the XML string:
#"<xml-service application=\"broadcast\" type=\"REQUEST\"><identity token=\"xxxxxxxx\".....
Any help would be appreciated! Thanks
This should solve your problem:
NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8 );
Source
You can try this. I found this helpful.
NSString *sampleUrl = #"http://www.google.com/search.jsp?params=Java Developer";
NSString* encodedUrl = [sampleUrl stringByAddingPercentEscapesUsingEncoding:
NSASCIIStringEncoding];
The following code works, but it's ugly and creates a bunch of autoreleased objects. I'm using similar code for parsing reserved HTML characters as well (for quotes, & symbols, etc). I'm just wondering... Is there a cleaner way?
NSString *result = [[NSString alloc] initWithString:userInput];
NSString *result2 = [result stringByReplacingOccurrencesOfString:#"#"
withString:#"\%23"];
NSString *result3 = [result2 stringByReplacingOccurrencesOfString:#" "
withString:#"\%20"];
formatted = [[result3 stringByReplacingOccurrencesOfString:#"&"
withString:#"\%26"] retain];
[result release];
#Yannick, you were on the right track, thank you. stringByAddingPercentEscapesUsingEncoding sort of works but ignores certain characters that can still be a problem (like slashes). Here is the best way to do URL encoding:
NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)#"!*'\"();:#&=+$,/?%#[]% ",
kCFStringEncodingUTF8 );
Have you tried to use the stringByAddingPercentEscapesUsingEncoding method?
formatted = [result stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];