什么是从NSURL中提取和删除方案名称和//的正确方法?
例如:
note://Hello -> @"Hello" calc://3+4/5 -> @"3+4/5"
所以
NSString *scheme = @"note://"; NSString *path = @"Hello";
供以后使用:
[[NSNotificationCenter defaultCenter] postNotificationName:scheme object:path];
解决方法
你可以这样看(大多数未经测试的代码,但你明白了):
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { NSLog(@"url: %@",url); NSLog(@"scheme: %@",[url scheme]); NSLog(@"query: %@",[url query]); NSLog(@"host: %@",[url host]); NSLog(@"path: %@",[url path]); NSDictionary * dict = [self parseQueryString:[url query]]; NSLog(@"query dict: %@",dict); }
所以你可以这样做:
NSString * strNoURLScheme = [strMyURLWithScheme stringByReplacingOccurrencesOfString:[url scheme] withString:@""]; NSLog(@"URL without scheme: %@",strNoURLScheme);
parseQueryString
- (NSDictionary *)parseQueryString:(NSString *)query { NSMutableDictionary *dict = [[[NSMutableDictionary alloc] initWithCapacity:6] autorelease]; NSArray *pairs = [query componentsSeparatedByString:@"&"]; for (NSString *pair in pairs) { NSArray *elements = [pair componentsSeparatedByString:@"="]; NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [dict setObject:val forKey:key]; } return dict; }