美文网首页
iOS获取URL参数

iOS获取URL参数

作者: OwenWong | 来源:发表于2019-07-23 17:19 被阅读0次

    NSURLComponents,iOS 7之后即可使用,可以完美替代 NSMutableURL

    /**
     获取url的所有参数
     @param url 需要提取参数的url
     @return NSDictionary
     */
    - (NSDictionary *)parameterWithURL:(NSURL *)url {
        NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithCapacity:2];
        
        //传入url创建url组件类
        NSURLComponents *urlComponents = [[NSURLComponents alloc] initWithString:url.absoluteString];
        
        //回调遍历所有参数,添加入字典
        [urlComponents.queryItems enumerateObjectsUsingBlock:^(NSURLQueryItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [params setObject:obj.value forKey:obj.name];
        }];
        
        return params;
    }
    

    NSURLComponents,参考文档:

    1、https://www.jianshu.com/p/9a5d2533d4e6
    2、苹果官方文档
    3、NSURL / NSURLComponents(附:原版)

    相关文章

      网友评论

          本文标题:iOS获取URL参数

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