Skip to main content

autoUpdater

Enable apps to automatically update themselves.启用应用程序自动更新自身。

Process: 进程:Main

See also: A detailed guide about how to implement updates in your application.另请参阅:关于如何在应用程序中实现更新的详细指南

autoUpdater is an EventEmitter.是一个EventEmitter

Platform Notices平台通知

Currently, only macOS and Windows are supported. 目前,仅支持macOS和Windows。There is no built-in support for auto-updater on Linux, so it is recommended to use the distribution's package manager to update your app.Linux上没有内置的自动更新程序支持,因此建议使用发行版的软件包管理器来更新您的应用程序。

In addition, there are some subtle differences on each platform:此外,每个平台都有一些细微的差异:

macOS

On macOS, the autoUpdater module is built upon Squirrel.Mac, meaning you don't need any special setup to make it work. 在macOS上,autoUpdater模块构建在Squirrel.Mac上,这意味着您不需要任何特殊的设置即可使其工作。For server-side requirements, you can read Server Support. 有关服务器端需求,您可以阅读服务器支持Note that App Transport Security (ATS) applies to all requests made as part of the update process. 请注意,应用程序传输安全(ATS)适用于作为更新过程一部分提出的所有请求。Apps that need to disable ATS can add the NSAllowsArbitraryLoads key to their app's plist.需要禁用ATS的应用程序可以将NSAllowsArbitraryLoads密钥添加到其应用程序的plist中。

Note: Your application must be signed for automatic updates on macOS. 您的应用程序必须经过签名才能在macOS上自动更新。This is a requirement of Squirrel.Mac.这是Squirrel.Mac的要求。

Windows

On Windows, you have to install your app into a user's machine before you can use the autoUpdater, so it is recommended that you use the electron-winstaller, electron-forge or the grunt-electron-installer package to generate a Windows installer.在Windows上,您必须将应用程序安装到用户的机器中,然后才能使用autoUpdater,因此建议您使用electron-winstallerelectron-forgegrunt-electron-installer包来生成Windows安装程序。

When using electron-winstaller or electron-forge make sure you do not try to update your app the first time it runs (Also see this issue for more info). 当使用electron-winstallerelectron-forge时,请确保在应用程序第一次运行时不要尝试更新(有关更多信息,请参阅此问题)。It's also recommended to use electron-squirrel-startup to get desktop shortcuts for your app.还建议使用electron-squirrel-startup来获取应用程序的桌面快捷方式。

The installer generated with Squirrel will create a shortcut icon with an Application User Model ID in the format of com.squirrel.PACKAGE_ID.YOUR_EXE_WITHOUT_DOT_EXE, examples are com.squirrel.slack.Slack and com.squirrel.code.Code. 使用Squirrel生成的安装程序将创建一个带有应用程序用户模型ID的快捷图标,格式为com.squirrel.PACKAGE_ID.YOUR_EXE_WITHOUT_DOT_EXE,例如com.squirrel.slack.Slackcom.squirrel.code.CodeYou have to use the same ID for your app with app.setAppUserModelId API, otherwise Windows will not be able to pin your app properly in task bar.您必须使用与app.setAppUserModelId API相同的应用程序ID,否则Windows将无法将您的应用程序正确地固定在任务栏中。

Like Squirrel.Mac, Windows can host updates on S3 or any other static file host. 与Squirrel.Mac一样,Windows可以在S3或任何其他静态文件主机上托管更新。You can read the documents of Squirrel.Windows to get more details about how Squirrel.Windows works.您可以阅读Squirrel.Windows的文档,以获得有关Squirrel.Windows如何工作的更多详细信息。

Events事件

The autoUpdater object emits the following events:autoUpdater对象会发出以下事件:

Event: 'error'

Returns:返回:

  • error Error

Emitted when there is an error while updating.当更新时出现错误时发出。

Event: 'checking-for-update'

Emitted when checking if an update has started.在检查更新是否已启动时发出。

Event: 'update-available'

Emitted when there is an available update. 在有可用更新时发出。The update is downloaded automatically.更新会自动下载。

Event: 'update-not-available'

Emitted when there is no available update.在没有可用更新时发出。

Event: 'update-downloaded'

Returns:返回:

  • event Event
  • releaseNotes string
  • releaseName string
  • releaseDate Date
  • updateURL string

Emitted when an update has been downloaded.在下载更新时发出。

On Windows only releaseName is available.在Windows上,只有releaseName可用。

Note: It is not strictly necessary to handle this event. 处理这个事件并不是绝对必要的。A successfully downloaded update will still be applied the next time the application starts.成功下载的更新仍将在下次应用程序启动时应用。

Event: 'before-quit-for-update'

This event is emitted after a user calls quitAndInstall().此事件是在用户调用quitAndInstall()后发出的。

When this API is called, the before-quit event is not emitted before all windows are closed. 当调用此API时,在关闭所有窗口之前不会发出before-quit事件。As a result you should listen to this event if you wish to perform actions before the windows are closed while a process is quitting, as well as listening to before-quit.因此,如果您希望在进程退出时关闭窗口之前执行操作,以及在before-quit上进行侦听,则应该侦听此事件。

Methods方法

The autoUpdater object has the following methods:autoUpdater对象具有以下方法:

autoUpdater.setFeedURL(options)

  • options Object
    • url string
    • headers Record<string, string> (optional) macOS - HTTP request headers.HTTP请求标头。
    • serverType string (optional) macOS - Can be json or default, see the Squirrel.Mac README for more information.可以是jsondefault,请参阅Squirrel.Mac自述文件以获取更多信息。

Sets the url and initialize the auto updater.设置url并初始化自动更新程序。

autoUpdater.getFeedURL()

Returns 返回string - The current update feed URL.当前更新源URL。

autoUpdater.checkForUpdates()

Asks the server whether there is an update. 询问服务器是否有更新。You must call setFeedURL before using this API.在使用此API之前,必须调用setFeedURL

Note: If an update is available it will be downloaded automatically. 如果有可用的更新,则会自动下载。Calling autoUpdater.checkForUpdates() twice will download the update two times.调用autoUpdater.checkForUpdates()两次将下载两次更新。

autoUpdater.quitAndInstall()

Restarts the app and installs the update after it has been downloaded. 重新启动应用程序,并在下载后安装更新。It should only be called after update-downloaded has been emitted.只有在发出update-downloaded后才能调用它。

Under the hood calling autoUpdater.quitAndInstall() will close all application windows first, and automatically call app.quit() after all windows have been closed.在后台调用autoUpdater.quitAndInstall()将首先关闭所有应用程序窗口,并在关闭所有窗口后自动调用app.quit()

Note: It is not strictly necessary to call this function to apply an update, as a successfully downloaded update will always be applied the next time the application starts.调用此函数应用更新并不是绝对必要的,因为成功下载的更新将始终在下次应用程序启动时应用。