Hi I have to send data to server via JSON and usually I do it like that:
NSMutableString * temp=[[NSMutableString alloc] initWithString:service_registra_inc];
//here I add more staff to temp
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:temp]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
but I get some error message that the url is wrong: something like url length but I have searched around and it means I have to escape my url si I have found this fonction that doesn't work for me:
NSString *temp2=(__bridge_transfer NSString*)CFURLCreateStringByAddingPercentEscapes(NULL,((CFStringRef)temp.UTF8String),NULL,NULL,kCFStringEncodingUTF8);
and there the program just stops and it says signal EXC_BAD_ACCess.
Well I don't really know how to transform mutable strings into CFStringRef so xcode just suggested the corrections for me but I don't really understand what is happening. Please help.... I have read the doc but it doesn't say how to cast NSSMutableString to CFStringRef and back or how to use the whole thing to create an NSURL object directly. Thks
Why are you using CFURL & CFStringRef functions here?
You could do what you are trying to do via NSString's stringByAddingPercentEscapesUsingEncoding: method. I've linked the documentation for you.
Something like:
NSMutableString * temp=[[NSMutableString alloc] initWithString:service_registra_inc];
// append your staff... errr, stuff here.
NSString * temp2 = [temp stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:temp2]];
[NSURLConnection alloc] initWithRequest:request delegate:self];
(don't forget to release things if you're not using ARC)
An NSString * is also a CFStringRef through a mechanism known as toll-free bridging and an NSMutableString * is also an NSString * through inheritance. So your second line of code should be:
NSString *temp2 = (__bridge_transfer NSString *)
CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)temp,
NULL,
NULL,
kCFStringEncodingUTF8);
Though in practice you might prefer:
NSString *temp2 =
[temp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Which has the secondary advantage of returning an object with a non-owning reference, so you don't need to worry about releasing it even if you're not using ARC.
Related
I have a simple POST coming from my iphone app. Its working fine, except passing an ampersand causes the backend to break - it's almost like its treating it like a GET request (ampersands seperate the variable names). Do I need to do some kind of encoding first? Here is the code:
NSString *content = [[NSString alloc] initWithFormat:#"data=%#&email=%#", str, emailAddress.text];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"http://www.myurl.com/myscript.php"]];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[content dataUsingEncoding:NSISOLatin1StringEncoding]];
// generates an autoreleased NSURLConnection
[NSURLConnection connectionWithRequest:request delegate:self];
I had this issue in iOS7 and the accepted answer didn't work at all (actually, that is my standard when sending data to the backend). The ampersand was breaking in the backend side, so I had to replace the & by %26. The backend was being done in python and the code was legacy and was using ASI.
Essentially I have done the following:
NSString *dataContent = [NSString stringWithFormat:#"text=%#",
[json stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
dataContent = [dataContent stringByReplacingOccurrencesOfString:#"&"
withString:#"%26"];
ByAddingPercent....... will not work as & is a valid URL character.
I needed to send a JSON with & in it, it is the same idea though;
NSString *post = [NSString stringWithFormat:#"JSON=%#", (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)jsonString, NULL, CFSTR(":/?#[]#!$&’()*+,;="), kCFStringEncodingUTF8))];
"jsonString" towards the end is what is converted.
Edit: As stringByAddingPercentEscapesUsingEncoding: should be used to encode parts of the query, not the whole one, you should be using another method instead.
Unfortunately, Foundation doesn't provide such a method, so you need to reach to CoreFoundation:
- (NSString *)stringByURLEncodingString:(NSString *)string {
return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(
kCFAllocatorDefault,
(__bridge CFStringRef)string,
NULL, // or (__bridge CFStringRef)(#"[].")
(__bridge CFStringRef)(#":/?&=;+!##$()',*"),
kCFStringEncodingUTF8
);
}
You can use
- stringByAddingPercentEscapesUsingEncoding:
In your case it will look like this:
NSString * content = [[NSString alloc] initWithFormat:#"data=%#&email=%#", [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [emailAddress.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
You can do this in this way, too:
NSString *dataStr = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *emailStr = [emailAddress.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *content = [[NSString alloc] initWithFormat:#"data=%#&email=%#", dataStr, emailStr];
I'm not sure if this will work in your case, but you could try %38 to try and encode the ampersand.
I want to create a url as below
http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA&sensor=false
I used the following code to create this
NSURL *jsonURL;
NSString *strurl = [[NSString alloc]initWithFormat:#"http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA&sensor=false"];
jsonURL = [NSURL URLWithString:strurl];
[strurl release];
NSLog(#"json Url%#",jsonURL);
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
if(jsonData == nil){
//NSLog(#"Data NIL .....");
}
else{
SBJSON *json = [[SBJSON alloc] init];
NSError *error = nil;
dic = [json objectWithString:jsonData error:&error];
[json release];
}
But every time I get jsonURL to be nil .
I think the problem is due to "|". Has someone come across same issue? If yes, can you help me out?
Try
[NSURL URLWithString:[strurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
The documentation for URLWithString says:
The string with which to initialize the NSURL object. Must conform to RFC 2396.
... which e.g. mentions:
Other characters are excluded because
gateways and other transport agents
are known to sometimes modify such
characters, or they are used as
delimiters.
unwise = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"
Data corresponding to excluded
characters must be escaped in order to
be properly represented within a URI.
Thus escape them properly as slf suggested.
Also, just use a string constant for predefined strings:
NSString *strurl = #"http://....";
As for your URL issue, Georg is right:
NSURL *jsonURL = [NSURL URLWithString:#"http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize%3Atrue%7CBarossa+Valley,SA%7CClare,SA%7CConnawarra,SA%7CMcLaren+Vale,SA&sensor=false"];
Fixed that issue for me.
However, the next bit:
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];
Is deeply troubling. You should never do synchronous data reads on the main thread. initWithContentsOfURL is going to spawn a synchronous NSURLConnection to go fetch that data and might return sometime before sunday, but you never know. (This method is ok for filesystem loads, where things are much more deterministic)
Look into an asynchronous loading API like NSURLConnection from Apple, or better yet ASIHTTPRequest, about which there is ample documentation online.
Happy webservicing!
I think, the root of cause is your string creating method.
NSString *strurl = [[NSString alloc]initWithFormat:#"http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA&sensor=false"];
Try with ...
NSString *strurl = [[NSString alloc]initWithString:#"http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA&sensor=false"];
I am trying to send a query as part a the URL to obtain an XML file, and then trying to parse the XML file using NSXMLParser and initWithContentsOfURL. However the parser is not able to parse the file. I tested the parser with the same file, but this time the file was saved on the server (it was not being generated) and it worked just fine, so I know it is not a problem with the parser.
I have come to think that it does not parse it because I need to load the file before I try to parse it, or give the initWithContentsOfURL time to load the contents. So I tried to put those contents in a NSString and a NSData and using a sleep function as well as using a block but that did not work either.
What would be the best way to go about this problem?
Here is some of the code:
NSString *surl = [NSString stringWithFormat:#"http://lxsrv7.oru.edu/~maria_hernandez/query.xml"];
url = [NSURL URLWithString:surl];
NSString *curl = [[NSString alloc] initWithContentsOfURL:url];
NSLog(#"URL: %#", surl);
NSLog(#"URL Content: %#", curl);
SXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:receivedData];
//Other stuff we have tried:
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:surl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLResponse = nil;
NSError = nil;
receivedData = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &theResponse error: &error];
Let me know if you have more questions or if you wish to see more code.
Thanks!
have you tried setting a delegate for the NSXMLParse that implements the NSXMLParserDelegate which has events for parsing the document
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html
We are pulling content off our website using XML/NSMutableURLRequest and sometimes it pulls through the "curly" style apostrophe and quotes, ’ rather than '. NSMutableURLRequest seems to hate these and turns them into the strange \U00e2\U0080\U0099 string.
Is there something that I can to do prevent this? I am using the GET method, so should I be somehow telling it to use UTF-8? Or, am I missing something?
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
NSString *urlStr = [NSString stringWithFormat:#"%#",url];
NSURL *serviceUrl = [NSURL URLWithString:urlStr];
NSMutableURLRequest *serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl];
[serviceRequest setHTTPMethod:#"GET"];
NSURLResponse *serviceResponse;
NSError *serviceError;
app.networkActivityIndicatorVisible = NO;
return [NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&serviceResponse error:&serviceError];
NSURLConnection returns an NSData response. You can take that NSData response and turn it into a string. Then take this string, turn it back into a NSData object, properly UTF-8 encoding it along the way, and feed it to NSXMLParser.
Example: (Assuming response is the NSData response from your request)
// long variable names for descriptive purposes
NSString* xmlDataAsAString = [[[NSString alloc] initWithData:response] autorelease];
NSData* toFeedToXMLParser = [xmDataAsAString dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser* parser = [[[NSXMLParser alloc] initWithData:toFeedToXMLParser] autorelease];
// now utilize parser...
I would suggest replacing those characters using stringByReplacingCharactersInRange:withString: to replace the unwanted strings.
NSString *currentTitle = #"Some string with a bunch of stuff in it.";
//Create a new range for each character.
NSRange rangeOfDash = [currentTitle rangeOfString:#"character to replace"];
NSString *location = (rangeOfDash.location != NSNotFound) ? [currentTitle substringToIndex:rangeOfDash.location] : nil;
if(location){
currentTitle = [[currentTitle stringByReplacingOccurrencesOfString:location withString:#""] mutableCopy];
}
I've done this in the past to handle the same problem you describe.
Try using the stringByReplacingPercentEscapesUsingEncoding:
I'm adding a Basic HTTP Authorisation Header into a request but need to encode the authString to Base64. For info, I can't use didReceiveAuthenticationChallenge due to excessive 401 errors being produced.
The code below works fine in iOS 4.2 but doesn't work on iOS 3.2 (and I want to support this).
NSString *authString = [[[NSString stringWithFormat:#"%#:%#", user, password] dataUsingEncoding:NSUTF8StringEncoding] base64Encoding];
authString = [NSString stringWithFormat: #"Basic %#", authString];
NSMutableURLRequest* request =
[[NSMutableURLRequest alloc] initWithURL: url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval: 30];
[request setValue: authString forHTTPHeaderField: #"Authorization"];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
In the first line of my code above I get the warning that NSData will not respond to 'base64Encoding'.
So I've downloaded the custom class NSData+Base64 from here:
http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
But......I don't know how to use this class to convert my NSString (authString). Please help?!
I think the following line of code should fix:
NSString *authString = [[[NSString stringWithFormat:#"%#:%#", user, password] dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];
but I get following message:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableData base64EncodedString]: unrecognized selector sent to instance
Have I missed an Import or something?
P.S. This is my first question on here, so go easy on me!!
NSString has a -dataUsingEncoding: method which you can use to convert NSString instances to NSData instances. After that, you can use MG's Base64 category.
Have you created the category to use the BASE64Encoding method in your NSData class?? here i made a tutorial to easy create categories, which basically are modifications of an existing class, in this case NSData: http://www.donttouchmycode.com/objective-c-class/extending-an-existing-class-with-categories i hope this can help you.