如何从NSURL中提取数据

时间:2012-07-30 07:12:13

标签: iphone objective-c xcode nsstring nsurl

我有以下NSSURL

myURL - myPlatform://sdk/functionName?key1=value1&key2=value2

如何获取网址组件?

NSString *myPlatform = [myURL...?
NSString *functionName = [myURL...?
NSString *key1 = [myURL...?
NSString *value1 = [myURL...?
NSString *key2 = [myURL...?
NSString *value2 = [myURL...?

我有这段代码:

- (BOOL)webView:(UIWebView *)webView2 shouldStartLoadWithRequest:(NSURLRequest *)request     navigationType:(UIWebViewNavigationType)navigationType {
    NSURL *url= [request URL];
    Log(@"url: %@", url);
    NSArray *components = [url pathComponents];  
    for (NSString *c in components)
        Log(@"URL component: %@", c);
}

这在日志中: 网址:

  

url:myPlatform:// sdk / functionName?key1 = value1& key2 = value2

     

组件:/

     

组件:functionName

提前致谢

1 个答案:

答案 0 :(得分:1)

它在文档中:

NSString *myPlatfrom = [myURL scheme];
NSString *sdk = [myURL host];
NSString *functionName = [[myURL path] substringFromIndex:1];
NSString *query = [myURL query];
for (NSString *arg in [query componentsSeparatedByString:"&"]) {
    NSArray argComponents = [arg componentsSeparatedByString:"="];
    NSString key = [argComponents[0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString value = [argComponents[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}