2014-11-24 147 views
0

如何将“http://www.google.com”这样的URL作为我的AFNetworking POST请求的参数传递给我?我可以尝试使用[statusText.text stringByReplacingOccurrencesOfString:@"/" withString:@"//"]作为URL,但是当我为其他目的检索我发布的数据时,我获得了http:////www.google.com,而这对我来说不会有好处。另外,如果用户使用正斜杠发送参数,则其他文本可能会受此方法的影响。将AFNetworking的URL作为参数传递

编辑

参数实际上是一个文本。有点像推特或FB状态我在这里工作。因此用户可以输入一堆单词和句子,并且可以将URL作为字符串。当存在URL时,我在Web服务上收到错误500。但是,当我使它http:////www.google.com错误不会发生。因此,例如,如果用户发送带有参数"Hello! Please check this website http://www.google.com"的请求,我需要将它传递给我的请求。

+0

使用hasPrefix所以你的问题其实是“我怎么解析从文本中的网址?”。 ['NSDataDetector'](http://nshipster.com/nsdatadetector/)怎么样? – mattt 2014-11-25 21:13:12

回答

0

查一下NSString

NSString *strURL = @"http://www.google.com"; 
//Check if string contains http 
if([strURL hasPrefix:@"http"]) 
{ 
    //Check if format is correct if not then make it correct 
    if(![strURL hasPrefix:@"http://"]) 
    { 
     strURL = [strURL stringByReplacingOccurrencesOfString:@"http:/" withString:@"http://"]; 
    } 
} 
else //doesnot contain http 
{ 
    //Add http to string 
    NSString *strTemp = [NSString stringWithString:strURL]; 
    strURL = [NSString stringWithFormat:@"http://%@",strTemp]; 
} 

NSLog(@"%@",strURL); 
+0

感谢您的快速回答。但是我正在处理一个字符串,它不仅包含一个URL,还包含一堆其他字词。请看我的编辑。 – 2014-11-24 07:42:22