我正在使用调度队列从服务器下载PDF文件,并将当前视图加载为PDF预览,当有效的PDF网址可用时,该文件正常工作。
但是当一个损坏的URL链接来自服务器时,我调用了一个警告视图来向用户显示一条错误消息。 虽然这个警报POP多次上升..
有人可以帮助我从这个循环中返回。
- (void)viewPdf:(CDVInvokedUrlCommand*)command
{
if (command.arguments[0] == (id)[NSNull null])
{
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR] callbackId:command.callbackId];
return;
}
NSString *requestIdStr;
NSDictionary* options = [[NSDictionary alloc]init];
if ([command.arguments count] > 0) {
options = [command argumentAtIndex:0];
requestIdStr = [options objectForKey:@"requestId"];
}
NSString *url = requestIdStr;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,(unsigned long)NULL), ^(void)
{
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"PDF.pdf"]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"GET"];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSLog(@"Error... %@",error);//Console message shown below
NSLog(@"pdf downloaded, opening...");
NSData *pdf = data;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)pdf);
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(provider);
if (document == nil) {
// error
NSLog(@"%@",@"error PDF broken link");
//*********** This alert Pops up multiple times ************//
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"" message:@"Error while opening PDF" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
// return ;// I have used this "return " but no use
//Or am I calling this alertview view at wrong place
//*************************************** //
}
else
{
NSURL *localURL = [NSURL fileURLWithPath:file];
[pdf writeToFile:file options:NSDataWritingAtomic error:&error];
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:localURL];
[self.documentInteractionController setDelegate:self];
[self.documentInteractionController presentPreviewAnimated:NO];
}
CGDataProviderRelease(provider);
CGPDFDocumentRelease(document);
}];
});
[self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId];
}
控制台消息下载无效网址时
错误......错误域= NSURLErrorDomain代码= -1007“HTTP重定向太多”UserInfo = 0x7aed5a80 {NSErrorFailingURLStringKey = http://vpcsapp.com/upload/media/details/image/2/1?1439449808478,NSErrorFailingURLKey = http://vpcsapp.com/upload/media/details/image/2/1?1439449808478,NSLocalizedDescription = HTTP重定向太多,NSUnderlyingError = 0x7ae8ab80“HTTP重定向太多”} 找不到PDF标题:找不到`%PDF'。
任何帮助表示赞赏!谢谢大家