J'essaie de faire une application qui télécharge un fichier pdf à partir d'un serveur en ligne. Ce que j'ai fait, c'est que j'ai créé 3 boutons qui ont 3 URL différentes et l'enregistrer dans le bac à sable de l'application et l'ouvrir dans iBooks.Objectif C: Téléchargements multiples
Mais il bloque l'application et j'ai cette erreur en disant ...
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter' ***
Voici mon code: Sur CLICK:
if (sender.tag == 1) {
pdfTag = 1;
pdfSource1 = @"http://myweb.com/folder/folderagain/file1.pdf";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:pdfSource1]];
NSURLConnection *conn = [[NSURLConnection alloc] init];
(void)[conn initWithRequest:request delegate:self];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
dataSource1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:pdfSource1]];
dispatch_sync(dispatch_get_main_queue(), ^{
});
pdfSourcePath1 = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"myPDF1.pdf"];
NSLog(@"PDF path: %@",pdfSourcePath1);
[dataSource1 writeToFile:pdfSourcePath1 atomically:YES];
});
} else if (sender.tag == 2) {
pdfTag = 2;
pdfSource2 = @"http://myweb.com/folder/folderagain/file2.pdf";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:pdfSource2]];
NSURLConnection *conn = [[NSURLConnection alloc] init];
(void)[conn initWithRequest:request delegate:self];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
dataSource2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:pdfSource2]];
dispatch_sync(dispatch_get_main_queue(), ^{
});
pdfSourcePath2 = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"myPDF2.pdf"];
NSLog(@"PDF path: %@",pdfSourcePath2);
[dataSource2 writeToFile:pdfSourcePath2 atomically:YES];
});
} else if (sender.tag == 3) {
pdfTag = 3;
pdfSource3 = @"http://myweb.com/folder/folderagain/file3.pdf";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:pdfSource3]];
NSURLConnection *conn = [[NSURLConnection alloc] init];
(void)[conn initWithRequest:request delegate:self];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
dataSource3 = [NSData dataWithContentsOfURL:[NSURL URLWithString:pdfSource3]];
dispatch_sync(dispatch_get_main_queue(), ^{
});
pdfSourcePath3 = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"myPDF3.pdf"];
NSLog(@"PDF path: %@",pdfSourcePath3);
[dataSource3 writeToFile:pdfSourcePath3 atomically:YES];
});
}
Pour ouvrir le fichier:
if (pdfTag == 1) {
NSURL *url = [NSURL fileURLWithPath:pdfSourcePath1];
docController1 = [UIDocumentInteractionController interactionControllerWithURL:url];
docController1.delegate = self;
[docController1 presentOpenInMenuFromRect:btn1.frame inView:self.view animated:YES];
} else if (pdfTag == 2) {
NSURL *url = [NSURL fileURLWithPath:pdfSourcePath2];
docController2 = [UIDocumentInteractionController interactionControllerWithURL:url];
docController2.delegate = self;
[docController2 presentOpenInMenuFromRect:btn2.frame inView:self.view animated:YES];
} else if (pdfTag == 3) {
NSURL *url = [NSURL fileURLWithPath:pdfSourcePath3];
docController2 = [UIDocumentInteractionController interactionControllerWithURL:url];
docController2.delegate = self;
[docController2 presentOpenInMenuFromRect:btn3.frame inView:self.view animated:YES];
}
J'espère que je pourrais trouver la réponse.
Selon moi 'pdfSourcePath1, pdfSourcePath2, pdfSourcePath3' sont' nil'. La méthode de fichier ouvert est appelée avant que le chemin de la source pdf ne soit défini dans le code de bloc. Essayez de placer des points de débogage. –
L'erreur se produit-elle en essayant d'ouvrir le fichier ou en téléchargement? Pouvez-vous vous connecter le pdfSourcePath1,2,3 avant de construire le NSURL pour l'ouverture. Enfin, votre utilisation de GCD directement pour faire les demandes est étrange et difficile à lire. Recherchez NSURLConnection sendAsynchronousRequest: .... – danh
@Deepesh mon code pour le fichier d'ouverture est à l'intérieur connectionDidFinishLoading: (NSURLConnection *) connexion –