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

SwiftUI macOS实现Chrome无痕新窗口打开指定URL求助

Fixing Chrome Incognito Window Launch in SwiftUI macOS

Hey there! Let's figure out why your Chrome incognito window code isn't working—there are a couple of key issues in your current implementation:

1. Wrong launchPath Target

You're pointing directly at the app bundle (/Applications/Google Chrome.app), but Process needs the path to the actual executable inside the bundle, not the bundle itself. The correct path is /Applications/Google Chrome.app/Contents/MacOS/Google Chrome.

2. Incorrect Argument Format

Your arguments are passed as a single string, which Process interprets as one big argument instead of separate commands. Each flag and URL should be its own element in the arguments array. Also, avoid leading spaces in flags like -incognito—they'll break the command parsing.

Corrected Code

Here's the fixed version that should work reliably:

import Foundation

func launchChromeIncognito(with url: String) {
    let chromeExecutablePath = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
    
    // Verify the executable exists first (optional but helpful)
    guard FileManager.default.fileExists(atPath: chromeExecutablePath) else {
        print("Google Chrome isn't installed at the expected path!")
        return
    }
    
    let process = Process()
    process.executableURL = URL(fileURLWithPath: chromeExecutablePath)
    // Split arguments into individual elements, use full HTTPS URL for consistency
    process.arguments = ["--incognito", "--new-window", url]
    
    do {
        try process.run()
        process.waitUntilExit() // Optional: Wait for the process to finish if needed
    } catch {
        print("Failed to launch Chrome: \(error.localizedDescription)")
    }
}

// Usage in your SwiftUI view or code
// launchChromeIncognito(with: "https://google.com")

Key Improvements:

  • Uses executableURL instead of the deprecated launchPath (Apple recommends this for modern macOS apps)
  • Splits arguments into a proper array for correct parsing
  • Adds a check to verify Chrome is installed at the expected path
  • Includes error handling to catch and report launch issues
  • Uses a full HTTPS URL instead of just google.com to ensure proper navigation

Bonus: Alternative Using NSWorkspace

For macOS apps, using NSWorkspace is often a cleaner approach that handles app bundles directly (no need to dig into Contents/MacOS):

import AppKit

func launchChromeIncognitoWithWorkspace(url: String) {
    guard let chromeURL = NSWorkspace.shared.urlForApplication(withBundleIdentifier: "com.google.Chrome") else {
        print("Google Chrome not found!")
        return
    }
    
    let configuration = NSWorkspace.OpenConfiguration()
    configuration.arguments = ["--incognito", "--new-window"]
    
    do {
        try NSWorkspace.shared.open([URL(string: url)!], withApplicationAt: chromeURL, configuration: configuration)
    } catch {
        print("Error launching Chrome: \(error.localizedDescription)")
    }
}

// Usage
// launchChromeIncognitoWithWorkspace(url: "https://google.com")

This method is more robust because it uses the app's bundle identifier to locate Chrome, even if it's installed in a non-standard location.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 08:17:44