手把手教你如何搭建iOS項目基本框架


手把手教你如何搭建iOS項目基本框架

今天我們來談談如何搭建框架,框架需要做一些什么。

第一步:找到我們的目標
我們的目標是讓其他開發人員拿到手后即可寫頁面,不再需要考慮其他的問題。


第二步:我們需要做哪些東西
各位跟着我一步一步來進行。

假定我們即將要寫的項目是一個tabbar+navgation的項目結構。

1. 新建工程

1.1 刪除不必要類目

選擇Single ViewApplication,命名為HomeHome
刪除選中的三項。


刪除選中的三項

1.2 修改info.plist

1.刪除info.plist main.storyboard字段


main.storyboard

2.添加字段
info.plist 中添加以下
Bundle display name --- 對應應用名
開啟http訪問,不添加該字段導致不能訪問http,只能訪問https。


開啟http

3.添加應用白名單
iOS9之后分享等必須添加白名單后才可以打開相關應用。字段值為LSApplicationQueriesSchemes

  1. 添加URL Types
    用於分享到QQ,微信等平台或者打開支付寶等,已經設置當前APP的Url shesmes

1.3 修改項目配置

  1. 關閉bitCode
    build setting中搜索bitCode 改為NO

2.配置項目

2.1 建立目錄


項目目錄

2.2 拖入各大依賴庫

我在這里主要依賴了以下庫:

AFNetWorking 網絡請求
SDWebImage 圖片加載
MWPhotoBrowser 圖片瀏覽器
MJRefresh 刷新加載控件
RDVTabBarController 一個很好用的tabbar控件
MBProgressHUD 菊花轉圈
NJKWebViewProgress webview的加載進度條
Masonry 用於適配

2.3 依賴常用第三方服務

通常集成了:

友盟分享
極光推送
騰訊bugly
Mob驗證碼服務

均根據官方文檔安裝

3. 編寫代碼

3.1 建立pch

xcode6之后建立pch 參考該網址

3.1 建立Api.h文件

該聲明文件用於查詢接口等

3.2 建立Config.h文件

該聲明文件用於編寫某些全局配置參數
如以下:

#define kPushPhotoBrowserNotifitationName @"PushPhotoBrowser" #define kPresentVideoPlayerNotifitationName @"playCallBackVideo" #define APPICONIMAGE [UIImage imageNamed:[[[[NSBundle mainBundle] infoDictionary] valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject]] #define APPNAME [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"] #define Main_Color [UIColor colorWithRed:(3)/255.0 green:(160)/255.0 blue:(235)/255.0 alpha:1.0] #define Main2_Color [UIColor colorWithRed:(135)/255.0 green:(202)/255.0 blue:(231)/255.0 alpha:1.0] #define VTColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0] #define Text_Color [UIColor colorWithRed:(51)/255.0 green:(71)/255.0 blue:(113)/255.0 alpha:1.0] #define BackGround_Color [UIColor colorWithRed:(235)/255.0 green:(235)/255.0 blue:(241)/255.0 alpha:1.0] #define Default_Person_Image [UIImage imageNamed:@"default_parents"] #define Default_General_Image [UIImage imageNamed:@"default_general"] #define kScreenW [UIScreen mainScreen].bounds.size.width #define kScreenH [UIScreen mainScreen].bounds.size.height //以及各種第三方服務商的appId或者App key

3.3 開始編寫項目結構

我們分別建立了3個AppDelegate的類別

HHAppDelegate+AppService //app的服務管理
HHAppDelegate+AppLifeCircle//app的生命周期管理
HHAppDelegate+RootController//app的跟視圖控制器實例

先看看HHAppDelegate+RootController

聲明文件

/** * 首次啟動輪播圖 */ - (void)createLoadingScrollView; /** * tabbar實例 */ - (void)setTabbarController; /** * window實例 */ - (void)setAppWindows; /** * 設置根視圖 */ - (void)setRootViewController;

實現:

- (void)setRoot { UINavigationController * navc = [[UINavigationController alloc] initWithRootViewController:self.viewController]; navc.navigationBar.barTintColor = Main_Color; navc.navigationBar.shadowImage = [[UIImage alloc] init]; [navc.navigationBar setTranslucent:NO]; [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; [navc.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}]; navc.navigationBar.tintColor = [UIColor whiteColor]; self.window.rootViewController = navc; } - (void)setTabbarController { HomePageViewController *school = [[HomePageViewController alloc]init]; AboutChildViewController *child = [[AboutChildViewController alloc]init]; CommuntiyViewController *edu = [[CommuntiyViewController alloc]init]; SZCourseListViewController *courseList = [[SZCourseListViewController alloc]init]; AboutMeViewController *about = [[AboutMeViewController alloc]init]; RDVTabBarController *tabBarController = [[RDVTabBarController alloc] init]; [tabBarController setViewControllers:@[school,edu,child,courseList,about]]; self.viewController = tabBarController; tabBarController.delegate = self; [self customizeTabBarForController:tabBarController]; } - (void)setAppWindows { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; [[UIApplication sharedApplication]setStatusBarHidden:NO]; [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent]; [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}]; } - (void)createLoadingScrollView { //引導頁實例 }

注意:這里將Navgation作為根視圖控制器,tabbar作為Navgation的rootViewController。
如此全局只有一個跟導航控制器。頁面Controller與Tabbar處於平級狀態。這與接下來我們彈出各種頁面均由關聯
自此一個大致的結構就出來了,一個Tabbar+Navgation的格式

3.4 編寫AppService

這類信息大都跟需求有關,我們在這里處理項目相關信息。如可能會由以下信息

- (void)registerBugly; /** * 基本配置 */ - (void)configurationLaunchUserOption; /** * 友盟注冊 */ - (void)registerUmeng; /** * Mob注冊 */ - (void)registerMob; /** * 檢查更新 */ - (void)checkAppUpDataWithshowOption:(BOOL)showOption; /** * 上傳用戶設備信息 */ - (void)upLoadMessageAboutUser; /** * 檢查黑名單用戶 */ -(void)checkBlack;

3.5 App結構整理

以上我們處理了跟視圖,服務,app聲明周期等方法實現
現在我們開始調用這些方法。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self setAppWindows]; [self setTabbarController]; [self setRootViewController]; [self configurationLaunchUserOption]; [self registerBugly]; [self registerMob]; [self registerUmeng]; [VTJpushTools setupWithOptions:launchOptions]; [self upLoadMessageAboutUser]; [self checkAppUpDataWithshowOption:NO]; [SZNotificationCenter addObserver:self selector:@selector(phontoBroserPush:) name:kPushPhotoBrowserNotifitationName object:nil]; [SZNotificationCenter addObserver:self selector:@selector(playCallBackVideo:) name:kPresentVideoPlayerNotifitationName object:nil]; [self.window makeKeyAndVisible]; return YES; }

好了,我們着重說一下這里兩個通知。
分別是收到圖片查看的通知、視頻播放的通知
這里以查看圖片舉列。我們認為查看圖片是push到一個新頁面中去。

因此我們需要找到導航控制器
根據之前所說,當前結構中只有一個導航控制器就是Root

+ (UINavigationController *)rootNavigationController { HHAppDelegate *app = (HHAppDelegate *)[UIApplication sharedApplication].delegate; return (UINavigationController *)app.window.rootViewController; }

找到控制器后就變得簡單的多了。
我們開始實現:

這里我們對傳來的數據格式由一定要求,類似於這樣的一個格式。

@{@"imageInfo":@[HHImageInfo*info,HHImageInfo *info2],@"index":@3};

原因:圖片一般有高清圖片和非高清圖片兩種格式。以及圖片有可能帶有文字描述。因此引入ImageInfo。

- (void)phontoBroserPush:(NSNotification *)note { self.imageArr = [NSMutableArray arrayWithCapacity:0]; [self.imageArr removeAllObjects]; NSDictionary *dic = note.object; NSArray *arr = dic[@"imageInfo"]; NSInteger index = [dic[@"index"] integerValue]; self.imageIndex = index; for(HHImageInfo *info in arr) { //[self.imageArr addObject:info.url]; MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:info.url]]; photo.caption = info.desc; [self.imageArr addObject:photo]; } MWPhotoBrowser *browser = [[MWPhotoBrowser alloc]initWithDelegate:self]; browser.zoomPhotosToFill = YES; browser.displayNavArrows = YES; browser.displayActionButton = NO; browser.alwaysShowControls = NO; browser.autoPlayOnAppear = YES; [browser setCurrentPhotoIndex:index]; [[HHAppDelegate rootNavigationController]pushViewController:browser animated:YES]; }

到這里,其他開發人員在進行模塊開發的時候需要圖片瀏覽器的時候直接發送通知即可集成。

HHImageInfo中

@property (nonatomic,strong)NSString *desc; @property (nonatomic,strong)NSString *url; //@property (nonatomic,assign)NSInteger imageIndex; + (NSString *)getHUDImageUrl:(NSString *)smallImageUrl;

實現:

+ (NSString *)getHUDImageUrl:(NSString *)smallImageUrl { NSMutableString *str = [NSMutableString stringWithString:smallImageUrl]; NSRange substr = [str rangeOfString:@"_thu"]; while (substr.location != NSNotFound) { [str replaceCharactersInRange:substr withString:@""]; substr = [str rangeOfString:@"_thu"]; } [str insertString:@"" atIndex:0]; return str; }

4. 其他配置

4.1 rootViewController -- 所有頁面均繼承該控制器

我通常在RootViewController實現以下方法以供調用

/** * 顯示沒有數據頁面 */ -(void)showNoDataImage; /** * 移除無數據頁面 */ -(void)removeNoDataImage; /** * 需要登錄 */ - (void)showShouldLoginPoint; /** * 加載視圖 */ - (void)showLoadingAnimation; /** * 停止加載 */ - (void)stopLoadingAnimation; /** * 分享頁面 * * @param url url * @param title 標題 */ - (void)shareUrl:(NSString *)url andTitle:(NSString *)title; - (void)goLogin; /** * 狀態欄 */ - (void)initStatusBar; - (void)showStatusBarWithTitle:(NSString *)title; - (void)changeStatusBarTitle:(NSString *)title; - (void)hiddenStatusBar;

具體實現方法請看demo

Demo下載
有點亂七八糟,但是你明白我的意思對不?


2016.04.29

  • 關於中文文件夾:咱們提倡使用英文。但是不反對中文.

樓主認為所有便於開發的操作都是可以允許的。

我接手過那么幾個項目,內部蹩腳(或高端)的英文讓我猝不及防。我多想他用的是拼音我就不會理解的那么困難了。

  • 關於pod:demo只是提供了一種思路與步驟,僅做參考。繼承第三方庫方式就看個人了。
  • 關於stroyboad:不利於團隊開發。
    好吧我承認我不會stroyboard和xib。不要打臉...


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM