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 ...
随机推荐
- 部署kubernetes-dashboard
1.生成访问证书 | awk '{print $2}' | base64 -d >> kubecfg.crt | awk '{print $2}' | base64 -d >> ...
- C#代码使用Process类调用SWFTools工具
一.Process类调用SWFTools工具将PDF文档转为swf文档 1 string cmdStr = "D:\\SWFTools\\pdf2swf.exe"; string ...
- react native( rn) 中关于navigationOptions中headerRight 获取navigation的问题 rn
使用以下代码获取navigation static navigationOptions = ({ navigation, navigationOptions }) => { const { pa ...
- Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) B. Weakened Common Divis
题目链接 让你找一个数,使得这个数,可以被每个二元组的两个数中的一个数整除. 先将第一个二元组的两个数质因数分解一下,分解的质数加入set中,然后,对剩下的n-1个二元组进行遍历,每次遍历到的二元组对 ...
- 开发一个项目之css
background属性 5+3 image,color,position,repeat,attachment size: 保持纵横比 contain 再发大就cover了 clip:背景绘制区域 b ...
- 多线程/多进程/异步IO
SOCK_STREAM :TCPSOCK_Dgram :UDP family=AF_INET: 服务器之间的通信AF_INET6: 服务器之间的通信AF_UNIX: Unix不同进程间的通信 永远遵循 ...
- elk的备份与恢复【转】
elasticsearch提供了快照功能: 1.在elsticsearch的配置文件中定义一个path.repo路径配置 path.repo: ["/elk/my_backup"] ...
- 前端笔记之JavaScript(五)关于数组和字符串那点事
一.数组 1.1数组概念 数组(array)是一个有序的数据集合.说白了,数组就是一组数.数组内部可以存放一个或多个单独的数据,整体组成数组. 定义数组最简单的方式:数组字面量. 数组的字面量“[]” ...
- 一、学习起步vue——安装
学习vue第一步:安装 (windows系统) 整个运行的命令: npm -v node -v 查看版本 npm uninstall -g vue-cli 卸载vue-cli npm install ...
- Idea中一些常用设置
idea展开和折叠方法的快捷键 Ctrl+”+/-”,当前方法展开.折叠Ctrl+Shift+”+/-”,全部展开.折叠 idea中也有自定代码块的功能 //region 描述.....业务代码//e ...