2014-11-24 149 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