簡體   English   中英

如何從AppDelegate返回當前窗口

[英]How to get back to the current window from AppDelegate

在我的macOS應用程序中,我正在遵循OAuth登錄過程。 我通過自定義url接收到url中的代碼,從而成功進行了身份驗證,通過它我已經可以獲取access_token。 我只需在ViewController單擊一個按鈕即可開始登錄過程。

我登錄的系統然后使用注冊的自定義url方案進行回答,為我提供了一個代碼,然后我必須使用該代碼來獲取帶有POST請求的access_token 該access_token可以用來進行api調用。

我現在的問題是,由於缺乏有關如何實現其他技術的知識,我目前正在AppDelegate函數application(_ application: NSApplication, open urls: [URL])執行所有后面的過程。

據我有限的理解,這是處理此問題的方法,但是現在我如何才能從那里回來掌握當前視圖? 我真的在為視圖控制器生命周期的一部分而苦苦掙扎...

在AppDelegate中,您可以按以下方式獲取對當前窗口的ViewController的引用。 將“ MainViewController”替換為您使用的名稱。

iOS Swift:

if let vc = window?.rootViewController as? MainViewController {
    // use `vc` here by calling public functions
    // and accessing public properties
}

macOS Swift:

if let vc = NSApplication.shared.mainWindow?.contentViewController as? MainViewController {
    // use `vc` here by calling public functions
    // and accessing public properties
}

好的,找到它了:由於在macOS-land中沒有與iOS一樣的rootViewController,因此它的工作方式略有不同:

在macOS中,您可以像“在鍵盤下”控制當前的窗口:

application(_:open:) ,其中redirect_uri被調用:

if let viewController = NSApplication.shared.keyWindow?.contentViewController as? ViewController {
    // make changes to the view
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM