Objective-C:如何在NSURL中添加查询参数



假设我有一个NSURL?无论它是否已经有一个空的查询字符串,我如何向NSURLquery添加一个或多个参数?也就是说,有人知道这个功能的实现吗?

- (NSURL *)URLByAppendingQueryString:(NSString *)queryString

从而满足这个NSURL+AdditionsSpec.h文件:

#import "NSURL+Additions.h"
#import "Kiwi.h"
SPEC_BEGIN(NSURL_AdditionsSpec)
describe(@"NSURL+Additions", ^{
    __block NSURL *aURL;
    beforeEach(^{
        aURL = [[NSURL alloc] initWithString:@"http://www.example.com"];
        aURLWithQuery = [[NSURL alloc] initWithString:@"http://www.example.com?key=value"];
    });
    afterEach(^{
        [aURL release];
        [aURLWithQuery release];
    });
    describe(@"-URLByAppendingQueryString:", ^{
        it(@"adds to plain URL", ^{
            [[[[aURL URLByAppendingQueryString:@"key=value&key2=value2"] query] should]
             equal:@"key=value&key2=value2"];
        });
        it(@"appends to the existing query sting", ^{
            [[[[aURLWithQuery URLByAppendingQueryString:@"key2=value2&key3=value3"] query] should]
             equal:@"key=value&key2=value2&key3=value3"];
        });
    });
});
SPEC_END

由于iOS 7,您可以使用非常简单的NSURLComponents。看看这些例子:

示例1

NSString *urlString = @"https://mail.google.com/mail/u/0/?shva=1#inbox";
NSURLComponents *components = [[NSURLComponents alloc] initWithString:urlString];
NSLog(@"%@ - %@ - %@ - %@", components.scheme, components.host, components.query, components.fragment);

示例2

NSString *urlString = @"https://mail.google.com/mail/u/0/?shva=1#inbox";
NSURLComponents *components = [[NSURLComponents alloc] initWithString:urlString];
if (components) {
    //good URL
} else {
    //bad URL
}

示例3

NSURLComponents *components = [NSURLComponents new];
[components setScheme:@"https"];
[components setHost:@"mail.google.com"];
[components setQuery:@"shva=1"];
[components setFragment:@"inbox"];
[components setPath:@"/mail/u/0/"];
[self.webview loadRequest:[[NSURLRequest alloc] initWithURL:[components URL]]];

但是,您可以使用NSURLComponents做许多其他事情。请查看Apple文档或此链接上的NSURLCmponents类参考:http://nshipster.com/nsurl/

这里有一个通过您的规范的实现:

@implementation NSURL (Additions)
- (NSURL *)URLByAppendingQueryString:(NSString *)queryString {
    if (![queryString length]) {
        return self;
    }
    NSString *URLString = [[NSString alloc] initWithFormat:@"%@%@%@", [self absoluteString],
                           [self query] ? @"&" : @"?", queryString];
    NSURL *theURL = [NSURL URLWithString:URLString];
    [URLString release];
    return theURL;
}
@end

这里是NSString:的一个实现

@implementation NSString (Additions)
- (NSURL *)URLByAppendingQueryString:(NSString *)queryString {
    if (![queryString length]) {
        return [NSURL URLWithString:self];
    }
    NSString *URLString = [[NSString alloc] initWithFormat:@"%@%@%@", self,
                           [self rangeOfString:@"?"].length > 0 ? @"&" : @"?", queryString];
    NSURL *theURL = [NSURL URLWithString:URLString];
    [URLString release];
    return theURL;
}
// Or:
- (NSString *)URLStringByAppendingQueryString:(NSString *)queryString {
    if (![queryString length]) {
        return self;
    }
    return [NSString stringWithFormat:@"%@%@%@", self,
            [self rangeOfString:@"?"].length > 0 ? @"&" : @"?", queryString];
}
@end

iOS8+现代方式

向min60.com上的url添加(或替换"ref"值(如果存在))ref=impm

if ([[url host] hasSuffix:@"min60.com"]) {
    NSURLComponents *components = [[NSURLComponents alloc] initWithURL:url resolvingAgainstBaseURL:NO];
    NSURLQueryItem * newQueryItem = [[NSURLQueryItem alloc] initWithName:@"ref" value:@"impm"];
    NSMutableArray * newQueryItems = [NSMutableArray arrayWithCapacity:[components.queryItems count] + 1];
    for (NSURLQueryItem * qi in components.queryItems) {
        if (![qi.name isEqual:newQueryItem.name]) {
            [newQueryItems addObject:qi];
        }
    }
    [newQueryItems addObject:newQueryItem];
    [components setQueryItems:newQueryItems];
    url = [components URL];
}

对于那些不想在用NSURLComponents构建NSURL时编写样板代码的人来说,这只是一篇友好的帖子
自从iOS8以来,我们有了NSURLQueryItem,它可以帮助快速构建URL请求。

我写了一个方便的分类来简化工作,你可以在这里获取:URLQueryBuilder
以下是使用它有多容易的例子:

NSString *baseURL = @"https://google.com/search";
NSDictionary *items = @{
    @"q"  : @"arsenkin.com",
    @"hl" : @"en_US",
    @"lr" : @"lang_en"
};
NSURL *URL = [NSURL ars_queryWithString:baseURL queryElements:items];  
// https://google.com/search?q=arsenkin.com&hl=en_US&lr=lang_en

我有一个NSURLComponents的扩展,它在swift:中添加了查询项

extension NSURLComponents {
    func appendQueryItem(name name: String, value: String) {
        var queryItems: [NSURLQueryItem] = self.queryItems ?? [NSURLQueryItem]()
        queryItems.append(NSURLQueryItem(name: name, value: value))
        self.queryItems = queryItems
    }
}

使用

let components = NSURLComponents(string: urlString)!
components.appendQueryItem(name: "key", value: "value")

如果您使用RestKit,它会提供对NSString的添加。其中之一是:

- (NSString *)stringByAppendingQueryParameters:(NSDictionary *)queryParameters

所以你可以做:

NSDictionary *shopParams = [NSDictionary dictionaryWithKeysAndObjects:
                                @"limit",@"20", 
                                @"location",@"latitude,longitude",
                                nil];
NSString *pathWithQuery = [@"/api/v1/shops.json" stringByAppendingQueryParameters:shopParams]

正如其他人所提到的,您可以使用NSURLComponents来构建URL。

@implementation NSURL (Additions)
- (NSURL *)URLByAppendingQueryParameters:(NSDictionary *)queryParameters
{        
    NSURLComponents *components = [[NSURLComponents alloc] initWithURL:self resolvingAgainstBaseURL:NO];
    NSMutableArray *queryItems = [NSMutableArray array:components.queryItems];
    for (NSString *key in [queryParameters allKeys]) {
        NSURLQueryItem *queryItem = [[NSURLQueryItem alloc] initWithName:key value:queryParameters[key]];
        [queryItems addObject:queryItem];
    }
    components.queryItems = queryItems;
    return [components URL];
}
@end

NSURL是不可变的,因此您不能直接基于NSURL实现此功能。相反,您必须获得URL的字符串表示,将参数附加到该字符串表示,然后创建一个新的NSURL。

这听起来不是一个好的解决方案。除非有充分的理由,否则最好使用字符串直到最后一刻,并且只有在收到完整格式的请求时才创建NSURL。

最新更新