I am writing a simple Adobe native extension for iOS.
All it does it show a UISwitch.
However, when I try to setFrame on that UISwitch (or any UIView in this piece of code), or even access the frame of the UISwitch, the app segfaults with a bad memory access crash. The UISwitch itself is NOT nil, because calling setOn on it works, and the UISwitch does show up on the screen if I don't try to call setFrame on it, or set its center, or position it in any way.
Here's the code. The function in question is the first one: showSwitch()
#import "FlashRuntimeExtensions.h"
#import <UIKit/UIKit.h>
FREObject showSwitch(FREContext context,
void* functionData,
uint32_t argc,
FREObject argv[])
{
int32_t isOn;
FREGetObjectAsInt32(argv[0], &isOn);
id delegate = [[UIApplication sharedApplication] delegate];
UIWindow* window = [delegate window];
// The following will segfault
// UISwitch* switchControl = [[[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)];
// This works fine.
UISwitch* switchControl = [[[UISwitch alloc] init] retain];
// This will segfault as well
// [switchControl setFrame:CGRectMake(100, 100, switchControl.frame.size.width, switchControl.frame.size.height)];
[switchControl setOn:(isOn == 1)]; // switchControl is not nil, because this works.
[window addSubview:switchControl];
return NULL;
}
void ContextFinalizer(FREContext context)
{
return;
}
void ContextInitializer(void* extensionData,
const uint8_t* contextType,
FREContext context,
uint32_t* numFunctionsToTest,
const FRENamedFunction** functionsToSet)
{
*numFunctionsToTest = 1;
FRENamedFunction* function = (FRENamedFunction*)malloc(sizeof(FRENamedFunction)*1);
function[0].name = (const uint8_t*)"showSwitch";
function[0].functionData = NULL;
function[0].function = &showSwitch;
*functionsToSet = function;
}
void ExtensionInitializer(void** extensionDataToSet,
FREContextInitializer* contextInitializerToSet,
FREContextFinalizer* contextFinalizerToSet)
{
*extensionDataToSet = NULL;
*contextInitializerToSet = &ContextInitializer;
*contextFinalizerToSet = &ContextFinalizer;
}
void ExtensionFinalizer(void* extensionData)
{
return;
}
I had the same problem to add a view to the window, and after search and test a lot I found this solution, that may help anybody with the same problem.
Add your subview this way: