Crash when embedding Unity into iOS

I’m trying to bring a Unity scene into our existing iOS application. I exported the Xcode project and modified the UnityAppController to keep it from taking over the app flow. Then on a button press I try to initialize the Unity stuff. Here’s what I have:

ViewController

-(IBAction)clikedStart:(id)sender
{
    _controller = [UnityController instance];
    [_controller startUnity];
}

UnityController

+(UnityController*)instance
{
    static UnityController* instance = nil;
    if(instance == nil)
    {
        UnityInitTrampoline();
        RegisterMonoModules();
        instance = [[UnityController alloc] init];
        [instance setUpDisplay];
    }
    return instance;
}

-(void)setUpDisplay
{
    if ([UIDevice currentDevice].generatesDeviceOrientationNotifications == NO)
        [[UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications];
    
    [DisplayManager Initialize];
    
    _mainDisplay	= [[[DisplayManager Instance] mainDisplay] createView:YES showRightAway:NO];
    _window			= _mainDisplay->window;
    
    [KeyboardDelegate Initialize];
    
    [self createViewHierarchy];
    UnityInitApplicationNoGraphics([[[NSBundle mainBundle] bundlePath]UTF8String]);
}

-(void)startUnity
{
    printf_console("-> Start Unity

");

	UnityInitApplicationGraphics();

	// we make sure that first level gets correct display list and orientation
	[[DisplayManager Instance] updateDisplayListInUnity];
	[self updateOrientationFromController:[SplashScreenController Instance]];

	UnityLoadApplication();
	Profiler_InitProfiler();

	[self showGameUI];
	[self createDisplayLink];
}

-(void)resumeUnity
{
    printf_console("-> Resume Unity

");

    if(_unityAppReady)
    {
        if(_didResignActive)
            UnityPause(false);
    }
    else
    {
        [self performSelector:@selector(startUnity) withObject:nil afterDelay:0];
    }
    
    [GetController().unityView recreateGLESSurfaceIfNeeded];
    
    _didResignActive = false;
}

-(void)pauseUnity
{
    printf_console("-> Pause Unity

");

    if(_unityAppReady)
    {
        UnityPause(true);
        
        extern void UnityStopVideoIfPlaying();
        UnityStopVideoIfPlaying();
        
        // Force player to do one more frame, so scripts get a chance to render custom screen for
        // minimized app in task manager.
        UnityForcedPlayerLoop();
        [self repaintDisplayLink];
    }
    
    _didResignActive = true;
}

-(void)stopUnity
{
    printf_console("-> Stop Unity

");

    Profiler_UninitProfiler();
    UnityCleanup();
}

-(void)dealloc
{
    extern void SensorsCleanup();
    SensorsCleanup();
    
    [self releaseViewHierarchy];
    [super dealloc];
}

But it’s crashing down in UnityInitApplicationNoGraphics([[[NSBundle mainBundle] bundlePath]UTF8String]); with this stack

#0	0x00b9488c in std::_Rb_tree<int, std::pair<int const, Object::RTTI>, std::_Select1st<std::pair<int const, Object::RTTI> >, std::less<int>, stl_allocator<std::pair<int const, Object::RTTI>, (MemLabelIdentifier)1, 16> >::_M_begin() [inlined] at /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:476
#1	0x00b9488c in std::_Rb_tree<int, std::pair<int const, Object::RTTI>, std::_Select1st<std::pair<int const, Object::RTTI> >, std::less<int>, stl_allocator<std::pair<int const, Object::RTTI>, (MemLabelIdentifier)1, 16> >::find(int const&) [inlined] at /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:1373
#2	0x00b94874 in std::map<int, Object::RTTI, std::less<int>, stl_allocator<std::pair<int const, Object::RTTI>, (MemLabelIdentifier)1, 16> >::find(int const&) [inlined] at /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk/usr/include/c++/4.2.1/bits/stl_map.h:542
#3	0x00b94874 in Object::ClassIDToRTTI(int) at /Applications/buildAgent/work/d63dfc6385190b60/Runtime/BaseClasses/BaseObject.cpp:828
#4	0x00b9b88c in Unity::GameObject::RegisterClass() at /Applications/buildAgent/work/d63dfc6385190b60/Runtime/BaseClasses/GameObject.cpp:1374
#5	0x00b94f48 in Object::InitializeAllClasses() at /Applications/buildAgent/work/d63dfc6385190b60/Runtime/BaseClasses/BaseObject.cpp:903
#6	0x00cda400 in InitializeEngineNoGraphics() at /Applications/buildAgent/work/d63dfc6385190b60/Runtime/Misc/SaveAndLoadHelper.cpp:210
#7	0x00ccb594 in PlayerInitEngineNoGraphics(std::string const&, std::string const&) at /Applications/buildAgent/work/d63dfc6385190b60/Runtime/Misc/Player.cpp:711
#8	0x00ac6170 in UnityInitApplicationNoGraphics at /Applications/buildAgent/work/d63dfc6385190b60/PlatformDependent/iPhonePlayer/LibEntryPoint.mm:188

Specifically on this line of the assembly 0xbc288c: ldr r3, [r1]

Has anyone else managed to do this successfully? What am I missing?

Seems like the reverse integration doesn’t works & the other way does. When I tried porting unity libs & settings to my iOS project I saw a crash (the app exits without a stack trace) after internal call to

InitializeMonoFromMain(std::vector<std::string, std::allocator<std::string> > const&, std::string, int, char const**, bool) + 524 at MonoManager.cpp:2078
0x142d9a4:  movw   r0, #0xef40
0x142d9a8:  mov    r1, r4
0x142d9ac:  movt   r0, #0xae
0x142d9b0:  ldr    r0, [pc, r0]
0x142d9b4:  bl     0x142df08                 ; std::vector<std::string, std::allocator<std::string> >::operator=(std::vector<std::string, std::allocator<std::string> > const&) at stl_vector.h:283
0x142d9b8:  mov    r0, #0x0
0x142d9bc:  mov    r4, #0x0
0x142d9c0:  bl     0x18d9724                 ; mono_config_parse
0x142d9c4:  mov    r0, #0x0
0x142d9c8:  bl     0x1841bd4                 ; mono_parse_default_optimizations
0x142d9cc:  mov    r1, r0
0x142d9d0:  mov    r0, #0x0
0x142d9d4:  bl     0x183ad0c                 ; mono_set_defaults
0x142d9d8:  movw   r0, #0x2964 

But if I add my classes to the unity editor’s auto generated project, it works fine.

Has someone found any solution to be able to embed Unity scene into an existing iOS project?

It worked for me with the latest version of Unity, but when I updated to Unity 2017 it started crushing in the same line as @The13Beast:

UnityInitApplicationNoGraphics([[[NSBundle mainBundle] bundlePath]UTF8String]);

Thank you!