I have obj-C method that encodes String:
- (NSString *) encodeValue:(NSString*) unescaped{
return [unescaped stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet URLHostAllowedCharacterSet]];
}
input: testswiftapppod://
output: testswiftapppod%3A%2F%2F
I wrote the same method in Swift but got different output: testswiftapppod:%2F%2F
static func encodeValue(unescaped:String!) -> String{
return unescaped.addingPercentEncoding(
withAllowedCharacters: CharacterSet.urlHostAllowed)!
}
For some reason colon not converted
How to fix this issue?
I use Xcode 8.3
[EDIT]
From Docs:
// Returns a new string made from the receiver by replacing all
characters not in the allowedCharacters set with percent encoded
characters. UTF-8 encoding is used to determine the correct percent
encoded characters. Entire URL strings cannot be percent-encoded. This
method is intended to percent-encode an URL component or subcomponent
string, NOT the entire URL string. Any characters in allowedCharacters
outside of the 7-bit ASCII range are ignored.
- (nullable NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters NS_AVAILABLE(10_9, 7_0);
EDIT:
This is probably undocumented but intended behavior. See is `addingPercentEncoding` broken in Xcode 9 beta 2? for more details.
This is a bug.
I went over different cases, it seems all Swift code works correctly. Note that : is allowed in URL host, therefore it should not be encoded and the bug is in the Obj-C version.
NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet];
NSLog(#"Colon is member: %#", [set characterIsMember:':'] ? #"true" : #"false"); // prints true
It's an interesting bug because if you add ":" to the character set manually
NSMutableCharacterSet *set = [[NSCharacterSet URLHostAllowedCharacterSet] mutableCopy];
[set addCharactersInString:#":"];
Everything starts to work correctly.
Report it.
Note that when encoding for URL parameters, you shouldn't use urlHostAllowed. If possible, use NSURLQuery to build your URL instead. Neither of the predefined sets is actually suitable for URL encoding. You can start with urlQueryAllowed but you still have to remove some characters from it.
See for example this answer for a correct solution or for example the implementation in Alamofire library.
The desired output can be generated by:
func encodeValue(_ string: String) -> String? {
guard let unescapedString = string.addingPercentEncoding(withAllowedCharacters: CharacterSet(charactersIn: ":/").inverted) else { return nil }
return unescapedString
}
let encodedString = encodeValue("testswiftapppod://") // testswiftapppod%3A%2F%2F
Related
This question already has answers here:
Objective-C and Swift URL encoding
(13 answers)
Closed 9 years ago.
I am working on an IOS application that needs to communicate with an API (CloudStack). The API requires that each request is signed.
I need to URL encode the parameters and create an HMAC SHA1 hash. Everything works fine until I pass an parameter that contains an plus sign or an colon.
So I guess it is the URL encoding part of my application that isn't working correct. I've searched several sites and tried the provided solutions but without any results.
One of the API specifications is that all the spaces needs to be encoded as "%20" rather than "+".
The API signing guide: http://cloudstack.apache.org/docs/en-US/Apache_CloudStack/4.1.0/html/Developers_Guide/signing-api-requests.html
Currently I am using the following code to URL encode the URL:
-(NSString *)urlenc:(NSString *)val
{
NSString *result = [(NSString *)val stringByReplacingOccurrencesOfString:#"+" withString:#" "];
result = [result stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
return result;
}
I call the method like this:
[self urlenc#"2014-01-20T14:02:48+0100"]
In your case the problem probably is both the "+" and ":" character that stringByAddingPercentEscapesUsingEncoding does not encode.
You need to use an encoder that supports more characters, see this SO answer for more complete information.
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding encodes 14 characrters:
`#%^{}[]|\"<> plus the space character as percent escaped.
Here is sample code (iOS7 and above, otherwise see this SO answer):
You may need to change the characters that are encoded.
NSString *testString = #"2014-01-20T14:02:48+0100";
NSString *encodedString1 = [testString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"encodedString1: %#", encodedString1);
NSString *charactersToEscape = #"!*'();:#&=+$,/?%#[]\" ";
NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
NSString *encodedString2 = [testString stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
NSLog(#"encodedString2: %#", encodedString2);
NSLog output:
encodedString1: 2014-01-20T14:02:48+0100
encodedString2: 2014-01-20T14%3A02%3A48%2B0100
I'm very new to objective-c and I'm getting a basic error and unsure on how to fix it.
for(ZBarSymbol *sym in syms) {
resultText.text = sym.data; //sym.data = 0012044012482
[self phpPost:(int)sym.data];
break;
}
}
- (void)phpPost: (int)barcode {
NSString *theValue = [NSString stringWithFormat:#"%#", barcode]; //problem line
labelScan.text = theValue;
//labelScan.text = #"Barcode scanned";
}
when i use #"%#" the text of the label is correct (0012044012482), but when i use #"%d" it isn't (random number every time i restart the app). I would like to use #"%#" (or something that works) but for some reason xCode is giving me an error. and I'm unsure on how to fix the error.
The error is:
Format specifies type 'id' but the argument has type 'int'
In the end I plan on having that code (sym.data) written to a MySQL database using the POST method.
You can't just convert it to an int by casting if it's an object (which it must be if the %# format specifier isn't causing a crash).
Assuming from the fact that you're assinging it directly to a label's text that it's an NSString, you should either change the parameter type of phpPost:
- (void)phpPost: (NSString *)barcode {
labelScan.text = barcode;
}
or extract the intValue before passing sym.data:
[self phpPost:[sym.data intValue]];
and then use the proper %d format specifier in phpPost:.
Your barcode isn't an int, it is an NSString. Instead of doing (int)sym.data, pass in [sym.data intValue]. That should correctly convert it to an integer.
The reason you get a random number is because you can't just cast a string object to a primitive data type :)
I don't know what type sym.data is, but it is likely a pointer to an object, and not the value itself. You cast that pointer to int, so when you are using %d you are effectively printing the memory location of the object. That is why it changes each time you run the program (Objective-C let's you do this without any warnings - something to watch out for).
To fix this, either extract the integer value you need from the sym.data object using it's properties; or pass the object as a pointer. For instance, you could try calling your method like this:
[self phpPost:sym.data];
And then change your method to be:
- (void)phpPost: (id)barcode {
NSString *theValue = [NSString stringWithFormat:#"%#", barcode];
labelScan.text = theValue;
}
Ok, I did some thinking while I was at work today, and I figured out that an INT isn't going to work for me. if I make that object to an int, I would loss some data that is vital to what I'm doing. eg. object=001234 int=1234. I need the zeros. So, in the end, I'm keeping it an object (string) and just passing it into the function.
Here is my code after I got it working correctly.
for(ZBarSymbol *sym in syms) {
resultText.text = sym.data;
[self phpPost:sym.data];
break;
}
}
- (void)phpPost: (NSString *)barcode {
labelScan.text = barcode;
//labelScan.text = #"Barcode scanned"; //My custon label
}
Thanks, everyone for your responses. Your answer will not go unused. I'm sure I'll be needing this information here soon.
O, if you see that I did this wrong, or not the correct way, please make a comment and tell me .
The stringByReplacingPercentEscapesUsingEncoding method is not working properly as it's not decoding special symbols that dont start with a % character, i.e., the + character. Does anyone know of a better method to do this in iOS?
Here's what I'm currently using:
NSString *path = [#"path+with+spaces"
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
And here's an example of the output:
path+with+spaces
NSString *path = [[#"path+with+spaces"
stringByReplacingOccurrencesOfString:#"+" withString:#" "]
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Note that the plus-for-space substitution is only used in application/x-www-form-urlencoded data - the query string part of a URL, or the body of a POST request.
// Decode a percent escape encoded string.
- (NSString*) decodeFromPercentEscapeString:(NSString *) string {
return (__bridge NSString *) CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
(__bridge CFStringRef) string,
CFSTR(""),
kCFStringEncodingUTF8);
}
http://cybersam.com/ios-dev/proper-url-percent-encoding-in-ios
This seems to be the preferred way because...
"Apparently" this is a "bug" apple is aware of, but they haven't done anything about it yet... ( http://simonwoodside.com/weblog/2009/4/22/how_to_really_url_encode/ )
If you are trying to replace the plus sign with percent escapes, perform a string replacement from "+" to " " (single space). Then use stringByAddingPercentEscapesUsingEncoding: to add the percent escapes.
The plus sign is one of many reserved URL characters that is never encoded.
(stringByReplacingPercentEscapesUsingEncoding: decodes the percent escapes)
swift 2 :
extension String {
func uriDecodedString() -> String? {
return self.stringByReplacingOccurrencesOfString("+", withString: " ").stringByRemovingPercentEncoding
}
}
Also you can use the PercentEncoder library from Cocoapods.
Swift 4
Include the library to your Podfile:
pod PercentEncoder
Import the library PercentEncoder
import PercentEncoder
class ViewController{
...
}
Replace the "+" character by "%20" and use the method "ped_decodeURI"
"path+with+spaces".replacingOccurrences(of: "+", with: "%20").ped_decodeURI()
It will return "path with spaces"
Here the link for reference: https://cocoapods.org/pods/PercentEncoder
I'm looking for a way in the iPhone SDK to read in a Properties file (not the XML flavor) for example this one:
# a comment
! a comment
a = a string
b = a string with escape sequences \t \n \r \\ \" \' \ (space) \u0123
c = a string with a continuation line \
continuation line
d.e.f = another string
would result in four key/value pairs.
I can't change this format as it is sent to me by a web service. Can you please direct me?
Thanks,
Emmanuel
I would take a look at ParseKit http://parsekit.com/. Otherwise you could use RegexKitLite and create some regular expressions.
I've ended with this solution if anybody is interested :
#interface NSDictionary (PropertiesFile)
+ (NSDictionary *)dictionaryWithPropertiesFile:(NSString *)file;
#end
#implementation NSDictionary (PropertiesFile)
+ (NSDictionary *)dictionaryWithPropertiesFile:(NSString *)file {
NSError *error = nil;
NSString *propertyFileContent = [[NSString alloc] initWithContentsOfFile:file encoding:NSUTF8StringEncoding error:&error];
if (error) return nil;
NSArray *properties = [propertyFileContent componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
if (properties.count == 0) return nil;
NSMutableDictionary *result = [[NSMutableDictionary alloc] initWithCapacity:properties.count];
for (NSString *propertySting in properties) {
NSArray *property = [propertySting componentsSeparatedByString:#"="];
if (property.count != 2) continue;
[result setObject:property[1] forKey:property[0]];
}
return result.allKeys.count > 0 ? result : nil;
}
#end
It's a perfectly simple parsing problem. Read a line. Ignore if comment. Check for continuation, and read/append continuation lines as needed. Look for the "=". Make the left side of the "=" (after trimming white space) the key. Either parse the right side yourself or put it into an NSString and use stringWithFormat on it to "reduce" any escapes to pure character form. Return key and reduced right side.
(But refreshing my memory on the properties file format reminds me that:
The key contains all of the characters in the line starting with the
first non-white space character and up to, but not including, the
first unescaped '=', ':', or white space character other than a line
terminator. All of these key termination characters may be included in
the key by escaping them with a preceding backslash character;
So a little scanning of the line is required to separate the key from the rest. Nothing particularly difficult, though.)
Have you considered using lex/yacc or flex/bison to generate your own compiler code from a description of the grammar for properties files? I'm not sure if there are any existing grammars defined for a Java properties file, but it seems like it would be a pretty simple grammar to write.
Here's another SO post that mentions this approach for general purpose parsing
Take a look at this PropertyParser
NSString *text = #"sample key = sample value";
PropertyParser *propertyParser = [[PropertyParser alloc] init];
NSMutableDictionary *keyValueMap = [propertyParser parse:text];
You can now use NSRegularExpression class to do this.
I have tried using a variable as an input parameter to NSLocalizedString, but all I am getting back is the input parameter. What am I doing wrong? Is it possible to use a variable string value as an index for NSLocalized string?
For example, I have some strings that I want localized versions to be displayed. However, I would like to use a variable as a parameter to NSLocalizedString, instead of a constant string. Likewise, I would like to include formatting elements in the parameter for NSLocalizedString, so I would be able to retrieved a localized version of the string with the same formatting parameters. Can I do the following:
Case 1: Variable NSLocalizedstring:
NSString *varStr = #"Index1";
NSString *string1 = NSLocalizedString(varStr,#"");
Case 2: Formatted NSLocalizedString:
NSString *string1 = [NSString stringWithFormat:NSLocalizedString(#"This is an %#",#""),#"Apple"];
(Please note that the variable can contain anything, not just a fixed set of strings.)
Thanks!
If what you want is to return the localized version of "This is an Apple/Orange/whatever", you'd want:
NSString *localizedVersion = NSLocalizedString(([NSString stringWithFormat:#"This is an %#", #"Apple"]), nil);
(I.e., the nesting of NSLocalizedString() and [NSString stringWithFormat:] are reversed.)
If what you want is the format to be localized, but not the substituted-in value, do this:
NSString *finalString = [NSString stringWithFormat:NSLocalizedString(#"SomeFormat", nil), #"Apple"];
And in your Localizable.strings:
SomeFormat = "This is an %#";
I just want to add one very helpful definition which I use in many of my projects.
Inspired by androids possibility, I've added this function to my header prefix file:
#define NSLocalizedFormatString(fmt, ...) [NSString stringWithFormat:NSLocalizedString(fmt, nil), __VA_ARGS__]
This allows you to define a localized string like the following:
"ExampleScreenAuthorizationDescriptionLbl"= "I authorize the payment of %# to %#.";
and it can be used via:
self.labelAuthorizationText.text = NSLocalizedFormatString(#"ExampleScreenAuthorizationDescriptionLbl", self.formattedAmount, self.companyQualifier);
For swift :
let myString = String(format: NSLocalizedString("I authorize the payment of %d ", comment: ""), amount)
extension String {
public var localizedString: String {
return NSLocalizedString(self, comment: "")
}
public func localizedString(with arguments: [CVarArg]) -> String {
return String(format: localizedString, arguments: arguments)
}
}
Localizable.string:
"Alarm:Popup:DismissOperation:DeviceMessage" = "\"%#\" will send position updates on a regular basis again.";
"Global:Text:Ok" = "OK";
Usage:
let message = "Alarm:Popup:DismissOperation:DeviceMessage".localizedString(with: [name])
and
let title = "Global:Text:Ok".localizedString
It turns out that a missing target entry is to blame. Just checking that my current build target includes the Localizable.string file solved the problem!
If you have more than one variable in your localized string can you use this solution:
In Localizable.strings
"winpopup" = "#name# wins a #type# and get #points# points(s)";
And use stringByReplacingOccurrencesOfString to insert the values
NSString *string = NSLocalizedString(#"winpopup", nil); //"#name# wins a #type# and get #points# points(s)"
NSString *foo = [string stringByReplacingOccurrencesOfString:#"#name#" withString:gameLayer.turn];
NSString *fooo = [foo stringByReplacingOccurrencesOfString:#"#type#" withString:winMode];
NSString *msg = [fooo stringByReplacingOccurrencesOfString:#"#points#" withString:[NSString stringWithFormat:#"%i", pkt]];
NSLog(#"%#", msg);
Your ideas should work. But if you are getting back the input parameter, that means that the input parameter was not found as a key in your Localizable.strings file. Check the syntax and location of that file.
This works for me:
NSMutableString *testMessage = [NSMutableString stringWithString:NSLocalizedString(#"Some localized text", #"")];
testMessage = [NSMutableString stringWithString:[testMessage stringByAppendingString:someStringVariable]];