在iOS中通过WebView加载Url或者请求HTTP时,若是链接中包含中文、特殊符号&%或是空格等都需要预先进行一下转码才可正常访问。

许久没编码,原先的方法已废弃了都,在此对应当前最新的方法进行记录:

官方源码“NSRUL.h”文件中可以看到如下信息:后两个方法已废弃,从iOS7.0开始提供新的两个方法:

@interface NSString (NSURLUtilities)

// 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 API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0)); // Returns a new string made from the receiver by replacing all percent encoded sequences with the matching UTF-8 characters.
@property (nullable, readonly, copy) NSString *stringByRemovingPercentEncoding API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0)); - (nullable NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)enc API_DEPRECATED("Use -stringByAddingPercentEncodingWithAllowedCharacters: instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid.", macos(10.0,10.11), ios(2.0,9.0), watchos(2.0,2.0), tvos(9.0,9.0));
- (nullable NSString *)stringByReplacingPercentEscapesUsingEncoding:(NSStringEncoding)enc API_DEPRECATED("Use -stringByRemovingPercentEncoding instead, which always uses the recommended UTF-8 encoding.", macos(10.0,10.11), ios(2.0,9.0), watchos(2.0,2.0), tvos(9.0,9.0)); @end

一:stringByAddingPercentEncodingWithAllowedCharacters方法:是将普通字符转为百分比编码字符;

调用方法如下:

NSString *originalString = @"浙江省";

NSString *charactersToEscape = @"?!@#$^&%*+,:;='\"`<>()[]{}/\\| ";
NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
NSString *encodeString = [originalString stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];

eg. “浙江省”转码后即变成了“%E6%B5%99%E6%B1%9F%E7%9C%81”;

方法二:stringByRemovingPercentEncoding方法:则是将百分比编码字符重新转会普通字符;

调用方法如下:

NSString *decodeString = [encodeString stringByRemovingPercentEncoding];

iOS 9之后Url链接的NSUTF8StringEncoding转码实现的更多相关文章

  1. ios系统中各种设置项的url链接

    ios系统中各种设置项的url链接 在代码中调用如下代码:NSURL*url=[NSURL URLWithString:@"prefs:root=WIFI"];[[UIApplic ...

  2. iOS解析新浪微博的@##以及URL链接并展示

    最近在做一个跟微博相关的应用.其中涉及到了对微博中@.##以及URL链接的解析与展示.分享一下个人处理的方式,希望对需要的人有所帮助. 最终的展现效果: 首先,第一步是你得从纯文本中找到它们.毫无疑问 ...

  3. iOS Universal Links(通用链接)

    公司的运维,发现最近大量的请求 /.well-known/apple-app-site-association这个文件,造成了大量的404,可是这是谁请求的呢? 其实是苹果从iOS9.3开始更改了通用 ...

  4. iOS 9学习系列:打通 iOS 9 的通用链接(Universal Links)

    在WWDC 2015 上, Apple 为 iOS 9 宣布了一个所谓 通用链接 的深层链接特性, 视频地址为 [无缝链接到您的 App].虽然它不是一个必须实现的功能, 但还是需要引起一些注意. 在 ...

  5. iOS/Android/Web Url Encode空格處理 原文連結:http://read01.com/3gDO.html

    iOS/Android/Web Url Encode空格處理 原文連結:http://read01.com/3gDO.html 前言 這裡只是講一個故事,一個發生在我身上的真實的故事.曾經,我以為搞加 ...

  6. android/IOS各平台分享链接/跳转链接配置说明(备用)

    Android: [Java] 纯文本查看 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 微信/朋友圈 //设置 ...

  7. 文顶顶iOS开发博客链接整理及部分项目源代码下载

    文顶顶iOS开发博客链接整理及部分项目源代码下载   网上的iOS开发的教程很多,但是像cnblogs博主文顶顶的博客这样内容图文并茂,代码齐全,示例经典,原理也有阐述,覆盖面宽广,自成系统的系列教程 ...

  8. [CareerCup] 10.6 Find Duplicate URLs 找重复的URL链接

    10.6 You have 10 billion URLs. How do you detect the duplicate documents? In this case, assume that ...

  9. URL链接中文参数乱码的若干处理方法

    JAVA 中URL链接中文参数乱码的若干处理方法,现在整理收录如下: 方法一: (1) JS中,在URL参数中确保用UTF-8编码,用js函数encodeURI()编码,例如 url:"xx ...

随机推荐

  1. Ps 应用小技巧总结

    一.如何等比例放大图片? 使用形状工具,画图之后,保存为智能对象,但是 ctrl+T 之后放大,会有虚边: 解决办法:编辑智能对象,在新的画布中:图像---图像大小----ctrl+alt+I 此处进 ...

  2. Redux中间件组合方法

    中间件 https://redux.js.org/glossary#middleware 中间件在发送端 action端点 和 处理方reducer的端点之间. 主要负责将async action转换 ...

  3. linxu上安装mongodb3.6实战

    根据linux 版本到官网下载对应mongodb版本 查看服务器版本:cat /proc/version 查看linux发行版本:cat /etc/redhat-release 我用的阿里云服务器,对 ...

  4. Visual Studio Code Shortcuts

    https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf https://code.visualstudio.com ...

  5. cocos2d-x入门学习笔记——Hello world分析

    Hello world分析 1. “resource”文件夹 用于存放图片.音频和配置等资源文件.为了方便管理,可以创建在其中创建子文件夹.Cocos2d-x为我们屏蔽了不同平台对于文件路径的定义. ...

  6. 初始化仓库(git init)

    创建新的仓库 首先进入需要初始化的目录,然后输入git init D:\Git\test λ git init Initialized empty Git repository in D:/Git/t ...

  7. django QuerySet

    Django对数据库的封装1——QuerySet 发布时间:2018-02-28 来源:网络 上传者:用户 关键字: manager 数据库 记录 影响 发表文章 摘要:   Django对数据库的操 ...

  8. Tomcat 改端口

    Tomcat 改端口 进入 tomcat 解压包下面的 conf 目录 打开文件 server.xml 找到以下 三处位置,并改为对应端口 1)找到 8005 <Server port=&quo ...

  9. 【Flask】报错解决方法:AssertionError: View function mapping is overwriting an existing endpoint function: main.user

    运行Flask时出现了一个错误, AssertionError: View function mapping is overwriting an existing endpoint function: ...

  10. 洛谷 P1983 车站分级

    题目链接 https://www.luogu.org/problemnew/show/P1983 题目描述 一条单向的铁路线上,依次有编号为 1,2,…,n的 n个火车站.每个火车站都有一个级别,最低 ...