xcode6製作framework(使用第三方依賴架構),xcode6framework
這兩天由於會用到framework所以研究了一下framework的製作,我用到了xcode6.1、AFNetworing。
轉載請註明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)
#import <UIKit/UIKit.h>#import "AFNetworking.h"@interface XYTestViewController : UIViewController@end
.m檔案
#import "XYTestViewController.h"@interface XYTestViewController ()@end@implementation XYTestViewController- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"進入架構內博"); self.view.backgroundColor = [UIColor whiteColor]; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 200, 60)]; [button setTitle:@"點擊" forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; }- (void)click:(UIButton*)sender { NSLog(@"點擊"); AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; AFHTTPRequestOperation *option1 = [manager GET:@"http://www.baidu.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"xxxxxxxxxxxx----%@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }]; }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end
5 測試
修改工程內的ViewController類,修改後的內容如下:
.h檔案
#import <UIKit/UIKit.h>#import "XYTestViewController.h"@interface ViewController : UIViewController@end
.m檔案
//// ViewController.m// sssssssssss//// Created by mxy on 14-11-11.// Copyright (c) 2014年 mxy. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 200, 60)]; [button setTitle:@"進入下一個VC" forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; }- (void)click:(UIButton*)sender { NSLog(@"進入下一個VC"); XYTestViewController *testVC = [[XYTestViewController alloc] init]; [self.navigationController pushViewController:testVC animated:YES];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@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),然後運行正常。
終於寫完了,歡迎拍磚。