ios开发-- xcode6创建静态库framework

转载请注明http://blog.csdn.net/mengxiangyue

废话不多说了,下面是步骤:

1 建立一个single view application工程,然后打开工程中的Main.storyboard,选中里面的唯一一个ViewController,点击菜单栏的Editor->embed in->navigation Controller(嵌入这个navigation controller只是为了测试需要,并不是必须的)。

2 点击工程,在targets项目点击左下角的加号,如下图(下图中的TTTTTTT是我已经添加的Framework):

然后会出现如下的图,选择Cocoa Touch Framework


选择next后,输入对应的framework的名字,到这里就创建好了这个framework的工程。

3 引入AFNetWorking,将AFNetWorking拖到项目中,会出现如下的图,选择好Finish导入成功。

4 创建Framework内的类

在创建的Framework上面点击右键,new File-->Coco Touch Class,创建一个Objective-C的类XYTestViewController,类的内容如下:(这个类只是简单的演示,里面引用了AFnetworking)

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2. #import "AFNetworking.h"  
  3.   
  4. @interface XYTestViewController : UIViewController  
  5.   
  6. @end  
.m文件

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import "XYTestViewController.h"  
  2.   
  3. @interface XYTestViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation XYTestViewController  
  8.   
  9. - (void)viewDidLoad {  
  10.     [super viewDidLoad];  
  11.     NSLog(@"进入框架内博");  
  12.     self.view.backgroundColor = [UIColor whiteColor];  
  13.       
  14.     UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10010020060)];  
  15.     [button setTitle:@"点击" forState:UIControlStateNormal];  
  16.     [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  17.     [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];  
  18.     [self.view addSubview:button];  
  19.       
  20. }  
  21.   
  22. - (void)click:(UIButton*)sender {  
  23.     NSLog(@"点击");  
  24.     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];  
  25.     manager.responseSerializer = [AFHTTPResponseSerializer serializer];  
  26.     AFHTTPRequestOperation *option1 =  [manager GET:@"http://www.baidu.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {  
  27.         NSLog(@"xxxxxxxxxxxx----%@", responseObject);  
  28.     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  29.     }];  
  30.       
  31. }  
  32.   
  33. - (void)didReceiveMemoryWarning {  
  34.     [super didReceiveMemoryWarning];  
  35.     // Dispose of any resources that can be recreated.  
  36. }  
  37.   
  38. /* 
  39. #pragma mark - Navigation 
  40.  
  41. // In a storyboard-based application, you will often want to do a little preparation before navigation 
  42. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
  43.     // Get the new view controller using [segue destinationViewController]. 
  44.     // Pass the selected object to the new view controller. 
  45. } 
  46. */  
  47.   
  48. @end  

5 测试

修改工程内的ViewController类,修改后的内容如下:

.h文件

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2. #import "XYTestViewController.h"  
  3.   
  4. @interface ViewController : UIViewController  
  5.   
  6.   
  7. @end  
.m文件

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  ViewController.m  
  3. //  sssssssssss  
  4. //  
  5. //  Created by mxy on 14-11-11.  
  6. //  Copyright (c) 2014年 mxy. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10.   
  11. @interface ViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation ViewController  
  16.   
  17. - (void)viewDidLoad {  
  18.     [super viewDidLoad];  
  19.     UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10010020060)];  
  20.     [button setTitle:@"进入下一个VC" forState:UIControlStateNormal];  
  21.     [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  22.     [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];  
  23.     [self.view addSubview:button];  
  24.       
  25. }  
  26.   
  27. - (void)click:(UIButton*)sender {  
  28.     NSLog(@"进入下一个VC");  
  29.     XYTestViewController *testVC = [[XYTestViewController alloc] init];  
  30.     [self.navigationController pushViewController:testVC animated:YES];  
  31. }  
  32. - (void)didReceiveMemoryWarning {  
  33.     [super didReceiveMemoryWarning];  
  34.     // Dispose of any resources that can be recreated.  
  35. }  
  36.   
  37. @end  

点击运行看看能否正常运行,如果能正常运行就没问题了,然后就能进行下一步了。

6 生成framework

在运行的过程中其实我们已经生成了framework,点击项目中的products,能够看到已经生成的framework,右键show In Finder,能够看到我们生成的framework。在其上级目录会出现四个文件夹:Debug-iphoneos、Debug-iphonesimulator、Release-iphoneos、Release-iphonesimulator,分别对应不同情况下使用的framework。

下面图片中的1、2选择不同,然后commond+b编译出来的framework是不同的:

1 framework框架 2 iPhone模拟器 scheme debug 会编译出Debug-iphonesimulator

1 framework框架 2 IOS Device  scheme debug 会编译出Debug-iphoneos

1 framework框架 2 iPhone模拟器 scheme Release 会编译出Release-iphonesimulator

1 framework框架 2 IOS Device  scheme Release 会编译出Release-iphoneos


设置对外公开的api,如下图:

7 使用

另外新建一个工程,将合适的framework导入到工程内,编写代码使用,由于framework中使用到了AFNetworking,但是并没有将其打包到Framework中,所以新建的工程需要自己手动导入一个framework。如果在代码中遇到如下错误:

dyld: Library not loaded: @rpath/TTTTTTTT.framework/TTTTTTTT
  Referenced from: /Users/mxy/Library/Developer/CoreSimulator/Devices/72AF601F-3B90-4720-ACB0-E98EE7FD26FE/data/Containers/Bundle/Application/5B492415-EB7E-4188-8342-3C4099502F42/testFFFFFF.app/testFFFFFF
  Reason: image not found

上面这个错误我个人理解,是工程编译的过程中没有找到我们自己写的framework。

在工程的buildsetting中搜索runpath search paths,添加$(SRCROOT),然后运行正常。

补充网上看到的一篇文章写得挺好的Xcode 6制作动态及静态Framework

终于写完了,欢迎拍砖。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值