• iOS中文网址路径转换URLEncode


    方法1:

    NSString* encodedString = [urlStringstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    方法2:

    NSString * encodedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)urlString,NULL,NULL,kCFStringEncodingUTF8);

    如果在所有的类里都要用到这个方法,可以写成category,然后在头文件import "NSString+URL.h" 即可调用.

    NSString+URL.h  文件

    @interface NSString (URL)
    - (NSString *)URLEncodedString;
    @end
    

    NSString+URL.m  文件

    #import "NSString+URL.h"
     
    @implementation NSString (URL)
     
    - (NSString *)URLEncodedString
    {
        NSString *encodedString = (NSString *)
        CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                (CFStringRef)self,
                                                (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",
                                                NULL,
                                                kCFStringEncodingUTF8);
        return encodedString;
    }
    @end
    

    调用方法:

        NSString *encodedString = [urlString URLEncodedString];

        //encodedString do something

  • 相关阅读:
    列式数据库
    Subway POJ
    操作系统知识汇总
    Linux工具指南
    常用数据结构
    bzoj1257: [CQOI2007]余数之和 整除分块
    HDU
    hdu1693 Eat the Trees 插头dp
    HDU
    poj2411 轮廓线dp裸题
  • 原文地址:https://www.cnblogs.com/newBlash/p/4288104.html
Copyright © 2020-2023  润新知