ios – 如何从NSURL中提取和删除方案名称?

什么是从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;
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...