From a97e0bcc064acbae8eed290c8415e48232b1c72c Mon Sep 17 00:00:00 2001 From: JvanChow Date: Tue, 10 Jan 2017 19:39:37 +0800 Subject: [PATCH] =?UTF-8?q?FIX:=20If=20HTTP=20URL=20only=20contains=20frag?= =?UTF-8?q?ment.=20When=20I=20call=20=E2=80=98uq=5FURLByAppendingQueryDict?= =?UTF-8?q?ionary=E2=80=99,=20it=20returns=20nil.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit e.g. NSURL *url = [NSURL URLWithString:@"https://test.com#test1"]; url = [url uq_URLByAppendingQueryDictionary:@{@"key" : @"value"}]; Then, url is nil. --- NSURL+QueryDictionary/NSURL+QueryDictionary.m | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/NSURL+QueryDictionary/NSURL+QueryDictionary.m b/NSURL+QueryDictionary/NSURL+QueryDictionary.m index b29de46..22fab54 100644 --- a/NSURL+QueryDictionary/NSURL+QueryDictionary.m +++ b/NSURL+QueryDictionary/NSURL+QueryDictionary.m @@ -37,13 +37,16 @@ - (NSURL *)uq_URLByAppendingQueryDictionary:(NSDictionary *)queryDictionary if (newQuery.length) { NSArray *queryComponents = [self.absoluteString componentsSeparatedByString:kQueryBegin]; if (queryComponents.count) { - return [NSURL URLWithString: - [NSString stringWithFormat:@"%@%@%@%@%@", - queryComponents[0], // existing url - kQueryBegin, - newQuery, - self.fragment.length ? kFragmentBegin : @"", - self.fragment.length ? self.fragment : @""]]; + NSArray *fragmentComponents = [queryComponents[0] componentsSeparatedByString:kFragmentBegin]; + if (fragmentComponents.count) { + return [NSURL URLWithString: + [NSString stringWithFormat:@"%@%@%@%@%@", + fragmentComponents[0], // existing url + kQueryBegin, + newQuery, + self.fragment.length ? kFragmentBegin : @"", + self.fragment.length ? self.fragment : @""]]; + } } } return self;