有一个 hack 可用于在 iOS 中的任何两个应用程序或应用程序和扩展程序之间进行通信。唯一的 - 它不适用于 NetworkExtension,因为 Apple 阻止了其中的任何 I/O。
您可以通过以下方式向 DarwinNotificationCenter 发布通知:
let notificationName = CFNotificationName("com.notification.name" as CFString)
let notificationCenter = CFNotificationCenterGetDarwinNotifyCenter()
CFNotificationCenterPostNotification(notificationCenter, notificationName, nil, nil, false)
在您的应用中添加观察者:
let notificationName = "com.notification.name" as CFString
let notificationCenter = CFNotificationCenterGetDarwinNotifyCenter()
CFNotificationCenterAddObserver(notificationCenter,
nil,
{ (
center: CFNotificationCenter?,
observer: UnsafeMutableRawPointer?,
name: CFNotificationName?,
object: UnsafeRawPointer?,
userInfo: CFDictionary?
) in
print("Notification name: \(name)")
},
notificationName,
nil,
CFNotificationSuspensionBehavior.deliverImmediately)
一些链接:
https ://github.com/choefele/CCHDarwinNotificationCenter
https://developer.apple.com/documentation/corefoundation/1542572-cfnotificationcentergetdarwinnot
https://developer.apple.com/library/content/documentation/Darwin/Conceptual/MacOSXNotifcationOv/DarwinNotificationConcepts/DarwinNotificationConcepts.html