Swift/Xcode开发:分享PDF时分享按钮无可选应用求助
Hey Jacob, let's figure out why your share sheet is only showing that empty "[...] more" option instead of the apps you expect. From the code snippet you shared, there are a few common pitfalls that could be causing this—let's walk through fixing them step by step.
First, Fix the PDF Path Retrieval
The first red flag I see is your ofType parameter: you're passing ".pdf" (with a leading dot), but Bundle.main.path(forResource:ofType:) expects the file extension without the dot. That might be causing your path variable to be nil, and when you force-unwrap it with path!, you're either crashing silently or passing an invalid URL to the share sheet.
Update that line to:
let path = Bundle.main.path(forResource: "Brewolution A Bunnys Tale - Påske 2012", ofType: "pdf")
Ensure Your PDF is Actually in the Bundle
Double-check that your PDF file is properly added to your project:
- Go to your target's Build Phases > Copy Bundle Resources
- Make sure "Brewolution A Bunnys Tale - Påske 2012.pdf" is listed here. If not, add it by clicking the "+" button and selecting the file.
Fix the Share Sheet Implementation
Assuming you have a share button connected to an action, here's a robust way to present the share sheet with your PDF:
First, add the MobileCoreServices import at the top of your file (we'll need it for PDF type identifiers):
import UIKit import WebKit import MobileCoreServices
Then implement your share action:
@IBAction func sharePDFButtonTapped(_ sender: UIButton) { // Safely retrieve the PDF file URL guard let pdfFileName = "Brewolution A Bunnys Tale - Påske 2012", let fileURL = Bundle.main.url(forResource: pdfFileName, withExtension: "pdf") else { print("Error: Could not locate PDF file in bundle") return } // Option 1: Share the file URL directly (simplest for most cases) let activityItems: [Any] = [fileURL] // Option 2: If Option 1 doesn't work, use an NSItemProvider for better type recognition // let pdfData = try? Data(contentsOf: fileURL) // let itemProvider = NSItemProvider(item: pdfData as NSData, typeIdentifier: kUTTypePDF as String) // let activityItems: [Any] = [itemProvider] // Create and present the activity view controller let activityVC = UIActivityViewController(activityItems: activityItems, applicationActivities: nil) // Critical for iPad: Set the popover source so the sheet displays correctly if let popoverController = activityVC.popoverPresentationController { popoverController.sourceView = sender popoverController.sourceRect = sender.bounds } present(activityVC, animated: true) }
Why This Works
- Using
Bundle.main.url(forResource:withExtension:)is safer thanpathbecause it returns aURLdirectly, avoiding string-to-URL conversion issues. - Sharing the file URL (or a properly typed
NSItemProvider) tells the system exactly what kind of content you're sharing, so it can show all relevant apps (Mail, Messages, Files, etc.). - The popover configuration ensures the share sheet behaves correctly on iPad (a common oversight that can cause unexpected display issues).
Quick Additional Checks
- If you're still seeing no apps, test with a different PDF file (with a simpler name, no spaces or special characters) to rule out filename encoding issues.
- Make sure you're not accidentally sharing the WKWebView's request URL instead of the actual PDF file URL—sometimes web views modify URLs internally, which can break sharing.
Give these changes a try, and your share sheet should start showing all the appropriate apps again!
内容的提问来源于stack exchange,提问作者Jacob Smede

