This question already has answers here:
CFURLCreateStringByAddingPercentEscapes is deprecated in iOS 9, how do I use "stringByAddingPercentEncodingWithAllowedCharacters"
(3 answers)
Closed 5 years ago.
In my code, the method CFURLCreateStringByAddingPercentEscapes is marked as deprecated as of iOS 9. My code is as follows:
static NSString *encodeByAddingPercentEscapes(NSString *input) {
NSString *encodedValue = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)input, NULL, (CFStringRef)#"!*'();:#&=+$,/?%#[]", kCFStringEncodingUTF8));
return encodedValue;
}
The warning suggests that I use [NSString stringByAddingPercentEncodingWithAllowedCharacters:]
How can I accomplish the same result using the newer method?
you can use [#"" stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]].
And there are many set to use:URLPathAllowedCharacterSet、URLQueryAllowedCharacterSet.etc
Related
This question already has answers here:
ios UTF8 encoding from nsstring
(5 answers)
Closed 6 years ago.
How to convert NSString to UTF8Encode ?
My NSLog print the same result as input string
NSString* username =#"MYUSERNAME";
username=[username stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* Password =#"MYUSERNAMEPASS";
Password=[Password stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"username=%#",username);
NSLog(#"Password=%#",Password);
You need to use stringByAddingPercentEscapesUsingEncoding instead of stringByReplacingPercentEscapesUsingEncoding.
NSString *encodeUserName = [username stringByAddingPercentEscapesUsingEncoding:NSUTF32StringEncoding];
Also stringByAddingPercentEscapesUsingEncoding is deprecated so batter to use stringByAddingPercentEncodingWithAllowedCharacters like this
NSString *encodeUserName = [username stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
This question already has answers here:
String replacement in Objective-C
(6 answers)
Closed 8 years ago.
I have a NSString:
NSString *string1 = #"http://example.com/this-is-an-example-post"
By removing a range of characteres I have substringed it to:
NSString *string2 = #"this-is-an-example-post"
But I am struggling to get it into the form of:
NSString *string3 = #"this is an example post"
Any help? Much appreciated.
For example this way
string3 = [string2 stringByReplacingOccurrencesOfString:#"-" withString:#" "];
This question already has answers here:
How to initialize NSString to NSMutableString?
(4 answers)
Closed 8 years ago.
Is there any way to convert an NSString to NSMutableString in Objective-C?
NSString *string = #"A string.";
NSMutableString *mutableString = [string mutableCopy];
This question already has answers here:
urldecode in objective-c
(6 answers)
Closed 9 years ago.
I'm trying to Decode URL,but i'm getting warning message like "NSString may not respond to URLDecode".
NSString* token = [[urlStr substringFromIndex:r.location + 1] URLDecode];
Any ideas?
Because it is not a method a classic NSString respond to, you can though add a category like the following, to add the method yourself :
#interface NSString (URLDecode)
- (NSString *)URLDecode;
#end
#implementation NSString
- (NSString *)URLDecode
{
NSString *result = [(NSString *)self stringByReplacingOccurrencesOfString:#"+" withString:#" "];
result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
return result;
}
#end
Try like this.
NSString* urlEncoded = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
and check this link https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/uid/TP40003744
it may help you.
This question already has answers here:
Is it possible to include a quotation mark as part of an nsstring?
(7 answers)
Closed 8 years ago.
i want to initialize NSString is as "hai" with double qoutes..is it possible? any help please?
but NSString *str = #"hai"; i want to convert it as "hai" without direct initialization?any help pls?
If you want to convert an existing string please use this:
NSString *newString = [NSString stringWithFormat:#"\"%#\"",str];
NSString* foo = #"\"hai\"";