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:
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'sInfo.plistand 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.waistored 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 } }
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 genericpublic/*associations. For the app list to only show WhatsApp, you need to:- Use the correct file extension: Your target file must end with
.wai(image),.waa(audio), or.wam(video)—not standard extensions like.jpgor.mp3. For example, rename a JPG image tomy-image.wai. - Explicitly set the exclusive UTI in code: When initializing
UIDocumentInteractionController, set theutiproperty to the corresponding WhatsApp UTI (e.g.,net.whatsapp.audiofor.waafiles) instead of using generic UTIs likepublic.audio.
- Use the correct file extension: Your target file must end with
Troubleshooting Tips
- If other apps still appear in the list, double-check your
Info.plistconfiguration 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.
- If other apps still appear in the list, double-check your
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

