1

我很难调试这个错误。单击 Adob​​e 或 Nook 作为所选应用程序以打开下载的 PDF 后,我收到 EXC_BAD_ACCESS 错误。我已经使用了调试器,它在我编写的方法之外的某个地方中断了。这是有问题的代码。

        //download protocol
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *storedEmail = [defaults stringForKey:@"email"];
    NSString *storedPassword = [defaults stringForKey:@"password"];
    NSString *projectID = self.study.ProjectID;
    NSString *urlString = [NSString stringWithFormat:@"http://service.pharmatech.com/Document/projectprotocol/%@/%@/%@", storedEmail, storedPassword, projectID];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [NSData dataWithContentsOfURL:url];
    if (data) {
        NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString  *documentsDirectory = [paths objectAtIndex:0];

        NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, [NSString stringWithFormat:@"Protocol_%@.pdf", projectID]];
        [data writeToFile:filePath atomically:YES];

        UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
        docController.delegate = self;
        docController.UTI = @"com.adobe.pdf";
        self.navigationController.toolbarHidden = NO;
        BOOL isValid = [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
        NSLog([NSString stringWithFormat:@"DISPLAYED? %d", isValid]);

        NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];
        for (int count = 0; count < (int)[directoryContent count]; count++) {
            NSLog(@"File %d: %@", (count + 1), [directoryContent objectAtIndex:count]);
        }

        [self.navigationController setToolbarHidden:YES animated:YES];

弹出窗口显示能够显示 pdf 的应用程序。在我单击一个之后,会发生错误访问错误,但直到离开调用它的方法之后,这只是一个按钮单击操作。

如果我在不带调试器的情况下运行应用程序,则只会关闭我的应用程序并且什么也不打开。任何人都有任何提示。我对这个 ios 东西还是很陌生。

编辑:对于那些稍后访问并且不阅读评论的人,答案是 UIDocumentInteractionController 必须是实例变量,而不是本地变量。

4

1 回答 1

2

“docController”似乎是一个问题,是一个局部变量,我认为必须是一个实例变量。

改变

UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];

为了

docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];

并在您的 .h 中声明

UIDocumentInteractionController *docController;
于 2013-05-22T17:22:03.560 回答