我正在使用以下代码检查网址的应用程序。
-(void)exists { NSString *strImg = @"some url"; NSURL *aImgUrl = [NSURL URLWithString:strImg]; NSMutableURLRequest *imgRequest = [NSMutableURLRequest requestWithURL:strImg]; [imgRequest setHTTPMethod:@"HEAD"]; imgConnection = [NSURLConnection connectionWithRequest:imgRequest delegate:self]; }
这就是我得到回应时所做的。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { if ([(NSHTTPURLResponse *)response statusCode] == 200) { // url exists appDel.isExists = TRUE; temp = FALSE; [self loadData]; NSLog(@"%ld",(long)[(NSHTTPURLResponse *)response statusCode]); } else if([(NSHTTPURLResponse*)response statusCode] == 404) { //url does not exists appDel.isExists = FALSE; temp = TRUE; [self loadData]; NSLog(@"%ld",(long)[(NSHTTPURLResponse *)response statusCode]); } }
这是我的装载数据代码
-(void)loadData { result = FALSE; isReqSent = FALSE; do { if (appDel.isExists == TRUE) { NSURL *aTxtUrl = [NSURL URLWithString:apiText]; NSURL *aImgUrl = [NSURL URLWithString:apiImage]; NSURLRequest *imgRequest = [NSURLRequest requestWithURL:aImgUrl]; NSURLRequest *txtRequest = [NSURLRequest requestWithURL:aTxtUrl]; [NSURLConnection sendAsynchronousRequest:txtRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (data) { NSString *strTxt = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; [lblTime setText:strTxt]; [policyImgWebView loadRequest:imgRequest]; result = TRUE; } }]; } if (result) { appDel.isExists = TRUE; } else { if(!isReqSent) { NSString *soapMessage = @"my soap message"; NSString *soapAction = @"my soap url"; [objWebServiceParser xmlParsing:soapMessage :soapAction :Number]; isReqSent = TRUE; } } if (temp) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ [self backgroundProcess]; }); } } while (result == FALSE);
这是我的背景流程
-(void)backgroundProcess { NSString *strImg = @"some url"; NSURL *aImgUrl = [NSURL URLWithString:apiImage]; NSMutableURLRequest *imgRequest = [NSMutableURLRequest requestWithURL:aImgUrl]; [imgRequest setHTTPMethod:@"HEAD"]; imgConnection = [NSURLConnection connectionWithRequest:imgRequest delegate:self]; }
我不知道哪里做错了,谁能指引我?
我希望后台进程一直运行,直到它收到数据,当数据接收到appdel时。isExists变为现实
我试过GCD
它与NSURLSessionDownloadTask一起工作,但仍在寻找更好的选项。
不要在后台调用NSURLConnection
。AsNSURLConnection
将自动工作。由于您不发送异步请求,它将在后台工作,并且不会挂起主线程。在加载数据中删除分派。你有:
if (temp)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[self backgroundProcess];
});
}
成功:
if (temp)
{
[self backgroundProcess];
}
您不需要使用调度。
要从服务器加载UIImageView
中的图像,可以使用以下框架。
SDWebImage
您将获得completionBlock
,progressBlock
样本:
如果imageURL
有效(这是您的情况),则只需加载图像,这与此代码一样简单
UIImageView *imageView;
[imageView setImageWithURL:[NSURL URLWithString:YOUR_URL]];
它也有丰富的方法,如下所示
UIImageView *imageView;
[imageView setImageWithURL:[NSURL URLWithString:YOUR_URL]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
{
// in "image" you'll get the downloaded image from the URL
... completion code here ...
}];