NSURL组成部分详解

手思中有这么一段代码,初看下,让人摸不着头脑

//功能:UIWebView响应长按事件
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([request URL].query){
        NSArray *array = [[request URL].query componentsSeparatedByString:@"&"];
        if (array.count)
        {
            for (int i = 0; i<array.count; i++) {
                NSString *str = [array objectAtIndex:i];
                NSArray *tArray = [str componentsSeparatedByString:@"="];
                DLog(@"params = %@",tArray);
                if (tArray.count>1){
                    for (int j = 0; j<tArray.count; j++) {
                        if (j==0){
                            if ([[tArray objectAtIndex:j]isEqualToString:@"id"]){
                                NSString   *idString = [tArray objectAtIndex:j+1];
                                NSLog(@"idString====%@",idString);
                            }
                        }
                    }
                }
                
            }
            
        }
    }
}

是不是,这是什么鬼。。。?

 

完整Url是这样的:http://183.6.151.51:8082/gwapi/news/detail?id=2776&uid=#top

[request URL].query  即为 id=2776&uid=

打印下idString为2776

之所以这样是为了获取id号,重新请求新数据

 

NSURL组成部分详解(原文:http://lizhuang.iteye.com/blog/1973787)

NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/search?id=1"];
NSLog(@"scheme:%@", [url scheme]); //协议 http
NSLog(@"host:%@", [url host]);     //域名 www.baidu.com
NSLog(@"absoluteString:%@", [url absoluteString]); //完整的url字符串 http://www.baidu.com:8080/search?id=1
NSLog(@"relativePath: %@", [url relativePath]); //相对路径 search
NSLog(@"port :%@", [url port]);  // 端口 8080
NSLog(@"path: %@", [url path]);  // 路径 search
NSLog(@"pathComponents:%@", [url pathComponents]); // search
NSLog(@"Query:%@", [url query]);  //参数 id=1
posted @ 2015-09-11 15:01  sixindev  阅读(1401)  评论(0编辑  收藏  举报