我想将用于在 iPhone 中打开应用程序的自定义 url 拆分为值,我的方案类似于:
appname://user=jonsmith&message=blah%20blah
我希望能够将“用户”和“消息”作为两个 NSString 获取。关于最佳方法的任何建议?
最佳答案
假设您的 url 位于名为 url
的 NSURL
对象中:
NSMutableDictionary *queryParams = [[NSMutableDictionary alloc] init];
NSArray *components = [[url query] componentsSeparatedByString:@"&"];
for (NSString *component in components) {
NSArray *pair = [component componentsSeparatedByString:@"="];
[queryParams setObject:[[pair objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding: NSMacOSRomanStringEncoding]
forKey:[pair objectAtIndex:0]];
}
...
[queryParams release];
关于iphone - 将 url 参数拆分并转换为字符串值的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2119640/