Skip to content

Instantly share code, notes, and snippets.

@phatblat
Created July 7, 2015 20:29
Show Gist options
  • Save phatblat/b10a463ff40d9dc6a808 to your computer and use it in GitHub Desktop.
Save phatblat/b10a463ff40d9dc6a808 to your computer and use it in GitHub Desktop.
Darwin notifications in swift
// MARK: - Darwin Notifications
static private let defaultsOnPhoneChangedDarwinNotificationName = "UserDefaultsOnPhoneDidChangeDarwinNotification"
static private let defaultsOnWatchChangedDarwinNotificationName = "UserDefaultsOnWatchDidChangeDarwinNotification"
static var notificationNameForReceiving: String?
static var notificationNameForSending: String?
/// Sets up the broadcast and receiving darwin notification names as well as
/// as the necessary observer callbacks.
static private func setupDarwinNotifications() {
// Assume the only .appex bundle is the watch extension
if NSBundle.mainBundle().bundleURL.pathExtension == "appex" {
// Running inside an extension
notificationNameForReceiving = defaultsOnPhoneChangedDarwinNotificationName
notificationNameForSending = defaultsOnWatchChangedDarwinNotificationName
}
else {
// Main app bundle
notificationNameForReceiving = defaultsOnWatchChangedDarwinNotificationName
notificationNameForSending = defaultsOnPhoneChangedDarwinNotificationName
}
registerForDarwinNotificationsWithIdentifier(notificationNameForReceiving!)
}
static private func registerForDarwinNotificationsWithIdentifier(identifier: String) {
//func CFNotificationCenterAddObserver(_ center: CFNotificationCenter!,
// _ observer: UnsafePointer<Void>,
// _ callBack: CFNotificationCallback,
// _ name: CFString!,
// _ object: UnsafePointer<Void>,
// _ suspensionBehavior: CFNotificationSuspensionBehavior)
//typealias CFNotificationCallback = CFunctionPointer<((CFNotificationCenter!, UnsafeMutablePointer<Void>, CFString!, UnsafePointer<Void>, CFDictionary!) -> Void)>
let center = CFNotificationCenterGetDarwinNotifyCenter()
let suspensionBehavior = CFNotificationSuspensionBehavior.DeliverImmediately
// let suspensionBehavior = CFBooleanGetValue(true) // Session 224
let callback: @objc_block
(CFNotificationCenter!, UnsafeMutablePointer<Void>, CFString!, UnsafePointer<Void>, CFDictionary!) -> Void = {
(center, observer, name, object, userInfo) in
println("darwin callback")
NSNotificationCenter.defaultCenter().postNotificationName(EBWUserDefaultsDidChangeNotification, object: nil,
userInfo: nil)
}
let imp: COpaquePointer = imp_implementationWithBlock(unsafeBitCast(callback, AnyObject.self))
let notificationCallback: CFNotificationCallback = CFunctionPointer(imp)
CFNotificationCenterAddObserver(center,
nil,
notificationCallback,
identifier,
nil,
suspensionBehavior)
}
static private func sendDarwinNotificationWithIdentifier(identifier: String) {
let center = CFNotificationCenterGetDarwinNotifyCenter()
let deliverImmediately: Boolean = Boolean()
CFNotificationCenterPostNotification(center, identifier as CFString, nil, nil, deliverImmediately)
}
@cnstoll
Copy link

cnstoll commented Mar 7, 2016

Hey Ben, did you ever get this to work in Swift 2? I'm pretty sure CFunctionPointer was only available in betas. I've been struggling to get this to work for a while now and not having much luck.

@amitrajmodi
Copy link

I am also struggling for CFunctionPointer in swift. It is unavailable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment