美文网首页iOS开发技术iOS学习笔记
“URL小能手”NSURLComponents

“URL小能手”NSURLComponents

作者: lucky_lee | 来源:发表于2019-08-07 15:54 被阅读106次
背景
  • 在项目开发中,移动原生应用会经常嵌入一些H5页面,根据不同的业务场景,可能需要移动端拼接上不同的业务参数。(比如展示一个订单详情,后端返回了一个baseURL,我们需要拼接订单号,或者是其他的业务flag)。那么这个时候端上为了容错,有些时候会判断后端返回的URL中的一些特殊符号(?&&&...),根据是否有相应的拼接符进行不同的起始拼接。
if ([urlPath rangeOfString:@"?"].location == NSNotFound) {
    urlPath = [urlPath stringByAppendingFormat:@"?orderId=%@&type=%lu&lang=%@&cityId=%ld", orderId,type,lang,cityId];
} else {
    urlPath = [urlPath stringByAppendingFormat:@"orderId=%@&type=%lu&lang=%@&cityId=%ld", orderId,type,lang,cityId];
}

诸如此类的代码,不知道有多少小伙伴踩过坑~

  • WebView加载的页面地址中提取参数(估计又有很多小伙伴会根据&进行字符串的分割...)

那么这个时候如果使用NSURLComponents进行组合和分解将会事半功倍。

相关知识延伸

在一维数据类型中,URI占据了至高无上的地位。这里,在单个的,可解析的字符串中,是编码在计算机上具有,确实和将要存在的任何信息的位置所必需的每条可想到的信息。

在最基本的形式中,URI由方案名称和层次结构部分组成,带有可选的查询和片段:

<scheme name> : <hierarchical part> [ ? <query> ] [ # <fragment> ]

许多协议(包括HTTP)为层次结构部分中的用户名,密码,端口和路径等信息指定常规结构:

网址结构

扎实地掌握网络编程,做到对URL组件的熟悉。

NSURLComponents

NSURLComponents 在iOS 7.0和macOS 10.9中引入的,所以从时间上看已经是存在一段时间了。
要创建NSURLComponents对象,您可以使用a String或an URL

正如官方文档所描述:

If resolvingAgainstBaseURL is true and url is a relative URL, the components of url.absoluteURL are used. If the url string from the URL is malformed, nil is returned.

下面我们就根据下面的代码,进行一个简单的介绍分析

NSString * urlString = @"http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=image";
NSURLComponents * urlComponents = [[NSURLComponents alloc]initWithString:urlString];
NSLog(@"urlComponents.host = \n%@",urlComponents.host);//>>image.baidu.com
NSLog(@"urlComponents.scheme = \n%@",urlComponents.scheme);//>>http
NSLog(@"urlComponents.path = \n%@",urlComponents.path);//>>/search/index
NSLog(@"urlComponents.query = \n%@",urlComponents.query);//>>tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=image

这个时候大家会不会就有眼前一亮的感觉😁
如果在配合下面一句呢

 NSLog(@"urlComponents.ququeryItems=%@",urlComponents.queryItems);//>>(
    "<NSURLQueryItem 0x6000016fe720> {name = tn, value = baiduimage}",
    "<NSURLQueryItem 0x6000016fe740> {name = ps, value = 1}",
    "<NSURLQueryItem 0x6000016fe760> {name = ct, value = 201326592}",
    "<NSURLQueryItem 0x6000016fe780> {name = lm, value = -1}",
    "<NSURLQueryItem 0x6000016fe7a0> {name = cl, value = 2}",
    "<NSURLQueryItem 0x6000016fe7c0> {name = nc, value = 1}",
    "<NSURLQueryItem 0x6000016fe7e0> {name = ie, value = utf-8}",
    "<NSURLQueryItem 0x6000016fe800> {name = word, value = image}"
)

可以看出,为我们提供了一个NSURLQueryItem,各个查询项的键/值对象,这使我们更容易追加查询参数,或检查URL是否包含查询参数。

按照这个思路,各位小伙伴,现在是不是可以放飞自我了😏,对已有的URL进行尽情的组合,分解。

拼装
NSURLQueryItem * item = [[NSURLQueryItem alloc]initWithName:@"imageSize" value:@"1024*1024"];
NSMutableArray * items = [NSMutableArray arrayWithArray:urlComponents.queryItems];
[items addObject:item];
urlComponents.queryItems = [items copy];
NSLog(@"urlComponents.ququeryItems=%@",urlComponents.queryItems);//>>(
    "<NSURLQueryItem 0x6000037712a0> {name = tn, value = baiduimage}",
    "<NSURLQueryItem 0x6000037712c0> {name = ps, value = 1}",
    "<NSURLQueryItem 0x6000037712e0> {name = ct, value = 201326592}",
    "<NSURLQueryItem 0x600003771300> {name = lm, value = -1}",
    "<NSURLQueryItem 0x600003771320> {name = cl, value = 2}",
    "<NSURLQueryItem 0x600003771340> {name = nc, value = 1}",
    "<NSURLQueryItem 0x600003771360> {name = ie, value = utf-8}",
    "<NSURLQueryItem 0x600003771380> {name = word, value = image}",
    "<NSURLQueryItem 0x6000037713a0> {name = imageSize, value = 1024*1024}"
)

再来打印一下urlComponents.query

NSLog(@"urlComponents.query = \n%@",urlComponents.query);//>>tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=image&imageSize=1024*1024

简单而美观,所有的辛勤工作都被委托给NSURLComponents我们甚至不用担心是否使用?&...

分解

如上述代码,将数据遍历,就可以拿到单个的NSURLQueryItem,此时咱们可以处理 包含、去重等等操作,现在我们知道NSURLQueryItems检查查询参数是否存在太容易了。😁

现在在回头看,现在的处理是不是更加的优雅,便于维护,便于扩展了呢。

如果你想更好的运用和深入地了解,可以去apple官方文档查阅。

参考资料

相关文章

  • “URL小能手”NSURLComponents

    背景 在项目开发中,移动原生应用会经常嵌入一些H5页面,根据不同的业务场景,可能需要移动端拼接上不同的业务参数。(...

  • [iOS-Foundation] NSURL

    NSURL /NSURLComponents URI URL(统一资源定位符) 是一种 URI,URN(统一资源名...

  • 2019-02-21

    NSURLComponents *c = [NSURLComponents componentsWithStrin...

  • iOS 获取URL中的参数

    在开发中经常遇到通过URL来获取参数,下面总结了几个方法 通过 NSURLComponents 获取 通过 key...

  • NSURLComponents 使用简介 - iOS

    该类苹果在 iOS 7中添加,它(NSURLComponents)可以方便的把 URL 地址分解成多个部分; 其中...

  • iOS NSURLComponents 遇到的坑

    iOS NSURLComponents 遇到的坑 项目中突然有个需求,根据服务端下的地址,自己去拼完整的 url。...

  • NSURLComponents

    NSURLComponents 苹果在 iOS 7 和 OS X Mavericks 中悄悄添加了 NSURLCo...

  • NSUrl/NSUrlComponents

    可以参考这个新接手的项目与原来的项目在请求接口的业务区分上有差别,原负责项目的接口区分业务都是用接口中的某个参数暂...

  • iOS 使用NSURLComponents提取URL中的各个参数

  • 寒假日记16邹俊良诗

    《我是小能手》 我是小能手 我是什么小能手? 写作业小能手; 家务小能手; 吃东西小能手。 《昨夜的雨》 昨夜的雨...

网友评论

    本文标题:“URL小能手”NSURLComponents

    本文链接:https://www.haomeiwen.com/subject/wiyodctx.html