Pass string with ampersand (&) from iOS to PHP/MySQL with NSURLSession
Typically for a webrequest like that it uses % followed by the ascii hex value. The hex value for & is 26, so it would be %26
google.com/#q=red%26white is example of a google search for red&white, which uses that replacement
Its called URL encoding or percent encoding and here is another question with an answer on how to do more broad URL encoding: stackoverflow.com/questions/24551816/swift-encode-url
Note that you would have to URL encode each element you were interpolating into the string, not the whole result string, as you want to keep your &'s that separate parameters.
Encode NSURL when query params contain ampersand
I found the solution, and it was with NSURLComponents
- at this point a completely undocumented class added in iOS7.
NSURLComponents *components = [NSURLComponents new];
components.scheme = @"http";
components.host = @"myurl.com";
components.path = [NSString stringWithFormat:@"%@/mypath/%@", @"/mobile_dev/api", user_id];
components.percentEncodedQuery = [NSString stringWithFormat:@"name=%@", [term urlEncodeUsingEncoding:NSUTF8StringEncoding]];
NSURL *fullURL = [components URL];
By using components.percentEncodedQuery
, the term
element uses the encoding I put on it, and apple doesn't touch it.
Hopefully this helps someone else.
Posting any text with ampersand & back to web service is causing issues
This happens when you try to pass some values in the URL
and the value contains &
which is used to separate parameters in the URL
, hence the web server spilts the content into parts wherever it encounters an &
rather than taking it as a single continuos value.
You can url encode the string to resolve the problem.
NSString *valueToSubmit = @"This is my value which contains & in it.";
NSString *urlEncodedString = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,(CFStringRef)valueToSubmit,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8));
NSLog(@"Non-Encoded: %@ \n Encoded: %@",valueToSubmit,urlEncodedString];
The output will be something like this:
Non-Encoded: This is my value which contains & in it.
URL Encoded: This%20is%20my%20value%20which%20contains%20%26%20in%20it.
Problem with RegExKitLite and ampersands
In ICU regular expression character classes, & means intersection. For example @"[[:letter:] & [a-z]]". So it needs to be quoted as Peter suggestion, with a backslash, ie \& in the regular expression. However, \ has a special meaning in C strings, including Objective C strings. So the \ has to itself be quoted with . So you need \& in your pattern. Ie, [-a-zA-Z0-9+\&@#/%?=~_()|!:,.;]
Also, I'm not sure what your intention is with the ^ at the start of the URL. If you want the regex to match anywhere in the string, you should use \b (word break) instead. If you want it to match URLs that are only at the start of the message, then you would only ever get a single match as written. If you want it to match URLs that are at the start of a line, then add (?m) at the start of the regex to turn on multiline matching for ^ (and consider adding $ to the end of the regex).
NSXMLParser problem with &(Ampersand) character
Replace
NSString *str = [[NSString alloc] initWithData:downloadedData encoding:NSASCIIStringEncoding];
//NSString *contentString = [str stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
NSData * data=[str dataUsingEncoding:NSUTF8StringEncoding];
With:
NSString *str = [[NSString alloc] initWithData:downloadedData encoding:NSASCIIStringEncoding];
NSString *contentString = [str stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
NSData * data=[contentString dataUsingEncoding:NSUTF8StringEncoding];
Related Topics
Subclass Uiapplication With Swift
How to Iterate For Loop in Reverse Order in Swift
Getting All Cookies from Wkwebview
Xcode 9 Error: "Iphone Has Denied the Launch Request"
How to Change Multiplier Property For Nslayoutconstraint
Add Image to Uialertaction in Uialertcontroller
How to Debug iOS 8 Extensions With Nslog
How to Handle Background Audio Playing While iOS Device Is Locked or on Another Application
Add "...Read More" to the End of Uilabel
Get Indexpath of Uitableviewcell on Click of Button from Cell
Swrevealviewcontroller Without Using Navigationcontroller
How to Perform Wireless Debugging in Xcode 9 With iOS 11, Apple Tv 4K, etc
Swift Convert Unix Time to Date and Time
Nsinternalinconsistencyexception', Reason: 'Could Not Load Nib in Bundle: 'Nsbundle
How to Exclude Notes and Reminders Apps from the Uiactivityviewcontroller