escape编码 ios
2016-11-01 17:47:00 weixin_34236869 阅读数 26

与轻应用对接遇到中文编码问题,轻应用端对中文编码使用的是 escape(),UIWebView不识别这样的URL; 

并且stringByReplacingPercentEscapesUsingEncoding 解码失败返回nil;

解决方案:轻应用端将编码格式转为 encodeURL()

var b = '桂PZ0121'

escape(b)

"%u6842PZ0121"

encodeURI(b)

"%E6%A1%82PZ0121"

2018-10-02 23:32:00 weixin_33941350 阅读数 63
/**
 字符串转义

 @param str 需要转义的字符串
 @return 转义后的字符串
 */
-(NSString *)encodeWithEscape:(NSString *)str
{
    NSArray *hex = [NSArray arrayWithObjects:
                    @"00",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"0A",@"0B",@"0C",@"0D",@"0E",@"0F",
                    @"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"1A",@"1B",@"1C",@"1D",@"1E",@"1F",
                    @"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"2A",@"2B",@"2C",@"2D",@"2E",@"2F",
                    @"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37",@"38",@"39",@"3A",@"3B",@"3C",@"3D",@"3E",@"3F",
                    @"40",@"41",@"42",@"43",@"44",@"45",@"46",@"47",@"48",@"49",@"4A",@"4B",@"4C",@"4D",@"4E",@"4F",
                    @"50",@"51",@"52",@"53",@"54",@"55",@"56",@"57",@"58",@"59",@"5A",@"5B",@"5C",@"5D",@"5E",@"5F",
                    @"60",@"61",@"62",@"63",@"64",@"65",@"66",@"67",@"68",@"69",@"6A",@"6B",@"6C",@"6D",@"6E",@"6F",
                    @"70",@"71",@"72",@"73",@"74",@"75",@"76",@"77",@"78",@"79",@"7A",@"7B",@"7C",@"7D",@"7E",@"7F",
                    @"80",@"81",@"82",@"83",@"84",@"85",@"86",@"87",@"88",@"89",@"8A",@"8B",@"8C",@"8D",@"8E",@"8F",
                    @"90",@"91",@"92",@"93",@"94",@"95",@"96",@"97",@"98",@"99",@"9A",@"9B",@"9C",@"9D",@"9E",@"9F",
                    @"A0",@"A1",@"A2",@"A3",@"A4",@"A5",@"A6",@"A7",@"A8",@"A9",@"AA",@"AB",@"AC",@"AD",@"AE",@"AF",
                    @"B0",@"B1",@"B2",@"B3",@"B4",@"B5",@"B6",@"B7",@"B8",@"B9",@"BA",@"BB",@"BC",@"BD",@"BE",@"BF",
                    @"C0",@"C1",@"C2",@"C3",@"C4",@"C5",@"C6",@"C7",@"C8",@"C9",@"CA",@"CB",@"CC",@"CD",@"CE",@"CF",
                    @"D0",@"D1",@"D2",@"D3",@"D4",@"D5",@"D6",@"D7",@"D8",@"D9",@"DA",@"DB",@"DC",@"DD",@"DE",@"DF",
                    @"E0",@"E1",@"E2",@"E3",@"E4",@"E5",@"E6",@"E7",@"E8",@"E9",@"EA",@"EB",@"EC",@"ED",@"EE",@"EF",
                    @"F0",@"F1",@"F2",@"F3",@"F4",@"F5",@"F6",@"F7",@"F8",@"F9",@"FA",@"FB",@"FC",@"FD",@"FE",@"FF", nil];
    
    NSMutableString *result = [NSMutableString stringWithString:@""];
    int strLength = (int)str.length;
    for (int i=0; i<strLength; i++) {
        int ch = [str characterAtIndex:i];
        if (ch == ' ')
        {
            [result appendFormat:@"%c",'+'];
        }
        else if ('A' <= ch && ch <= 'Z')
        {
            [result appendFormat:@"%c",(char)ch];
            
        }
        else if ('a' <= ch && ch <= 'z')
        {
            [result appendFormat:@"%c",(char)ch];
        }
        else if ('0' <= ch && ch<='9')
        {
            [result appendFormat:@"%c",(char)ch];
        }
        else if (ch == '-' || ch == '_'
                 || ch == '.' || ch == '!'
                 || ch == '~' || ch == '*'
                 || ch == '\'' || ch == '('
                 || ch == ')')
        {
            [result appendFormat:@"%c",(char)ch];
        }
        else if (ch <= 0x007F)
        {
            [result appendFormat:@"%%"];
            [result appendString:[hex objectAtIndex:ch]];
        }
        else
        {
            [result appendFormat:@"%%"];
            [result appendFormat:@"%c",'u'];
            [result appendString:[hex objectAtIndex:ch>>8]];
            [result appendString:[hex objectAtIndex:0x00FF & ch]];
        }
    }
    return result;
}

巨坑,记录下

2017-10-26 20:09:12 plpldog 阅读数 107






iOS编码
2016-05-06 16:37:13 xuchaovip 阅读数 247
1.定义通知名字
类名+xxxx+Notification

在OC中定义在.m中,extern在.h中


2.定义枚举名字
类名+xxxx+style type...


3.注释都在后边//xxxx

4.setter写在后边

5.函数的return如果创建返回的话中间使用一个空格, 其他情况不用加空格


6 命名

@property (nonatomic, strong) UILabel *statusLabel;

@property (nonatomic, strong) CALayer *backgroundLayer;
@property (nonatomic, strong, readonly) NSTimer *fadeOutTimer;

@property (nonatomic, readonly) CGFloat visibleKeyboardHeight;

@property (strong, nonatomic) UIImage *errorImage UI_APPEARANCE_SELECTOR;   
@property (strong, nonatomic) UIView *viewForExtension UI_APPEARANCE_SELECTOR; 

@property (assign, nonatomic) UIOffset offsetFromCenter

@property (assign, nonatomic) SVProgressHUDStyle defaultStyle


方法名: 动词+xxxx

set reset update show pop dismiss push regist move create setup


定义数组,字典集合类型使用xxxs 如HTTPHeaders

定义Block
// 方法参数带Block


7 方法顺序相同功能,参数多的放在下边

+ (void)show ;

+ (void)showWithMaskType:(SVProgressHUDMaskType)maskType;


8 被调用的方法写在下边

updateHUDFrame {

[self updateBlurBounds];

}

updateBlurBounds {

}


9 注释一组的 使用/**/

/* The initializers available to subclasses. See further below for additional init methods.
*/

注释单个的使用

//

定义的枚举, .h中的属性 都要有注释



iOS编码
2015-05-30 13:52:59 gaoyuqiang30 阅读数 248

gbk

NSStringEncoding enc =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);

utf8

NSUTF8StringEncoding

iso_8891

NSISOLatin1StringEncoding

编码

      %2b%2b%2b是url编码,如果是3个代表一个汉字,是Utf8;2个代表汉字,是gbk.

NSString *dataUTF8 = [@"测试"stringByAddingPercentEscapesUsingEncoding:enc];

或者

NSString* test =  (__bridge_transferNSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,  (__bridgeCFStringRef)@"测试"NULL,  (CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);


解码

    NSString* c = [bstringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


NSString 与 NSData互相转化

    NSData* data = [@"haha"dataUsingEncoding:NSUTF8StringEncoding];

    NSString* str = [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];


从Utf8转成gbk

      NSString* a = [@"你好" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

      NSString* b = [a stringByReplacingPercentEscapesUsingEncoding:gbk];

或者

    NSString* a = [@"你好" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString* b = [a stringByReplacingPercentEscapesUsingEncoding:gbk];


结果:浣犲ソ

从gbk转成utf8 同理,不过结果 为nil



iOS编码一次

阅读数 89

URL 编码 iOS

阅读数 19

iOS字符串编码

阅读数 29

iOS BCD编码

阅读数 2223

没有更多推荐了,返回首页