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

Xcode/iOS:如何用WhatsApp专属扩展名唤起应用及启用其文档交互API

Hey there! Let's tackle your two questions about using WhatsApp's exclusive file extensions in iOS/Xcode development, step by step:

1. How to directly launch WhatsApp using its exclusive extensions (.wai, .waa, .wam) in Xcode/iOS development?

To make this work, you'll need to configure your project to recognize WhatsApp's unique UTIs (Uniform Type Identifiers) and then use Apple's UIDocumentInteractionController to handle the file launch. Here's the breakdown:

  • Step 1: Register WhatsApp's exclusive UTIs and extensions in Info.plist
    First, tell iOS your app can interact with these file types. Open your project's Info.plist and add the following configurations (you can use the visual editor or edit the source code directly):

    <!-- Document Types: Let your app recognize WhatsApp's file types -->
    <key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeName</key>
            <string>WhatsApp Image</string>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
            <key>LSHandlerRank</key>
            <string>Alternate</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>net.whatsapp.image</string>
            </array>
        </dict>
        <dict>
            <key>CFBundleTypeName</key>
            <string>WhatsApp Audio</string>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
            <key>LSHandlerRank</key>
            <string>Alternate</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>net.whatsapp.audio</string>
            </array>
        </dict>
        <dict>
            <key>CFBundleTypeName</key>
            <string>WhatsApp Video</string>
            <key>CFBundleTypeRole</key>
            <string>Viewer</string>
            <key>LSHandlerRank</key>
            <string>Alternate</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>net.whatsapp.movie</string>
            </array>
        </dict>
    </array>
    
    <!-- Exported UTIs: Declare the exclusive types to the system -->
    <key>UTExportedTypeDeclarations</key>
    <array>
        <dict>
            <key>UTTypeIdentifier</key>
            <string>net.whatsapp.image</string>
            <key>UTTypeDescription</key>
            <string>WhatsApp Exclusive Image</string>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.image</string>
            </array>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.filename-extension</key>
                <array>
                    <string>wai</string>
                </array>
            </dict>
        </dict>
        <dict>
            <key>UTTypeIdentifier</key>
            <string>net.whatsapp.audio</string>
            <key>UTTypeDescription</key>
            <string>WhatsApp Exclusive Audio</string>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.audio</string>
            </array>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.filename-extension</key>
                <array>
                    <string>waa</string>
                </array>
            </dict>
        </dict>
        <dict>
            <key>UTTypeIdentifier</key>
            <string>net.whatsapp.movie</string>
            <key>UTTypeDescription</key>
            <string>WhatsApp Exclusive Video</string>
            <key>UTTypeConformsTo</key>
            <array>
                <string>public.movie</string>
            </array>
            <key>UTTypeTagSpecification</key>
            <dict>
                <key>public.filename-extension</key>
                <array>
                    <string>wam</string>
                </array>
            </dict>
        </dict>
    </array>
    
  • Step 2: Use UIDocumentInteractionController to launch WhatsApp
    Once your project is configured, use this Swift code to trigger WhatsApp with a file that has one of the exclusive extensions (e.g., test.wai stored in your app bundle):

    import UIKit
    
    class YourViewController: UIViewController, UIDocumentInteractionControllerDelegate {
        private var docInteractionController: UIDocumentInteractionController?
    
        func openWhatsAppWithExclusiveFile() {
            // Replace "test" with your file name, and "wai" with the correct extension
            guard let fileURL = Bundle.main.url(forResource: "test", withExtension: "wai") else {
                print("Oops, target file not found in bundle!")
                return
            }
    
            docInteractionController = UIDocumentInteractionController(url: fileURL)
            docInteractionController?.delegate = self
            // Explicitly set the WhatsApp exclusive UTI to ensure correct app matching
            docInteractionController?.uti = "net.whatsapp.image"
            // Present the options menu—this should directly show WhatsApp if configured correctly
            docInteractionController?.presentOptionsMenu(from: view.bounds, in: view, animated: true)
        }
    
        // Required delegate method to handle preview presentation
        func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
            return self
        }
    }
    
2. Can we make WhatsApp's document interaction API function work where only WhatsApp shows up in the app list?

Absolutely! This functionality relies on the system correctly associating WhatsApp's exclusive UTIs and extensions with the app itself. Here's how to ensure it triggers properly:

  • Core Requirement: Match the UTI and Extension Exactly
    WhatsApp's exclusive types are designed to bypass generic public/* associations. For the app list to only show WhatsApp, you need to:

    1. Use the correct file extension: Your target file must end with .wai (image), .waa (audio), or .wam (video)—not standard extensions like .jpg or .mp3. For example, rename a JPG image to my-image.wai.
    2. Explicitly set the exclusive UTI in code: When initializing UIDocumentInteractionController, set the uti property to the corresponding WhatsApp UTI (e.g., net.whatsapp.audio for .waa files) instead of using generic UTIs like public.audio.
  • Troubleshooting Tips

    • If other apps still appear in the list, double-check your Info.plist configuration for typos in UTI strings or extensions.
    • Restart your Xcode project and the simulator/physical device—sometimes iOS needs to refresh its UTI cache to recognize new type associations.
    • Ensure the user has a recent version of WhatsApp installed, as older versions might not support these exclusive extensions.

When the functionality works correctly, the system will immediately recognize the exclusive type as tied to WhatsApp, so the app selection menu will only display WhatsApp. Most issues stem from mismatched UTIs or incorrect file extensions, so those are the first things to verify.


内容的提问来源于stack exchange,提问作者Antoine Nedelec

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:36:47