這是一個相當stringWithFormat
消息!首先要檢查的是您獲得的URL與您的期望相符。
一旦你得到了一個URL請求對象,並考慮使用普通NSURLRequest
,它應該是更快,它看起來並不像你打算重用這個對象:
// already autoreleased
NSURLRequest *request = [NSURLRequest requestWithURL:url];
然後你需要實際提出請求。這裏有兩種方法。如果您希望將請求保存到文件,您將使用NSURLDownload
。看起來您正在尋求向某種類型的電子郵件服務器發送GET
請求,因此您可能需要其他方法:NSURLConnection
。
NSURLConnection
主要用於異步請求。您提供了一些委託,並且您的NSURLConnection將使用這些方法通知您何時完成通信;是否有錯誤;等等。
將屬性添加到您的視圖控制器類的連接,以及NSMutableData
屬性以及。
// initialize our storage for the file
self.downloadData = [NSMutableData dataWithLength:1024];
// create and start the connection
self.urlConnection = [NSURLConnection connectionWithRequest:request delegate:self];
if(nil == self.urlConnection) {
NSLog(@"Couldn't create connection to url %@", url);
}
在你的代碼的某個地方,可能是你的當前視圖控制器,您需要同時實現這些方法:
-(void) connection:(NSURLConnection*)connection didReceiveData:(NSData*)data {
// if you have more than one NSURLConnection in this class, test against the
// connection parameter
[downloadData appendData:data];
}
-(void) connectionDidFinishLoading:(NSURLConnection*)connection {
// download completed successfully, we can do what we like with the downloadData object now
// ...
}
-(void) connection:(NSURLConnection*)connection didFailWithError:(NSError*)error {
// handle failure with the grace of Audrey Hepburn. probably log something, too
}
(即當前類也是你代表假設),您將開始您的網絡連接
可能想通過使用stringWithFormat清理URLWithString參數 – 2010-08-14 21:21:25