0

I am really a newbie in osX development environment. My application is developed for cross-platform, written in C++ without using any framework and non-gui. I want to add notification mechanism to my application for osX. My os version is Monterey now.

I tried to write an application by inspiring from this repo objective-c in c. But this repository is using a deprecated method that NSUserNotificationCenter. Apple recommends that method create any local notification.

After then wrote this code,

#define OBJC_OLD_DISPATCH_PROTOTYPES 1
#include <CoreFoundation/CoreFoundation.h>
#include <objc/objc-runtime.h>


int main(int argc, char** argv) {
    Class contentClass = objc_getClass("UNMutableNotificationContent");
    
    id content = objc_msgSend((id)contentClass, sel_registerName("alloc"), sel_registerName("init"));
    
    Ivar titleIvar = class_getInstanceVariable(contentClass, "title");
    
    object_setIvar((id)contentClass, titleIvar, objc_msgSend((id)objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"), "asdas"));
    
    Ivar bodyIvar = class_getInstanceVariable(contentClass, "body");
    
    object_setIvar((id)contentClass, bodyIvar, objc_msgSend((id)objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"), "asdasdasdasd"));
    
    id notificationTrigger = objc_msgSend((id)objc_getClass("UNTimeIntervalNotificationTrigger"), sel_registerName("triggerWithTimeInterval:repeats:"), 5, true);
    
    id notificationRequest = objc_msgSend((id)objc_getClass("UNNotificationRequest"), sel_registerName("requestWithIdentifier:content:trigger:"), objc_msgSend((id)objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"), "FiveSecond"), (id)contentClass, notificationTrigger);
    
    id center = objc_msgSend((id)objc_getClass("UNUserNotificationCenter"), sel_registerName("currentNotificationCenter"));
    
    objc_msgSend(center, sel_registerName("addNotificationRequest:"), notificationRequest);
    
    return 0;
}

but it did not work. May I have misunderstood the usage of the runtime library of Objective-c.

I don`t want to migrate my application to any language, it should keep in C++.

hrn
  • 48
  • 6
  • 1
    You can in some files use `Objective-C++`. For example create `C++` classes that will have `Objective-C++` implementation. So you classes public interface will be in plain `C++` and you will able to use it with other `C++` code. – Cy-4AH Nov 12 '21 at 10:07
  • How can use it? Do you have any examples of what you said? – hrn Nov 12 '21 at 10:46
  • You need to use `.mm` files instead of `.cpp` and you can use `Objective-C` code where you need it. – Cy-4AH Nov 12 '21 at 11:18
  • How does it not work? I tried your code and I get a warning 'Unused variable `content`' and an exception 'time interval must be at least 60'. – Willeke Nov 12 '21 at 12:44
  • @Cy-4AH Okay, There is a problem with how can I delegate my objective-c code in my cpp code? I could not find anything about it on the web. – hrn Nov 13 '21 at 20:58
  • @Willeke it does not already work. The unused variable is not that does not prevent to properly working as well. The time interval is the same as in Apple documentation. If you think this makes it work, I could try it. – hrn Nov 13 '21 at 21:03
  • There are more problems, better try using Objective-C first. See [Calling Objective-C method from C++ member function?](https://stackoverflow.com/questions/1061005/calling-objective-c-method-from-c-member-function) – Willeke Nov 15 '21 at 08:49
  • @Willeke Yes, I have seen this topic. But it does not work for me because it does not explain any mechanism of objc runtime wrappers. BTW I gave up, do not implement it in my project. – hrn Nov 17 '21 at 19:55
  • Note: it is possible to do this with Objective-C Runtime functions but it needs blocks, a framework and a bundle. – Willeke Nov 18 '21 at 07:38
  • Yes, you may be right but as I said in the previous comment I do not know anything about block and bundle in objective-c or osX. – hrn Nov 18 '21 at 14:05

0 Answers0