这是一个相当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