搭建iOS項目基本框架-傾城孤月

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

1. 新建工程

1.1 刪除沒必要要類目

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

刪除選中的三項

1.2 修改info.plist

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


 
main.storyboard

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

開啓http

3.添加應用白名單
iOS9以後分享等必須添加白名單後才能夠打開相關應用。字段值爲LSApplicationQueriesSchemesxcode

  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驗證碼服務app

均根據官方文檔安裝ide

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;
相關文章
相關標籤/搜索