2010-02-13 63 views
1

我在應用程序上使用NSURLConnection。 要建立HTTP連接,我用下面的代碼:如何從字符串中獲取有效的URL?

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL 
             URLWithString:@"http://www.mydomain.com/path/[email protected]"]      cachePolicy:NSURLRequestUseProtocolCachePolicy 
            timeoutInterval:45]; 
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request 
                   delegate:self]; 

但它拋出一些異常的控制檯上要求:

Connection failed! Error - bad URL (null) 

所以,我已經把URL上Safari瀏覽器的地址欄。 Safari的固定網址爲:

http://www.mydomain.com/path/sample.php?id=%1B%[email protected] 

如何更正Safari之類的URL?

是否有API方法與此問題有關?

在此先感謝。

回答

3

在將NSString傳遞給NSURL之前,在NSString上使用-stringByAddingPercentEscapesUsingEncoding:方法。你應該能夠通過NSASCIIStringEncoding爲你正在做的事情。

+0

很酷。非常感謝。工作正常 – Ferdinand 2010-02-13 03:36:33