0

I am doing for enterprise app and I am studying about dlopen because I need to download code and run in runtime. (like sub-module).

Calling dlopen on ios

Download and execute iOS code dynamically at runtime

I start to confuse about that because some open .dylib and some open .framework.

I try to load from resource directory and hModule is always nil.

NSString *dlPath = @"StaticLibrary.dylib";//i try with complete path and it is not okay also

const char* cdlpath = [dlPath UTF8String];
void* hModule = dlopen(cdlpath, RTLD_LAZY);

I download from url and try like this too.

    const char *cString = [filePath cStringUsingEncoding:NSASCIIStringEncoding];

    void *uikit = dlopen(cString, RTLD_GLOBAL);
    id (*FunctionName)() = dlsym(uikit, "helloWorldFromStaticLibrary");
    FunctionName();
    dlclose(uikit);
    //my cString is "/Users/khantthulinn/Library/Developer/CoreSimulator/Devices/04F2B44A-B78E-4400-98B7-EF6AA8352646/data/Containers/Data/Application/63E7D709-F140-4AF1-A4F5-475454B347FE/Library/Caches/StaticLibrary.dylib"

I just don't know why my dlopen is always nil. Some people in stackoverflow said that I need to sign with same certificate. I have tried also. But it is not okay. How shall I do?

Community
  • 1
  • 1
Khant Thu Linn
  • 5,905
  • 7
  • 52
  • 120
  • 1
    And what does `dlerror()` tell you when `dlopen()` fails? – trojanfoe Apr 06 '16 at 14:44
  • Can I log like this? const char *dlErr = dlerror(); When I po dlerror(), it is . – Khant Thu Linn Apr 06 '16 at 14:48
  • and that's inside `if (uikit == NULL) { ... }`? – trojanfoe Apr 06 '16 at 14:49
  • yes. It is like that. I now po dlErr and I saw this. "dlopen(/Users/khantthulinn/Library/Developer/CoreSimulator/Devices/04F2B44A-B78E-4400-98B7-EF6AA8352646/data/Containers/Data/Application/EE454318-5521-49CE-80E6-2DF3361B209E/Library/Caches/StaticLibrary.dylib, 8): no suitable image found. Did find:\n\t/Users/khantthulinn/Library/Developer/CoreSimulator/Devices/04F2B44A-B78E-4400-98B7-EF6AA8352646/data/Containers/Data/Application/EE454318-5521-49CE-80E6-2DF3361B209E/Library/Caches/StaticLibrary.dylib: unknown file type, first eight bytes: 0x20 0x3C 0x21 0x44 0x4F 0x43 0x54 0x59" – Khant Thu Linn Apr 06 '16 at 14:52
  • OK, so there is your answer. The file isn't a dynamic library; it's a file that starts `" <!DOCTY"` (i.e. 95% chance an HTML file). So your download code is borked. – trojanfoe Apr 06 '16 at 14:55
  • ohh...my bad. Now it show me new error. How shall I do? "dlsym(0x7facca41fee0, helloWorldFromStaticLibrary): symbol not found" – Khant Thu Linn Apr 06 '16 at 15:20
  • Are you sure the symbol is visible? Run `nm` on the library and see if you can see it (it will have the type `T`, I believe). – trojanfoe Apr 06 '16 at 15:53
  • Could you please guide me how to run nm? I have uploaded my library to here. http://www.mediafire.com/download/ltcb23rf9lskt81/StaticLibrary_2.zip I wrote like this in .h. http://tinypic.com/r/34ecisz/9 It shall be visible? – Khant Thu Linn Apr 06 '16 at 16:02
  • I am not at my Mac at the moment so I am unable to help. I'm sure you will find plenty of help if you search online. – trojanfoe Apr 06 '16 at 16:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108464/discussion-between-khant-thu-linn-and-trojanfoe). – Khant Thu Linn Apr 07 '16 at 04:10
  • @KhantThuLinn did this work? I've got a similar use case, and I'm having trouble getting the dylib to open with dlopen – Joe Daniels Nov 02 '16 at 14:38
  • sadly no. I give up on that. – Khant Thu Linn Nov 02 '16 at 14:59

0 Answers0