2016-08-23 112 views
0

对不起,我说的不好的英语,但我会清楚地描述我的问题。NSBundle无法卸载动态框架

我想实现下载动态框架,并在我们的“点播服务”应用程序加载它。(见本article)(因为室内的应用程序,我们不关心的AppStore。)

下载,加载和使用动态框架是可以的!它工作正常。

但是,当我尝试卸载它并下载新的动态框架(相同的框架名称,但不同的内部类)来加载它(所有旧的框架将在加载新的框架之前卸载并删除)。类。

卸载框架代码:

NSBundle *bundle = [NSBundle bundleWithPath:documentsPath]; 
result = [bundle unload]; // I use break point, it says unload MyFramework.framework. 

[self removeBundleAndZip]; // It's just a function to remove bundle files. 

但它仍然可以使用类。我很确定这个类是dealloc(我在dealloc类中打印它)。我想这是Objective-C运行时缓存这个类。

所以我发现另一篇文章(How to remove NSBundle cache),我将其添加这样的:

NSBundle *bundle = [NSBundle bundleWithPath:documentsPath]; 
result = [bundle unload]; // I use break point, it says unload MyFramework.framework. 

if (FlushBundleCache(bundle)) { 
    NSLog(@" ** Success: flush bundle cache SUCCESS ..."); 
} 
else{ 
    NSLog(@" ** Faile: flush bundle cache FAIL! "); 
} 

[self removeBundleAndZip]; // It's just a function to remove bundle files. 

刷新成功,但是仍然无法加载和卸载后,旧的用新的框架。 (加载是好的,但仍然旧的类不新框架,如果我重新启动应用程序,它会使用新的框架。这表明我们下载它,并加载,如果我们重新启动应用程序是好的。)

有什么办法卸载旧的框架和加载新的,但不重新启动应用程序?

PS:这是我的负荷框架代码...

NSString *documentsPath = [NSString stringWithFormat:@"%@/Documents/%@/%@",NSHomeDirectory() , @"frameworkFolder" , @"myDownloadFramework.framework"]; 

if ([[NSFileManager defaultManager] fileExistsAtPath:documentsPath]) { 

    NSError *error = nil; 
    NSBundle *bundle = [NSBundle bundleWithPath:documentsPath]; 

    if ([bundle loadAndReturnError:&error]) { 
     if (error) { 
      NSLog(@"Fail: NSBundle load framework fail.(%@)" , error.description); 
     } 
     else{ 
      NSLog(@"Success: NSBundle load framework success!"); 
      result = YES; 
     } 
    } 
    else{ 
     NSLog(@"Fail: NSBundle load framework fail."); 
    } 
} 
else{ 
    NSLog(@"Fail: NSBundle load framework fail.(file not exist)"); 
} 

用我的类,这是在下载框架

Class myClass = objc_getClass("MyClassInFramework"); 

使用类方法

SEL myMethod = @selector(myMethodInClass); 
if([myClass responseToSelector:myMethod]){ 
    objc_msgSend(myClass , myMethod , nil); 
} 

PS:我们需要这样做是因为我们想要防止发布我们的应用程序,如果我们有新的更新。

+0

我认为苹果阻止这种方式来加载iOS 10后的动态框架。悲伤:( –

回答

0

也许你不应该在你的框架中使用ARC。

我认为应用程序仍然加载框架,直到你杀死它。