Recently I decided to create an application for iOS using the C language. Now I have the following code:

#include <CoreFoundation/CoreFoundation.h> #include <objc/runtime.h> #include <objc/message.h> extern void UIApplicationMain(int, ...); int main(int argc, char *argv[]) { Class autoreleasePoolClass = objc_getClass("NSAutoreleasePool"); id autoreleasePool = objc_msgSend(objc_msgSend(autoreleasePoolClass, sel_registerName("alloc")), sel_registerName("init")); UIApplicationMain(argc, argv, nil, CFSTR("AppDelegate")); objc_msgSend(autoreleasePool, sel_registerName("drain")); return EXIT_SUCCESS; } 

The following function is defined in another file:

 __attribute__((constructor)) void initAppDel() { AppDelClass = objc_allocateClassPair((Class) objc_getClass("UIResponder"), "AppDelegate", 0); objc_registerClassPair(AppDelClass); } 

In the OS simulator, this code runs without problems. However, if I launch it on a real device, the application immediately crashes. I can only assume that the thing is that I launch it on the arm64 device and I am calling something wrong. Has anyone come across this before (I understand that few people are addicted to such unusual perversions)?

    1 answer 1

    Everything was decided rather simply and most likely it was a transition from 32 bits to 64. To use the UIApplicationMain function without connecting UIKit explicitly via the import directive (because it was planned to minimize the presence of Objective-C in the project), the following line was used:

     extern void UIApplicationMain(int, ...); 

    However, I decided to specify more precisely the arguments of this function and it turned out the following:

     extern void UIApplicationMain(int, char**, void*, void*); 

    Now the arguments are passed to this function correctly (because the compiler now knows how many bytes its arguments occupy) and the application does not crash. I'm not sure that at least someone else will encounter such problems, but just in case I will leave the decision.