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

为何仅M2 Pro芯片MacBook上AppDelegate接收到空URL?

M2 Pro芯片MacBook上URL Scheme回调接收到空urls数组的问题解决

我首次开发Mac应用,在处理SSO的URL Scheme回调时遇到了一个奇怪的问题:只有搭载M2 Pro芯片的MacBook会出现application(_:open:)方法接收到空urls数组的情况,其他设备正常。以下是我的AppDelegate代码:

class AppDelegate: NSObject, NSApplicationDelegate {
    @StateObject var viewModel = GitHubCopilotViewModel()

    func applicationShouldTerminateAfterLastWindowClosed(_: NSApplication) -> Bool { true }

    // Handle custom URL Scheme
    func application(_ application: NSApplication, open urls: [URL]) {
        Logger.gitHubCopilot.info("Application awakened by URL Scheme")
        Logger.gitHubCopilot.info("Application state: \(application.isActive)")
        Logger.gitHubCopilot.info("Bundle ID: \(Bundle.main.bundleIdentifier ?? "unknown")")
        Logger.gitHubCopilot.info("Received SSO callback URL: \(urls)") // empty?? 🤔
        guard let url = urls.first else { return }
        let scheme = Bundle.main.object(forInfoDictionaryKey: "SSO_URL_SCHEME") as? String ?? ""
        Logger.gitHubCopilot.info("Received callback URL Scheme: \(url.scheme ?? "")")
        Logger.gitHubCopilot.info("Target application callback URL Scheme: \(scheme)")
        if url.scheme == scheme {
            DispatchQueue.main.async { [weak self] in
                Logger.gitHubCopilot.info("Processing SSO callback")
                self?.viewModel.handleSsoCallback(
                    code: NSURLComponents(string: url.absoluteString)?.queryItems?.first(where: {
                        $0.name == "code"
                    })?.value ?? ""
                )
            }
        }
    }
}

可能的原因及解决方案

1. 补充实现userActivity回调方法

Apple Silicon芯片设备上,应用从后台唤醒或冷启动时,URL可能通过NSUserActivity传递,而非直接进入application(_:open:)。添加以下方法补充处理:

func application(_ application: NSApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([NSUserActivityRestoring]) -> Void) -> Bool {
    if userActivity.activityType == NSUserActivityTypeBrowsingWeb, let url = userActivity.webpageURL {
        Logger.gitHubCopilot.info("Received URL from userActivity: \(url)")
        let scheme = Bundle.main.object(forInfoDictionaryKey: "SSO_URL_SCHEME") as? String ?? ""
        if url.scheme == scheme {
            DispatchQueue.main.async { [weak self] in
                self?.viewModel.handleSsoCallback(
                    code: NSURLComponents(string: url.absoluteString)?.queryItems?.first(where: {
                        $0.name == "code"
                    })?.value ?? ""
                )
            }
        }
        return true
    }
    return false
}

2. 验证Info.plist的URL Scheme配置

确保CFBundleURLTypes配置完整且无错误,避免Apple Silicon设备识别异常:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>你的自定义Scheme</string>
        </array>
        <key>CFBundleURLName</key>
        <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    </dict>
</array>

同时确认SSO_URL_SCHEME的键值与实际使用的Scheme完全一致。

3. 处理冷启动时的AppleEvent传递

应用完全关闭后通过URL Scheme启动时,M2 Pro设备可能将URL通过AppleEvent传递,在applicationDidFinishLaunching中添加处理逻辑:

func applicationDidFinishLaunching(_ aNotification: Notification) {
    NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(handleAppleEvent(_:withReplyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
}

@objc func handleAppleEvent(_ event: NSAppleEventDescriptor, withReplyEvent replyEvent: NSAppleEventDescriptor) {
    guard let urlString = event.paramDescriptor(forKeyword: AEKeyword(keyDirectObject))?.stringValue,
          let url = URL(string: urlString) else {
        return
    }
    Logger.gitHubCopilot.info("Received URL from AppleEvent: \(url)")
    let scheme = Bundle.main.object(forInfoDictionaryKey: "SSO_URL_SCHEME") as? String ?? ""
    if url.scheme == scheme {
        viewModel.handleSsoCallback(
            code: NSURLComponents(string: url.absoluteString)?.queryItems?.first(where: {
                $0.name == "code"
            })?.value ?? ""
        )
    }
}

4. 检查沙箱权限配置

若应用开启了沙箱,在Signing & Capabilities中确保勾选Incoming Network Connections (Server)和Outgoing Network Connections (Client),并在Info.plist中添加必要的权限描述(如NSAppleEventsUsageDescription)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 18:23:14