xcode6製作framework(使用第三方依賴框架)
這兩天由於會用到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)
.m檔案#import <UIKit/UIKit.h> #import "AFNetworking.h" @interface XYTestViewController : UIViewController @end
#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),然後執行正常。
終於寫完了,歡迎拍磚。