`
啸笑天
  • 浏览: 3435318 次
  • 性别: Icon_minigender_1
  • 来自: China
社区版块
存档分类
最新评论

IOS-使用framework实现功能模块动态更新

 
阅读更多

测试Xcode版本为6.1.1

实现过程简述:把想要的作为动态更新的模块,移动到我们创建的Framework工程中,然后得到我们想要的动态库文件,把此文件再通过iTunes放到“主程序”项目的document文件夹下,从而实现从主程序中去加载此动态库,从而实现功能模块的动态更新效果。理想效果为支付宝APP,而支付宝采用的是HTML5(网页)的形式实现的,这是目前最通用的实现方式,但是不适用于复杂界面效果。

 

第一步:创建IOS Framework工程

 



 

 

第二步:生成的工程中原本的类删掉不用

 

然后添加新的类文件来实现动态加载效果



 

 

图片和测试界面不多说,重点为PacteraFramework这个类文件中的代码部分:

添加入口方法:

 

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. /* 
  2.  主程序和此动态库的关系枢纽,也就是从“主程序”到“动态库内封装的程序”的入口方法 
  3.  @param mainCon “主程序”中入口按钮所在的ViewController对象 
  4.  @param bundle 此动态库在document文件中的路径,用于xib的加载和图片的加载 
  5.  */  
  6. -(void)showView:(id)mainCon withBundle:(NSBundle *)bundle;  


实现此方法:

 

 

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. -(void)showView:(id)mainCon withBundle:(NSBundle *)bundle  
  2. {  
  3.     /* 
  4.      *初始化第一个controller 
  5.       TheFirstViewController继承于RootViewController 
  6.      *这里的重点是xib文件的加载 
  7.       通常我们在初始化xib的时候并不是很在意bundle:这个参数,一般情况下都会赋予nil值 
  8.       其实我们所用到的图片、xib等资源文件都是在程序内部中获取的,也就是我们常用的[NSBundle mainBundle]中获取,所谓的NSBundle本质上就是一个路径,mainBundle指向的是.app下。 
  9.       而如果我们不指定bundle,则会默认从.app路径下去寻找资源。 
  10.       不过很显然,我们的动态库是放到“主程序”的document文件下的,所以资源文件是不可能在[NSbundle mainBundle]中获取到的,所以这里我们需要指定bundle参数,这也是传递framework的路径的意义所在 
  11.      */  
  12.     TheFirstViewController *firstCon = [[TheFirstViewController alloc]initWithNibName:@"TheFirstViewController" bundle:bundle];  
  13.     //保存NSBundle  
  14.     firstCon.root_bundle = bundle;  
  15.       
  16.     //加上导航栏,并隐藏。  
  17.     UINavigationController *navCon = [[UINavigationController alloc]initWithRootViewController:firstCon];  
  18.     [navCon setNavigationBarHidden:YES];  
  19.       
  20.     //转换传递过来的mainCon参数,实现界面跳转  
  21.     UIViewController *viewCon = (UIViewController *)mainCon;  
  22.     [viewCon presentViewController:navCon animated:YES completion:^{  
  23.         NSLog(@"跳转到动态更新模块成功!");  
  24.     }];  
  25. }  


上面描述了xib文件的加载,下面是使用图片的注意事项:(使用RootViewController基类的代码做说明)

 

 

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. /* 
  2.      *注意获取图片的方式,通过路径+图片名称去获取 
  3.      *如果直接使用[UIImage imageNamed:@"root_top_bg.png"]方式加载是会出现问题, 
  4.       因为在当前程序的路径下是找不到此图片的,图片也会被封装到framwork中 
  5.      *如果在xib文件中直接为某个控件添加图片的话,和平常使用一样直接添加图片名称,不会出现路径的问题 
  6.      */  
  7.     [navImageView setImage:[UIImage imageWithContentsOfFile:[self.root_bundle pathForResource:@"root_top_bg" ofType:@"png"]]];  

 

 

以上为一些frameweok工程中的注意事项,现在我们运行得到动态库文件



 

 

选择Show in Finder,取出framwork文件



 

 

第三步:创建“主程序”的项目工程



 

 

第四步:设置此工程可以通过iTunes来实现文件共享




 

 

然后通过itunes把framework放到document路径中



 

 

第五步:获取framework,并调用上面提到的动态库入口方法和传递参数

(此项目在界面上添加了一个简单的按钮,点击按钮来进入动态库)

 

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. -(void)testFramework  
  2. {  
  3.     NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);  
  4.     NSString *documentDirectory = nil;  
  5.     if ([paths count] != 0)  
  6.         documentDirectory = [paths objectAtIndex:0];  
  7.       
  8.     //拼接我们放到document中的framework路径  
  9.     NSString *libName = @"PacteraFramework.framework";  
  10.     NSString *destLibPath = [documentDirectory stringByAppendingPathComponent:libName];  
  11.       
  12.     //判断一下有没有这个文件的存在 如果没有直接跳出  
  13.     NSFileManager *manager = [NSFileManager defaultManager];  
  14.     if (![manager fileExistsAtPath:destLibPath]) {  
  15.         NSLog(@"There isn't have the file");  
  16.         return;  
  17.     }  
  18.       
  19.     //复制到程序中  
  20.     NSError *error = nil;  
  21.       
  22.     //加载方式一:使用dlopen加载动态库的形式 使用此种方法的时候注意头文件的引入  
  23. //    void* lib_handle = dlopen([destLibPath cStringUsingEncoding:NSUTF8StringEncoding], RTLD_LOCAL);  
  24. //    if (!lib_handle) {  
  25. //        NSLog(@"Unable to open library: %s\n", dlerror());  
  26. //        return;  
  27. //    }  
  28.     //加载方式一 关闭的方法  
  29.     // Close the library.  
  30. //    if (dlclose(lib_handle) != 0) {  
  31. //        NSLog(@"Unable to close library: %s\n",dlerror());  
  32. //    }  
  33.       
  34.     //加载方式二:使用NSBundle加载动态库  
  35.     NSBundle *frameworkBundle = [NSBundle bundleWithPath:destLibPath];  
  36.     if (frameworkBundle && [frameworkBundle load]) {  
  37.         NSLog(@"bundle load framework success.");  
  38.     }else {  
  39.         NSLog(@"bundle load framework err:%@",error);  
  40.         return;  
  41.     }  
  42.       
  43.     /* 
  44.      *通过NSClassFromString方式读取类 
  45.      *PacteraFramework 为动态库中入口类 
  46.      */  
  47.     Class pacteraClass = NSClassFromString(@"PacteraFramework");  
  48.     if (!pacteraClass) {  
  49.         NSLog(@"Unable to get TestDylib class");  
  50.         return;  
  51.     }  
  52.       
  53.     /* 
  54.      *初始化方式采用下面的形式 
  55.       alloc init的形式是行不通的 
  56.       同样,直接使用PacteraFramework类初始化也是不正确的 
  57.      *通过- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2; 
  58.       方法调用入口方法(showView:withBundle:),并传递参数(withObject:self withObject:frameworkBundle) 
  59.      */  
  60.     NSObject *pacteraObject = [pacteraClass new];  
  61.     [pacteraObject performSelector:@selector(showView:withBundle:) withObject:self withObject:frameworkBundle];  
  62.       
  63. }  


第六步:运行“主程序”项目到设备上,然后点击按钮进入动态库模块吧。

 

注意:有时候我们运行framework工程获取framework文件,内部并没有我们想要的xib文件(这个时候内部展示的为nib格式的),所以在我们每次运行此工程的时候,都需要clean

 

demo下载地址:点击跳转到下载页面 

感谢:http://blog.csdn.net/like7xiaoben/article/details/44081257

 

  • 大小: 114.5 KB
  • 大小: 61.3 KB
  • 大小: 95 KB
  • 大小: 35.2 KB
  • 大小: 120.7 KB
  • 大小: 96.5 KB
  • 大小: 159.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics