0

アプリに問題があります。

私は ASIHttpRequest を使用して、このコードで pdf ファイルをダウンロードしています:

- (void) downloadPdf{
AppDelegate *appDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate;
if (currentDownload) {

    [currentDownload clearDelegatesAndCancel];
    currentDownload = nil;
    totalWorker = 0;
    [workers release]; workers = nil;
    progressView.progress = 0.0;

} else {

    [self removeFile];

    appDelegate.alertGlobal   = [[UIAlertView alloc] initWithTitle: @"Download file..."
                                                 message: @""
                                                delegate: self
                                       cancelButtonTitle: @"Annul"
                                       otherButtonTitles: nil];

    progressView = [[UIProgressView alloc] initWithFrame:
                    CGRectMake(30.0f, 43.0f, 225.0f, 10.0f)];
    [appDelegate.alertGlobal addSubview:progressView];
    [progressView setProgressViewStyle: UIProgressViewStyleBar];

    [appDelegate.alertGlobal show];

    NSString *tmp = @"http://192.168.0.13:8888/file.pdf";

    currentDownload = [[HappyDownload alloc] initWithURL:[NSURL URLWithString:tmp]];
    currentDownload.numberOfWorkers = totalWorker;
    currentDownload.totalChunk = 1;
    currentDownload.delegate = self;
    currentDownload.downloadProgressDelegate = self;
    currentDownload.allowResumeForFileDownloads = YES;

    documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

    [currentDownload setDownloadDestinationPath:[documentsDirectory stringByAppendingPathComponent:@"file.pdf"]];

    [workers removeAllObjects];

    [currentDownload startAsynchronous];

    start = [NSDate new];
}
}



- (void) removeFile{

    NSString *pathTemp = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];
    //NSString *path = [pathTemp stringByAppendingPathComponent:@"file.pdf"];

    NSFileManager* fm = [[[NSFileManager alloc] init] autorelease];
    NSDirectoryEnumerator* en = [fm enumeratorAtPath:pathTemp];
    NSError* err = nil;
    BOOL res;

    NSString* file;
    while (file = [en nextObject]) {
        res = [fm removeItemAtPath:[pathTemp stringByAppendingPathComponent:file] error:&err];
        if (!res && err) {
            NSLog(@"error: %@", err);
        }
    }

}



-(void)requestFinished:(ASIHTTPRequest *)request{
    NSDateComponents *comps = [[NSCalendar currentCalendar] components:NSSecondCalendarUnit
                                                              fromDate:start
                                                                toDate:[[NSDate new] autorelease]
                                                               options:0];
    int duration  = [comps second];

    NSLog(@"request finished, duration:%d", duration);

    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate.downloaded = TRUE;

    [currentDownload clearDelegatesAndCancel];
    currentDownload = nil;
    totalWorker = 0;
    [workers release]; workers = nil;
    progressView.progress = 0.0;

    [appDelegate.alertGlobal dismissWithClickedButtonIndex:0 animated:YES];
    [appDelegate.alertGlobal release];

    downloadedFirstTime = TRUE;

    NSError *error;
    NSString *pathTemp = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSLog(@"Documents directory: %@", [fileManager contentsOfDirectoryAtPath:pathTemp error:&error]);

    [self openFile];

}

- (void) requestFailed:(ASIHTTPRequest *)request{
    AppDelegate *appDelegate = (AppDelegate*) [UIApplication sharedApplication].delegate;
    [currentDownload clearDelegatesAndCancel];
    currentDownload = nil;
    totalWorker = 0;
    [workers release]; workers = nil;
    progressView.progress = 0.0;

    [appDelegate.alertGlobal dismissWithClickedButtonIndex:0 animated:YES];
    [appDelegate.alertGlobal release];

    NSLog(@"request failed");
    NSLog(@"Error %@", [request error]);
    int statusCode = [request responseStatusCode];
    NSString *statusMessage = [request responseStatusMessage];
    NSLog(@"statuscode:%d", statusCode);
    NSLog(@"statusmessage:%@", statusMessage);
}

このようにして、PDFファイルを正しくダウンロードし、このコントロールでこの方法で開きます:

https://github.com/vfr/Reader

iPhoneでpdfファイルを開くためのクラシックコントローラーと私はこの方法を使用します:

- (void) openFile{

    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;

    NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files)

    NSString *pathTemp = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];
    NSString *path = [pathTemp stringByAppendingPathComponent:@"file.pdf"];

    assert(path != nil); // Path to last PDF file

    ReaderDocument *document = [ReaderDocument withDocumentFilePath:path password:phrase];

    if (document != nil) // Must have a valid ReaderDocument object in order to proceed
    {
        readerViewController = [[ReaderViewController alloc] initWithReaderDocument:document];

        readerViewController.delegate = self; // Set the ReaderViewController delegate to self

        //mainWindow.rootViewController = readerViewController; // Set the root view controller
    }

    [self presentModalViewController:readerViewController animated:YES];

    [readerViewController release];
}

最初は問題なく動作し、最初のファイルpdfを問題なく開きますが、サーバーにアップロードする2番目の別のpdfをダウンロードすると、大きな問題が発生します...その中に2つのpdfファイルが表示されます。他ですが、問題がASIHttpRequestなのかreaderViewControllerなのかわかりません。ご覧のとおり、HomeDirectory 内のファイルを削除しましたが、問題がわかりません。助けてもらえますか? ありがとう

4

1 に答える 1

0

remove メソッドで、パス (ファイル名を含む) が正しいことを確認しましたか? また、ファイルを削除した場所でもう1つ試してから、そのファイルの存在を確認してください。

それはいくつかのアイデアを与えるかもしれません。

于 2012-11-27T11:35:54.003 に答える