Last active
December 16, 2015 07:49
-
-
Save monjer/5401428 to your computer and use it in GitHub Desktop.
NSURL 扩展
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // NSURL+Extend.h | |
| // | |
| // Created by manjun.han on 13-4-17. | |
| // | |
| #import <Foundation/Foundation.h> | |
| @interface NSURL (Extend) | |
| + (NSString *) encodeURLParams: (NSDictionary *)urlParamsDictionary; | |
| + (NSDictionary *) decodeURLParams: (NSString *)urlParamStr; | |
| + (NSURL *) buildURL: (NSString *)baseURL params: (NSDictionary *)urlParams; | |
| + (NSDictionary *) parseURLToDictionary:(NSURL *)url ; | |
| + (BOOL) isFullURL:(NSString *)url; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // NSURL+Extend.m | |
| // | |
| // Created by manjun.han on 13-4-17. | |
| // | |
| #import "NSURL+Extend.h" | |
| @implementation NSURL (Extend) | |
| + (NSString *) encodeURLParams: (NSDictionary *)urlParamsDictionary | |
| { | |
| NSMutableArray *paramList = [NSMutableArray array]; | |
| for (NSString* key in urlParamsDictionary.keyEnumerator) { | |
| id value = [urlParamsDictionary valueForKey:key]; | |
| id kvStr = [NSString stringWithFormat:@"%@=%@", key, [value stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; | |
| [paramList addObject: kvStr]; | |
| } | |
| return [paramList componentsJoinedByString:@"&"]; | |
| } | |
| + (NSDictionary *) decodeURLParams: (NSString *)urlParamStr | |
| { | |
| NSMutableDictionary *paramDir = [[NSMutableDictionary alloc] init]; | |
| NSArray *kvStrsList = [urlParamStr componentsSeparatedByString: @"&"]; | |
| for (NSString *kvStr in kvStrsList) { | |
| NSArray *kv = [kvStr componentsSeparatedByString: @"="]; | |
| [paramDir setObject:[[kv objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] | |
| forKey:[kv objectAtIndex:0]]; | |
| } | |
| return paramDir; | |
| } | |
| + (NSURL *) buildURL: (NSString *)baseURL params: (NSDictionary *)urlParams | |
| { | |
| if (!urlParams || urlParams.count == 0) { | |
| return [NSURL URLWithString:baseURL]; | |
| } | |
| NSRange range = [baseURL rangeOfString:@"?"]; | |
| NSString *joinChar = (range.location == NSNotFound)? @"?" : @"&"; | |
| NSString *paramsStr = [self encodeURLParams:urlParams]; | |
| // baseURL , | |
| return [NSURL URLWithString:[NSString stringWithFormat:@"%@%@%@", baseURL, joinChar, paramsStr]]; | |
| } | |
| + (NSDictionary *) parseURLToDictionary:(NSURL *)url | |
| { | |
| NSString *query = [url query]; | |
| return [self decodeURLParams: query]; | |
| } | |
| + (BOOL) isFullURL:(NSString *)url | |
| { | |
| if (url.length == 0 || [[url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) { | |
| return NO; | |
| } | |
| url = [url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; | |
| NSRange httpRange = [url rangeOfString:@"http:"]; | |
| NSRange httpsRange = [url rangeOfString:@"https:"]; | |
| if (httpRange.location == 0 || httpsRange.location == 0){ | |
| return YES; | |
| } | |
| return NO; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment