Thursday 8 January 2015

iOS 8 App Extensions- dlopen() Loading Embeded framework at runtime

Unlink Your Embeded framework from Project Target


#import <dlfcn.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
 
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    if ([currSysVer  floatValue]>=8.0) {
        [self loadFrameworkDynamic];
    }
   
  return YES;
}

- (void)loadFrameworkDynamic
{
//WidgetSource is the name of framework
    NSString *LibName = @"WidgetSource";
    NSString *LibExtension = @"framework";
    NSString *Path = [[NSBundle mainBundle] pathForResource:LibName ofType:LibExtension];
    NSLog(@"Loading dynamic library: %@", Path);
    Path=[Path stringByAppendingString:@"/WidgetSource"];
  
    void *revealLib = NULL;
    revealLib = dlopen([Path cStringUsingEncoding:NSUTF8StringEncoding], RTLD_NOW);
   
    if (revealLib == NULL)
    {
        char *error = dlerror();
        NSLog(@"dlopen error: %s", error);
        NSString *message = [NSString stringWithFormat:@"%@.%@ failed to load with error: %s", LibName, LibExtension, error];
        [[[UIAlertView alloc] initWithTitle:@"Library could not be loaded" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    }
}


No comments:

Post a Comment