2012-07-30 72 views
0

exctract数据我有以下NSSURL如何从NSURL

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

我怎样才能获得URL组件?

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

这在日志中: 网址:

网址:myPlatform:// SDK/functionName键1 =值&键2 =值2

组件:/

组件:functionName

在此先感谢

+0

http://stackoverflow.com/questions/1967399/parse-nsurl-path-and-query -iphoneos – Maulik 2012-07-30 07:18:49

回答

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]; 
} 
+0

谢谢alllllllloooooot! – Luda 2012-07-30 07:53:11