在iOS7中,通过UIDocumentInteractionController打开iOS邮件应用程序并将其关闭会导致呈现视图控制器的视图被移除。

34

我在我的应用程序中实现了UIDocumentInteractionController来显示打开选项。在iOS8设备上它工作得很好,但是在iOS7上,当我从邮件中打开我的PDF文件时,它会打开邮件composer,当我关闭邮件composer时,也会将一个菜单按钮从我的视图中移除(该按钮添加到窗口)。我花了整整一天时间努力解决这个问题,但是没有找到任何解决方案。在其他选项中打开我的PDF时,没有问题。此问题仅出现在iOS7的邮件composer中。我知道UIDocumentInterfaceController在iOS7上存在问题。我在SO上找到了相同的问题,但这是关于快速查看预览选项的。

以下是我打开选项的代码:

[self.docInteractionController presentOptionsMenuFromRect:self.view.frame
                                                   inView:self.view
                                                 animated:YES];
任何对此的帮助将不胜感激。 提前致谢。

我认为你应该使用self.window而不是self.view。 - IOSDev
@IOSDev,无法从self访问窗口。我还尝试在应用程序窗口上呈现选项菜单。 - Mayank Jain
可以从 self 访问。请看一下。self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; - IOSDev
NSString *textToShare = @"Los!"; NSURL *myWebsite = [NSURL URLWithString:@"http://www.a.com/"]; UIImage *image = [UIImage imageNamed:@"index.jpg"];NSArray *objectsToShare = @[textToShare, myWebsite, image]; UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil]; NSArray *excludeActivities = @[UIActivityTypeAirDrop, ]; activityVC.excludedActivityTypes = excludeActivities; [self presentViewController:activityVC animated:YES completion:nil]; - ashForIos
它是移除菜单按钮还是移除底部模态视图控制器? - Vitalii Gozhenko
@MayankJain,你把邮件应用程序的关闭代码放在哪里了? - Ganpat
4个回答

1
   - (IBAction)previewDocument:(id)sender {
    NSURL *URL = [[NSBundle mainBundle] URLForResource:@"sample" withExtension:@"pdf"];

    if (URL) {
        // Initialize Document Interaction Controller
        self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];

        // Configure Document Interaction Controller
        [self.documentInteractionController setDelegate:self];

        // Preview PDF
        [self.documentInteractionController presentPreviewAnimated:YES];
    }
}

检查一下这个链接,它可以帮助你。 - Kamalkumar.E
检查以下链接获取更多参考资料:http://code.tutsplus.com/tutorials/ios-sdk-previewing-and-opening-documents--mobile-15130 - Kamalkumar.E

0

试试这个。对我来说很好用。

 @IBAction func btnPresentAction(_ sender: UIButton) {

        let fileURL = Bundle.main.path(forResource: "backgroundPerson", ofType: "png")

        let urlI = URL(fileURLWithPath: fileURL!)

        let documentController = UIDocumentInteractionController.init(url: urlI)

        documentController.delegate = self

//        documentController.presentOptionsMenu(from: self.view.frame, in: self.view, animated: true)
        documentController.presentPreview(animated: true)

//        self.present(documentController, animated: true, completion: nil)

    }

    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
        return self
    }

    func documentInteractionControllerViewForPreview(_ controller: UIDocumentInteractionController) -> UIView? {
        return self.view
    }

    func documentInteractionControllerRectForPreview(_ controller: UIDocumentInteractionController) -> CGRect {
        return self.view.frame
    }

0

试试这个,它可能有助于解决你的问题。

NSURL* url = //...Your URL //[NSURL fileURLWithPath:path];
UIDocumentInteractionController* docController = [UIDocumentInteractionController interactionControllerWithURL:url];
docController.delegate = self;
[docController presentPreviewAnimated:YES];

我不想打开预览@angel... 我只想从选项菜单中打开邮件撰写器.... - Mayank Jain

0

你可以检查iOS版本,如果版本号小于8,则像这样在Web浏览器中打开PDF文件

    UIWebView *webview = [[UIWebView alloc] init];
    [self.view addSubview:webview];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"pdfFileName" ofType:@"pdf"];
    NSURL *targetURL = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
    [webview loadRequest:request];`

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接