Javascript 如何从调度队列循环返回,该循环连续4次向我发出错误警报?
我正在使用dispatch queue从服务器下载PDF文件,并在当前视图中加载为PDF预览,当有效的PDF url可用时,该预览工作正常 但当一个断开的URL链接来自服务器时,我调用了一个警报视图来向用户显示一条错误消息。 而此警报会弹出多次 有人能帮我从这个循环中返回吗Javascript 如何从调度队列循环返回,该循环连续4次向我发出错误警报?,javascript,ios,objective-c,phonegap-plugins,Javascript,Ios,Objective C,Phonegap Plugins,我正在使用dispatch queue从服务器下载PDF文件,并在当前视图中加载为PDF预览,当有效的PDF url可用时,该预览工作正常 但当一个断开的URL链接来自服务器时,我调用了一个警报视图来向用户显示一条错误消息。 而此警报会弹出多次 有人能帮我从这个循环中返回吗 - (void)viewPdf:(CDVInvokedUrlCommand*)command { if (command.arguments[0] == (id)[NSNull null])
- (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];
}
下载无效url时的控制台消息:
错误。。。。。。Error Domain=NSURERRORDOMAIN Code=-1007“太多HTTP重定向”UserInfo=0x7aed5a80{NSERRORFAILINGURSTRINGKEY=,NSERRORFAILINGURKEY=,NSLocalizedDescription=太多HTTP重定向,NSUnderlyingError=0x7ae8ab80“太多HTTP重定向”}
找不到PDF标题:找不到“%PDF”
感谢您的帮助!谢谢大家尝试在主线程上显示警报并检查错误变量。