You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在UIActivityViewController弹窗中显示文件大小与类型

Fixing File Type & Size Display in UIActivityViewController

Hey there! The reason your PDF's type and size aren't showing up in the UIActivityViewController is down to how you're providing the file data. Let's break down the solutions depending on whether your file is local or remote:

Case 1: Local File

If the PDF is already saved locally on the device, make sure you're passing a proper file:// URL (not a string or remote URL). The system will automatically detect the file type and size from the local file system metadata.

Here's how to adjust your code:

// Replace with your local file's actual path
let localPDFURL = URL(fileURLWithPath: "/path/to/your/local/document.pdf")

// No need to pass a separate filename string—use the URL's lastPathComponent for the default name
// To set a custom filename, rename the local file first or define it in the URL path
let activityViewController = UIActivityViewController(
    activityItems: [localPDFURL],
    applicationActivities: nil
)

present(activityViewController, animated: true, completion: nil)

This will show the PDF icon, file type label, and size directly in the activity sheet.

Case 2: Remote File

For remote URLs (like a web-hosted PDF), the system can't fetch file metadata without downloading the file first. The most reliable fix is to download the file to the device's temporary directory, then pass the local file URL to UIActivityViewController.

Here's a complete example:

let remotePDFURL = URL(string: "https://example.com/your-remote-document.pdf")!
let tempDirectory = FileManager.default.temporaryDirectory
// Set your custom filename here
let localPDFURL = tempDirectory.appendingPathComponent("MyCustomPDF.pdf")

// Download the remote file first
URLSession.shared.downloadTask(with: remotePDFURL) { tempDownloadURL, response, error in
    guard let tempDownloadURL = tempDownloadURL, error == nil else {
        print("Download failed: \(error?.localizedDescription ?? "Unknown error")")
        return
    }

    do {
        // Move the downloaded file to our temporary path with the custom filename
        try FileManager.default.moveItem(at: tempDownloadURL, to: localPDFURL)
        
        // Present the activity sheet on the main thread
        DispatchQueue.main.async {
            let activityViewController = UIActivityViewController(
                activityItems: [localPDFURL],
                applicationActivities: nil
            )
            self.present(activityViewController, animated: true, completion: nil)
        }
    } catch {
        print("Failed to move file: \(error.localizedDescription)")
    }
}.resume()

Once the file is local, the UIActivityViewController will automatically display the PDF icon, file size, and type just like with any native local file.

Why Your Original Code Didn't Work

Passing a remote URL + a string filename doesn't give the system enough info to retrieve the file's metadata. The string is treated as a separate activity item (like a text snippet), not as metadata for the URL. By using a local file URL, you let the system access all the file's native metadata directly.

内容的提问来源于stack exchange,提问作者P. van der Laan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:10:19