0
votes

I'm coding a iOS-native extension to my OpenFl program that issues a notification. For some reason I simply can't convert the char* datatype from the parameter to the NSString that the alertBody property expects.

const void issueNotification (const char* message) {
    UILocalNotification* localNotification;
    localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:(1/6)];
    //NSString* messageString = [NSString stringWithFormat:@"%c" , message]; // 1
    //NSString* messageString = [NSString stringWithFormat:@"%s" , message]; // 2
    //std::string messageString = std::string(message); // 3
    //NSString *messageString = [NSString stringWithCString:message encoding:NSUTF8StringEncoding]; // 4
    //NSString *messageString = [[NSString alloc] initWithCString:message encoding:NSUTF8StringEncoding]; // 5
    NSString *messageString = @"AAAAA"; // 6
    localNotification.alertBody = messageString;
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

1 gives me "warning: format specifies type 'int' but the argument has type 'const char *' [-Wformat]" when compiling.

2 compiles but the notification text shows up as something unintelligible (~0y or something like that).

3 won't compile.

4 and 5 will compile but the notifications won't fire.

6 works but then again I'm not using the parameter.

Any idea of what I'm doing wrong?

Thank you.

2
4 and 5 would be the correct ways. When you walk through this in the debugger when it's not working, what is the value of messageString? When you use method 4 or 5, what is messageString before the notification is scheduled? And, obviously, is your C string NULL terminated and in UTF8 format? - quellish
I don't think I can use the debugger... I use a standalone .mm/.h file that gets compiled into a NDLL file that is used later by my OpenFl project... - ultranol
Regarding what messageString has before the notification is scheduled, I'm passing a simple phrase like Hello world, so I assume that's what it should have as soon as 4 or 5 happen? Also, I'm afraid I don't understand how exactly the string arrives there, because it's originally on my OpenFl source code (.hx), which somehow calls a .cpp file that has the .h included - from the .cpp file onwards it is a char* data type, but on the .hx is a String. Passing parameters like that works with float, though. - ultranol
Set a breakpoint in the debugger at the line where it queues the local notification. What is the value of messageString when you hit the breakpoint? A float is not a pointer, both String and char * are. That is a huge difference. The char * is also supposed to her terminated with a NULL character. - quellish

2 Answers

0
votes

In older environments, use [NSString stringWithUTF8String:message]. On recent versions of XCode, you can just write @(message), which is sugar for the older form.

0
votes

you can use [NSString stringWithFormat:@"%s",message]. It works perfectly for my code.