ios - 应用程序将终止通知
<p><p>我正在开发 SDK 并 try catch 应用程序终止 <code>Notification</code>。很容易将其作为(例如)<code>NSNotification.Name.UIApplicationWillResignActive</code></p> 的闭包
<pre><code>self.resignActiveNotification = NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationWillResignActive,
object: nil,
queue: nil) { _ in
//something goes here and it works like a charm.
}
</code></pre>
<p>所以我想有类似的终止行为:</p>
<pre><code>self.terminateNotification = NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationWillTerminate,
object: nil,
queue: nil) { _ in
NSLog("%@",#function)
}
</code></pre>
<p>而且这个永远不会被调用!</p>
<p>当然,如果我输入 <code>AppDelegate</code>:</p>
<pre><code>func applicationWillTerminate(_ application: UIApplication) {
//termination
}
</code></pre>
<p>它会起作用,但由于我正在构建一个 SDK,我无法使用 AppDelegate 方法。有没有办法获得终止关闭调用?或者任何其他方式知道应用程序即将终止?</p>
<p>在 <a href="https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623111-applicationwillterminate" rel="noreferrer noopener nofollow">Apple documentation</a>你可以找到:</p>
<blockquote>
<p>After calling this method, the app also posts a
UIApplicationWillTerminate notification to give interested objects a
chance to respond to the transition.</p>
</blockquote>
<p>然而这似乎被打破了</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这似乎对我有用:</p>
<pre><code>init() {
NotificationCenter.default.addObserver(self,
selector: #selector(applicationWillTerminate(notification:)),
name: UIApplication.willTerminateNotification,
object: nil)
}
@objc func applicationWillTerminate(notification: Notification) {
// Notification received.
}
deinit {
NotificationCenter.default.removeObserver(self)
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 应用程序将终止通知,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/49030947/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/49030947/
</a>
</p>
页:
[1]