forked from sonofsky2010/TQT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSURL+QAdditions.m
66 lines (52 loc) · 1.77 KB
/
NSURL+QAdditions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// NSURL+QAdditions.m
// QWeiboSDK4iOS
//
// Created on 11-1-13.
//
//
#import "NSURL+QAdditions.h"
@implementation NSURL (QAdditions)
#pragma mark -
#pragma mark Class methods
+ (NSDictionary *)parseURLQueryString:(NSString *)queryString {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSArray *pairs = [queryString componentsSeparatedByString:@"&"];
for(NSString *pair in pairs) {
NSArray *keyValue = [pair componentsSeparatedByString:@"="];
if([keyValue count] == 2) {
NSString *key = [keyValue objectAtIndex:0];
NSString *value = [keyValue objectAtIndex:1];
value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if(key && value)
[dict setObject:value forKey:key];
}
}
return [NSDictionary dictionaryWithDictionary:dict];
}
+ (NSURL *)smartURLForString:(NSString *)str {
NSURL * result;
NSString * trimmedStr;
NSRange schemeMarkerRange;
NSString * scheme;
assert(str != nil);
result = nil;
trimmedStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ( (trimmedStr != nil) && (trimmedStr.length != 0) ) {
schemeMarkerRange = [trimmedStr rangeOfString:@"://"];
if (schemeMarkerRange.location == NSNotFound) {
result = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", trimmedStr]];
} else {
scheme = [trimmedStr substringWithRange:NSMakeRange(0, schemeMarkerRange.location)];
assert(scheme != nil);
if ( ([scheme compare:@"http" options:NSCaseInsensitiveSearch] == NSOrderedSame)
|| ([scheme compare:@"https" options:NSCaseInsensitiveSearch] == NSOrderedSame) ) {
result = [NSURL URLWithString:trimmedStr];
} else {
// It looks like this is some unsupported URL scheme.
}
}
}
return result;
}
@end