I have reviewed a bunch of posts here, numerous online tutorials/sample code and I'm stumped. In my app I have no problem displaying the UIActivityController natively provided by iOS7 with the sharing options appropriate to my app (AirDrop and mail).

The specific problem I'm having is getting my saved document attached to the email message when the user selects the option to share via mail. The message body is being set to the text, but the attachment is MIA. The relevant code is:

// Generate the XML file to be shared for the currently displayed record... NSURL *url = [self createShareFile];

UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[@"Data shared from my app.", url] applicationActivities:nil];

// Filter out the sharing methods we're not interested in....
controller.excludedActivityTypes = @[UIActivityTypePostToTwitter, UIActivityTypePostToFacebook,
                                UIActivityTypePostToWeibo,
                                UIActivityTypeMessage,
                                UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
                                UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,
                                UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,
                                UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo];

// Now display the sharing view controller.
[self presentViewController:controller animated:YES completion:nil];

What am I missing? My file is being properly created, the contents is correct and the NSURL object contains the proper path to the file.

Thanks!

有帮助吗?

解决方案

Problem solved.....

The code posted in my original post is 100% accurate. The issue ended up being in the way I was constructing the NSURL being returned in my createShareFile method:

Incorrect (original way):

return [NSURL URLWithString:[docFile stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

Correct way:

return [NSURL fileURLWithPath:docFile];

As soon as I fixed that, it started working, even with my custom file type.

其他提示

I had a similar problem where the mail app was the only one I couldn't add a pdf to. Here is my code in Swift as well as handling iPad popup.

var filesToShare = [Any]()
filesToShare.append(self.myUrl)

let activityViewController = UIActivityViewController(activityItems: filesToShare as [Any], applicationActivities: nil)
present(activityViewController, animated: true)

// for iPad -> where to present on screen
if let popOver = activityViewController.popoverPresentationController {
    //action button being my top left icon
    popOver.barButtonItem = self.actionButton
}

My issue was as well handling the URL differently

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top