2016-04-18 4 views
0

J'ai essayé de faire en sorte que cela fonctionne sans avoir à charger d'abord la vue Web et à obtenir l'absoluteString pour pouvoir télécharger l'URL. J'ai essayé de nombreuses solutions shortURL et elles ne chargent jamais complètement l'URL. Ils me donnent toujours l'URL qui n'est pas l'url finale et ne le fait pas l'URL PDF. Toute aide serait incroyable. J'essaie de télécharger le PDF lorsque l'application s'ouvre pour la première fois ou quand elle vérifie les mises à jour, mais à ce moment-là, elle reçoit simplement l'URL courte et je dois attendre que la vue Web soit appelée pour obtenir l'URL complète. le PDF une tête de temps.Objective-C Essayer de télécharger un fichier PDF à partir d'une courte URL

Répondre

0

Vous téléchargez le PDF, comme si vous téléchargiez un autre fichier.

Jetez un oeil à NSURLDownload

- (void)startDownloadingURL:sender 
{ 
    // Create the request. 
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/index.html"] 
              cachePolicy:NSURLRequestUseProtocolCachePolicy 
              timeoutInterval:60.0]; 

    // Create the download with the request and start loading the data. 
NSURLDownload *theDownload = [[NSURLDownload alloc] initWithRequest:theRequest delegate:self]; 
    if (!theDownload) { 
     // Inform the user that the download failed. 
    } 
} 

- (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename 
{ 
    NSString *destinationFilename; 
    NSString *homeDirectory = NSHomeDirectory(); 

    destinationFilename = [[homeDirectory stringByAppendingPathComponent:@"Desktop"] 
     stringByAppendingPathComponent:filename]; 
    [download setDestination:destinationFilename allowOverwrite:NO]; 
} 


- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error 
{ 
    // Dispose of any references to the download object 
    // that your app might keep. 
    ... 

    // Inform the user. 
    NSLog(@"Download failed! Error - %@ %@", 
      [error localizedDescription], 
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 
} 

- (void)downloadDidFinish:(NSURLDownload *)download 
{ 
    // Dispose of any references to the download object 
    // that your app might keep. 
    ... 

    // Do something with the data. 
    NSLog(@"%@",@"downloadDidFinish"); 
} 
+0

J'ai essayé, il finit toujours par une erreur car il ne termine pas rediriger l'URL pour obtenir l'URL au format PDF. La façon dont je le fais maintenant, est-ce que j'attends le chargement de la vue web pour qu'il termine la redirection et fasse webView.request.URL.absoluteString. La chose est que le PDF télécharge maintenant quand la vue charge le PDF. Je veux que le PDF soit téléchargé lorsque l'application s'ouvre. J'ai donc besoin de l'URL complète de la redirection lorsque l'application s'ouvre. – GunplaGamer

0

S'il vous plaît vérifier AppleDocs sur la gestion redirect demande.

0

essayez d'utiliser afnetworking pour télécharger le fichier pdf pour le serveur

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://letuscsolutions.files.wordpress.com/2015/07/five-point-someone-chetan-bhagat_ebook.pdf"]]; 
    [request setTimeoutInterval:120]; 
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
    NSString *pdfName = @"2.zip"; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:pdfName]; 
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO]; 

    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { 
    }; 

     dispatch_async(dispatch_get_main_queue(), ^{ 

      NSLog(@"Download = %f", (float)totalBytesRead/totalBytesExpectedToRead); 
      NSLog(@"total bytesread%f",(float)totalBytesRead); 
      NSLog(@"total bytesexpected%lld",totalBytesExpectedToRead); 

     }); 


    }]; 
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"Successfully downloaded file to %@", path); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 
    }]; 

    [operation start];