如何獲取Internet上的文件內容?例如,我想讓NSString獲得一個來自http://news.tut.by/rss/index.rss的RSS源內容。我該怎麼做?我只想使用iOS SDK類:NSURLConnection,NSURLRequest等。 P.S.我看到這樣的類:如何獲取互聯網上的文件內容?
#import <Foundation/Foundation.h>
@interface RssDownloader : NSObject {
NSURLConnection * conn;
NSMutableData * receivedData;
}
@property(nonatomic, retain) NSURLConnection * conn;
@property(nonatomic, retain) NSMutableData * receivedData;
- (void)downloadUrlContent : (NSURL *)url;
@end
@implementation RssDownloader
@synthesize receivedData, conn;
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[receivedData release];
receivedData = nil;
}
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receivedData appendData:data];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[connection release];
[receivedData release];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
[receivedData release];
}
- (void) downloadUrlContent:(NSURL *)url {
NSURLRequest * urlRequest = [[NSURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
conn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];
if (conn) {
receivedData = [[NSMutableData data] retain];
}
}
- (void)dealloc {
[conn release];
[receivedData release];
}
@end
但它不起作用。
定義 「但它不工作」 – KevinDTimm 2011-01-31 15:22:21