iOS 9之后Url链接的NSUTF8StringEncoding转码实现
在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转码实现的更多相关文章
- ios系统中各种设置项的url链接
ios系统中各种设置项的url链接 在代码中调用如下代码:NSURL*url=[NSURL URLWithString:@"prefs:root=WIFI"];[[UIApplic ...
- iOS解析新浪微博的@##以及URL链接并展示
最近在做一个跟微博相关的应用.其中涉及到了对微博中@.##以及URL链接的解析与展示.分享一下个人处理的方式,希望对需要的人有所帮助. 最终的展现效果: 首先,第一步是你得从纯文本中找到它们.毫无疑问 ...
- iOS Universal Links(通用链接)
公司的运维,发现最近大量的请求 /.well-known/apple-app-site-association这个文件,造成了大量的404,可是这是谁请求的呢? 其实是苹果从iOS9.3开始更改了通用 ...
- iOS 9学习系列:打通 iOS 9 的通用链接(Universal Links)
在WWDC 2015 上, Apple 为 iOS 9 宣布了一个所谓 通用链接 的深层链接特性, 视频地址为 [无缝链接到您的 App].虽然它不是一个必须实现的功能, 但还是需要引起一些注意. 在 ...
- iOS/Android/Web Url Encode空格處理 原文連結:http://read01.com/3gDO.html
iOS/Android/Web Url Encode空格處理 原文連結:http://read01.com/3gDO.html 前言 這裡只是講一個故事,一個發生在我身上的真實的故事.曾經,我以為搞加 ...
- android/IOS各平台分享链接/跳转链接配置说明(备用)
Android: [Java] 纯文本查看 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 微信/朋友圈 //设置 ...
- 文顶顶iOS开发博客链接整理及部分项目源代码下载
文顶顶iOS开发博客链接整理及部分项目源代码下载 网上的iOS开发的教程很多,但是像cnblogs博主文顶顶的博客这样内容图文并茂,代码齐全,示例经典,原理也有阐述,覆盖面宽广,自成系统的系列教程 ...
- [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 ...
- URL链接中文参数乱码的若干处理方法
JAVA 中URL链接中文参数乱码的若干处理方法,现在整理收录如下: 方法一: (1) JS中,在URL参数中确保用UTF-8编码,用js函数encodeURI()编码,例如 url:"xx ...
随机推荐
- 计算机网络Web应用层与运输层(HTTP/TCP)
应用层协议原理 Web和HTTP DNS:英特网的目录服务 运输层 面向连接的运输:TCP及拥塞原理 一.应用层协议原理 DNS域名解析: (用例:www.baidu.com)域名解析是网络请求的第一 ...
- 一次使用innobackupex重新搭建主从复制报错解决方法及注意事项
[环境介绍] 系统环境:CentOS release 6.4 (Final) + Server version: 5.7.18-log MySQL Community Server (GPL) + i ...
- VMware 安装 Mac OS 注意事项
Ø 简介 本文主要介绍使用 VMware 安装 Mac OS 的注意事项,主要包括一下内容: 1. 安装参考 2. 使用 VMware 运行 Mac OS 虚拟机注意事项 3. 解决 M ...
- python3 练手实例4 九九乘法口诀表
for i in range(1,10): for j in range(1,i+1): print('{}*{}={}\t'.format(i,j,i*j),end='') print()
- QButtonGroup按钮组
继承 QObject 提供 一个抽象的按钮容器, 可以将多个按钮划分为一组,不具备可视化的效果,一般放的都是可以被检查的按钮 import sys from PyQt5.QtWidgets impo ...
- 堆应用---构造Huffman树(C++实现)
堆: 堆是STL中priority_queue的最高效的实现方式(关于priority_queue的用法:http://www.cnblogs.com/flyoung2008/articles/213 ...
- VS发布网站时,报错提示:“未能将文件xxx复制到xxx,未能找到文件xx”三种解决方案!
发布网站时候大家可能会遇到这样的情况,就是报错提示说:“未能将文件xxx复制到xxx,未能找到文件xx”,这个问题一般来说有三种解决方案,个人倾向第三种,如图: 解决方案如下: 方案一.把系统提示缺失 ...
- 在vscode中使用eslint+prettier格式化vue项目代码 (转载)
ESlint:javascript代码检测工具,可以配置每次保存时格式化js,但每次保存只格式化一点点,你得连续按住Ctrl+S好几次,才格式化好,自行体会~~ vetur:可以格式化html.标准c ...
- Mac os x下几款mysql客户端
Mac os x几款mysql客户端 1,Navicat 这是一款可支持多种数据库的客户端,可支持mysql,sqlite,oracle,sql server等数据库.当然也可以选择只支持某种数据库的 ...
- ABP core学习之二 IIS部署.NET CORE
本文是关于IIS部署.NET CORE的总结,以后有碰到问题将陆续添加 IIS部署.NET CORE总结 一.服务器环境 首先确定自己项目的core版本,然后下载对应的包在服务器上安装 下载地址: h ...