【问题标题】:AppDelegate function not called when open the host application from the Widget (Today Extension)从小部件打开主机应用程序时未调用 AppDelegate 函数(今日扩展)
【发布时间】:2018-04-21 15:05:53
【问题描述】:

目标

从今日扩展(Widget)打开应用并根据按钮点击执行不同的操作。

  • 目标:iOS 10

已阅读的帖子和文章


已经完成

创建 URLScheme

目标/MyApp/Info/URLTypes

  • URLType 标识符 = com.myCompany.MyAppName
  • URLType URLSchemes = MyAppName
  • URLType 角色 = 查看者

从 Widget 打开宿主应用程序

TodayViewController(小部件)

@IBAction func addButtonPressed(_ sender: UIButton) {
  let url = URL(string: "MyAppName://")!

  extensionContext?.open(url) { isSuccess in
    switch isSuccess {
    case true: print("Open URL Success") // It print when opening app
    case false: print("Open URL Failed")
    }
  }
}

点击按钮时效果很好。 Host 应用程序已启动。


委托 func 应用程序(_:open:options:) 未调用

AppDelegate(宿主应用程序)

// This delegate function is never called when the host application launches
func application(_ app: UIApplication, // breakpoint doesn't trigger
               open url: URL,
               options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { 

  print("FUNCTION CALLED ????????????") // Never print

  return true
}

当通过 Widget 中的按钮打开主机应用程序时,永远不会调用此委托函数。从小部件打开应用程序时,我的应用程序如何运行一些代码。 我尝试使用其他一些方法,但它们已被弃用。

我继续搜索,当小部件打开主机应用程序时,似乎无法调用 AppDelegate 函数,因为在应用程序启动时不会调用所有其他方法,例如 applicationWillEnterForeground。

我在真机上试过,但还是不行。

【问题讨论】:

    标签: ios swift widget url-scheme today-extension


    【解决方案1】:

    转到您的应用目标然后信息 -> URL 类型 -> 添加 URL 方案

    在您的 TodayViewController 中粘贴以下代码

    self.extensionContext?.open(URL(string: "Your URL Scheme identifier")!, completionHandler: nil)
    

    【讨论】:

      猜你喜欢
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-15
      相关资源
      最近更新 更多