简体   繁体   English

ios中的url编码

[英]url Encoding in ios

I get a link from server to image has already been Encoding so it comes with %20 But sometimes I get a code include Hebrew characters, so I need to do Encoding agian , With the above code: 我从服务器到图像的链接已经被Encoding编码了,所以它附带了%20,但是有时我得到一个包含希伯来字符的代码,因此我需要使用上述代码对agian进行编码:

But after my Encoding i get in response that % 20 change to % 2520 但是在我编码之后,我得到了回应,将%20更改为%2520

static CFStringRef charsToEscape = CFSTR("&=");

    + (NSString *)escapeStringByAddingPercentEscapes: (NSString*) string {

        return [(NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                    (CFStringRef)string,
                                                                    NULL,
                                                                    charsToEscape,
                                                                    CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)) autorelease];
    }

i tried to add % to charsToEscape like this: 我试图将%添加到charsToEscape中,如下所示:

static CFStringRef charsToEscape = CFSTR("&=%"); 静态CFStringRef charsToEscape = CFSTR(“&=%”);

but it did not help. 但这没有帮助。

thanks 谢谢

NSString *urlString = @"----YOUR URL HERE----";
[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString];

Your idea is good if I did not get the Hebrew characters in the link. 如果我没有在链接中获得希伯来语字符,则您的想法很好。

But I found the solution , I just sent the % in the parameter legalURLCharactersToBeEscaped 但是我找到了解决方案,我只是在参数LegalURLCharactersToBeEscaped中发送了%

now i use in the function like this: 现在我在这样的功能中使用:

static CFStringRef charsToEscape = CFSTR("&=");
static CFStringRef charsUnchanged = CFSTR("%");

    + (NSString *)escapeStringByAddingPercentEscapes: (NSString*) string {

        return [(NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                    (CFStringRef)string,
                                                                    charsUnchanged,
                                                                    charsToEscape,
                                                                    CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)) autorelease];
    } 

thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM