【发布时间】:2014-06-27 12:01:27
【问题描述】:
我使用 NSURLConnection 在与主线程分离的线程中从 Internet 获取一些数据: 我把它放在我的 JSONViewController.h 中:
#import <UIKit/UIKit.h>
@interface JSONViewController : UIViewController <NSURLConnectionDelegate> {
BOOL firstTime;
NSMutableData *_responseData;
}
@end
我使用此代码在 JSONViewController.m 中启动连接:
NSURLRequest *request;
if (self.jsonItem == nil) {
request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MY_URL,@"testvalue"]]];
}else {
request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MY_URL,(NSString *)self.jsonItem]]];
}
NSLog(@"json Item = %@",self.jsonItem);
// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
我也实现了那些与 NSURLConnection 协议相关的功能:
#pragma mark NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
_responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {}
一切正常。 问题是:我得到结果并且连接应该完成后,为什么在导航栏上方的运营商字段附近仍然看到这个小指示器?我应该手动停止连接吗?
【问题讨论】:
标签: ios iphone objective-c nsurlconnection