2016-07-23 105 views
0

這可能是一個重複的問題,但我已經檢查了所有,無法找到適用於iOS9的工作答案。 -stringByAddingPercentEscapesUsingEncoding已被棄用。我需要使用-stringByAddingPercentEncodingWithAllowedCharactersNSURL:使用NSCharacterSet逃脫反斜槓

以下是需要反斜槓的字符串,以便API可以驗證會話並返回響應。

NSString *base = @"http://domain.com/interface/?end=imember"; 
NSCharacterSet *set = [NSCharacterSet URLQueryAllowedCharacterSet]; 
NSString *key = [@"&client_key=KOB3N6KX9JXF2MRPO5U.BRFYM7TYVE\/16KIJVXZA6R7H\/1LD1K\/JYIYG7IZP2HA7NUYOVNT3CJG==&token=SGD7E9B29TQ.8HIITZ37XW3GLK5OGLZNLCDM=" stringByAddingPercentEncodingWithAllowedCharacters:set]; 

標準的URL字符集不逃避反斜槓,我把它們都試過:

URLUserAllowedCharacterSet 
URLPasswordAllowedCharacterSet 
URLHostAllowedCharacterSet 
URLPathAllowedCharacterSet 
URLQueryAllowedCharacterSet 
URLFragmentAllowedCharacterSet 

請,如果有人可以幫助,我是相當新的發展。是否可以創建一個包含反斜槓的自定義允許集?

編輯:

這是URL應該是什麼樣子:

http://domain.com/interface/?end=imember&client_key=KOB3N6KX9JXF2MRPO5U.BRFYM7TYVE\/16KIJVXZA6R7H\/1LD1K\/JYIYG7IZP2HA7NUYOVNT3CJG==&token=SGD7E9B29TQ.8HIITZ37XW3GLK5OGLZNLCDM= 

回答

1

確切的解決方案,你的回答是below.I得到它從Zaph's answer。那是比其他答案更好的答案。

NSString *unescaped = @"http://domain.com/interface/?end=imember"]; 
NSString *escapedString = [unescaped stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; 
NSLog(@"escapedString: %@", escapedString); 

URL編碼字符集

URLFragmentAllowedCharacterSet "#%<>[\]^`{|} 
URLHostAllowedCharacterSet  "#%/<>[email protected]\^`{|} 
URLPasswordAllowedCharacterSet "#%/:<>[email protected][\]^`{|} 
URLPathAllowedCharacterSet  "#%;<>?[\]^`{|} 
URLQueryAllowedCharacterSet  "#%<>[\]^`{|} 
URLUserAllowedCharacterSet  "#%/:<>[email protected][\]^` 
+0

當我使用URLHostAllowedCharacterSet這就是結果: – Alexander

+0

'&client_key = KOB3N6KX9JXF2MRPO5U.BRFYYGM7TYVE%2F16KIJVXZHUA6R7H%2F1LDD1K%2FJYIY22G7IZP2HA7NUYOVJWEHNT3CJG ==令牌= SGD7XXL ... LZNLCDM = '它已經用百分號代替反斜槓 – Alexander

+0

謝謝它的作品,這是一個虛假的錯誤,當連接... – Alexander