如何在UNNotificationContentExtension中执行打印语句?
I’ve run into this exact frustration before—when your extension fires up while the main app’s still connected to Xcode, the radio silence in the console makes debugging feel like shooting in the dark. Here are the most reliable fixes I’ve found:
1. Manually Attach the Debugger to the Extension Process
Xcode only attaches to the main app by default when you hit run. To capture the extension’s output:
- Launch your main app from Xcode as usual.
- Trigger a notification that will open your
UNNotificationContentExtension. - While the extension is visible, switch back to Xcode and go to Debug > Attach to Process. Look for your extension’s name (it’ll usually be something like
YourAppName-NotificationExtension). - Once attached, any
print()or logging statements from the extension will start showing up in the console.
2. Run the Extension Directly as the Active Scheme
Instead of launching the main app first, set your extension as the active scheme:
- In Xcode’s scheme dropdown (top-left corner), select your
UNNotificationContentExtensiontarget. - Hit the run button. Xcode will prompt you to choose which app to launch (select your main app).
- Now, when the extension opens, Xcode will automatically capture its console output—no manual attaching needed.
3. Use os_log Instead of Basic print()
Sometimes standard print() calls don’t make it to the console for extensions, especially if optimizations are enabled. Switch to Apple’s os_log API for more reliable logging:
import os.log class NotificationViewController: UIViewController, UNNotificationContentExtension { override func viewDidLoad() { super.viewDidLoad() os_log("Notification extension loaded successfully", log: .default, type: .debug) } }
You can also view these logs in the Console app (search for your extension’s name or log messages) if Xcode’s console still isn’t showing them.
4. Double-Check Extension Scheme and Build Settings
Make sure your extension is set up for debugging:
- Go to Product > Scheme > Edit Scheme for your extension.
- Under the Run tab, set the Build Configuration to
Debug. - In your extension’s Build Settings, ensure Debug Information Format is set to
DWARF with dSYM Filefor the Debug configuration. This ensures Xcode can capture debug logs properly.
5. Verify Signing and Provisioning Profiles
If your extension is signed with a release profile, debugging will be disabled. Confirm both your main app and extension are using:
- The same development team.
- Debug provisioning profiles (not Ad Hoc or App Store).
These steps should get your extension’s console output flowing again. Happy debugging!
内容的提问来源于stack exchange,提问作者Joshua Wolff

