How to allocate -> send -> receive -> cast -> deallocate UnsafeRawPointer from extension to app?
Asked Answered
N

1

1

I am new in UnsafeRawPointer. As I got I need to allocate unsafe memory in my extension and send pointer to my app where has to cast and release it. Below is a sketch of what I want to do. I'd like to send a string message from Safari extension and receive it in app by CFNotificationCenterGetDarwinNotifyCenter, how to do it?

let center = CFNotificationCenterGetDarwinNotifyCenter()

CFNotificationCenterAddObserver(center, nil, { (_, observer, name, message, _) -> Void in
    //message as String???             
}, Self.action, nil, .deliverImmediately)

var message = "some text"
CFNotificationCenterPostNotification(center, .init(action), message, nil, true)

Main questions?

  • What type use to send String?
  • How to allocate memory right?
  • How to cast it to String in observer?
  • How to deallocate memory right?
Northernmost answered 3/10, 2019 at 19:50 Comment(14)
If the extension runs as a separate process then this is impossible: Memory allocated in one process is not available in a different process.Alysa
Sound strange I see CFNotificationCenterGetDarwinNotifyCenter manage it.Northernmost
Compare developer.apple.com/documentation/corefoundation/…: if center is a Darwin notification center then object and userInfo are ignored.Alysa
How max length a message name?Northernmost
I have no idea ...Alysa
I guess I have to use combine UserDefaults and CFNotificationCenterGetDarwinNotifyCenter to send some long text?Northernmost
Compare https://mcmap.net/q/832871/-send-message-from-ios-today-widget-to-app.Alysa
Thanks for point out that object and userInfo are ignored. I missed it. Why use UnsafeRawPointer in same process?Northernmost
Here is an example where the raw pointer is used to pass a pointer to a class instance to the callback function.Alysa
I see but I can't imagine a situation why it is necessary since class it is reference type?Northernmost
When I call some method with class type parameter I send a pointer.Northernmost
CFNotificationCenterPostNotification is actually a C function, and the callback is also a pure C function. C knows nothing about Swift types or instance pointers. That's why the object parameter is a UnsafeRawPointer (the Swift equivalent of void *).Alysa
Awesome, I got it now, thanks.Northernmost
I add https://mcmap.net/q/815360/-how-to-properly-use-cfnotificationcenteraddobserver-in-swift-for-ios to help guys to get the goal quickly. The question is closed by me thanks @MartinR.Northernmost
N
1

Thanks @MartinR, I got all answers on my questions.

We can't send an object though CFNotificationCenterPostNotification so we need to use Darwin and UserDefaults(suitename:) combination.

Schema

  1. Add the CFNotificationCenterAddObserver observer in the app code
  2. Save sending object in UserDefaults(suitename:)
  3. Send the didObjectChanged notification through CFNotificationCenterPostNotification code
  4. Catch didObjectChanged notification in CFNotificationCallback in the app code
  5. Read sending object from UserDefaults(suitename:)

Off-topic

Question Why do we use UnsafeRawPointer? sample

Answer CFNotificationCenterPostNotification is actually a C function, and the callback is also a pure C function. C knows nothing about Swift types or instance pointers. That's why the object parameter is a UnsafeRawPointer (the Swift equivalent of void *).

Northernmost answered 3/10, 2019 at 21:1 Comment(2)
You should summarize the contents of the comments in your answer. Comments are ephemeral and can be deleted at any time.Alysa
I hope I added enough info.Northernmost

© 2022 - 2024 — McMap. All rights reserved.