Sunday, May 19, 2013

NSString stringByAddingPercentEscapesUsingEncoding example ios


[NSString stringByAddingPercentEscapesUsingEncoding]

Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.
- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
Parameters
encoding
The encoding to use for the returned string. If you are uncertain of the correct encoding you should use NSUTF8StringEncoding.
Return Value of [NSString stringByAddingPercentEscapesUsingEncoding]
A representation of the receiver using encoding to determine the percent escapes necessary to convert the receiver into a legal URL string. Returns nil if encodingcannot encode a particular character.
Discussion of [NSString stringByAddingPercentEscapesUsingEncoding]
It may be difficult to use this function to "clean up" unescaped or partially escaped URL strings where sequences are unpredictable. SeeCFURLCreateStringByAddingPercentEscapes for more information.
Example of [NSString stringByAddingPercentEscapesUsingEncoding]
//localisationName is a arbitrary string here
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString* stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false", webName];
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];
Example of [NSString stringByAddingPercentEscapesUsingEncoding]
NSString *urlString =
    [NSString stringWithFormat:@"%@/iphone/push/create?pn[token]=%@&pn[send_at]=%@",
     kHTTPURL, appDelegate.deviceAPNToken, [dateTimeToUse description]];

urlString =
    [[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
     stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
Example of [NSString stringByAddingPercentEscapesUsingEncoding]
NSString *appname = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  //Or your String Encoding


NSString *result = [rwanswer stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  //Or your String Encoding;
NSLog(@"result=%@", result);

NSString *comment =[comments.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  //Or your String Encoding;
NSString *name = [username.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];  //Or your String Encoding;