@"3+4/5" 所以 NSS-6ren">
gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:04:52 25 4
gpt4 key购买 nike

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);

解析查询字符串

- (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;
}

关于ios - 如何从 NSURL 中提取和删除方案名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17101227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com