從頭開始寫一款開源app上線,相互學(xué)習(xí)(一)

前言

最近比較閑,在聚合數(shù)據(jù)上找了個(gè)開放的api,加上swift的語法更新,就想著用swift3.0寫個(gè)新聞?lì)惖腶pp,想把過程記錄下來,一是回顧與鞏固,二是拋磚引玉.

首先,先明確目的: 做一個(gè)簡單的新聞?lì)愘Y訊的App
數(shù)據(jù)來源: 聚合api,有許多開放的api,接口比較簡單
語言: swift3.0,我也是邊寫邊查資料的,有錯(cuò)誤的地方希望大家指出,互相學(xué)習(xí)
系統(tǒng): iOS
Github地址: TopOmnibus
AppStore下載:新聞巴士

工程搭建

二話不說,祭出Xcode8,新建工程,選擇swift
使用cocoapods,新建Podfile,添加需要的第三方庫

platform :ios, '8.0'
use_frameworks!

target 'TopOmnibus' do

pod 'SnapKit' , '~> 3.0.2'             //跟Masonry一樣,用于自動(dòng)布局
pod 'AFNetworking'                     //網(wǎng)絡(luò)請求
pod 'SDWebImage'                      //圖片加載 
pod 'MJRefresh'                           //下拉刷新 
pod 'AXWebViewController'        //與微信相同的web導(dǎo)航效果

end

網(wǎng)絡(luò)層

網(wǎng)絡(luò)層是在基于AFNetworkKit的基礎(chǔ)上,我稍微封裝了一下, 項(xiàng)目中一般只用到GET和POST兩種請求,并且添加了聯(lián)網(wǎng)指示器
大致步驟是新建一個(gè)OYHTTPSessionManager單例類,繼承自AFHTTPSessionManager(這個(gè)并不是單例類,每次請求都會(huì)生成一個(gè)新的對(duì)象), 代碼如下:

/// GET/POST請求
    func request(type: RequestType, URLString: String, parameters: Any?, progress: ((Progress) -> Void)?, success: ((URLSessionDataTask, Any?) -> Void)?, failure: ((URLSessionDataTask?, Error) -> Void)?) -> Void {
        /// 封閉聯(lián)網(wǎng)指示器指示
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
        let success1 = {
            (task: URLSessionDataTask, result: Any?) in
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            success?(task, result)
        }
        let failure1 = {
            (task: URLSessionDataTask?, error: Error) in
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            failure?(task, error)
        }
        if type == .GET {
            self.get(URLString, parameters: parameters, progress: progress, success: success1, failure: failure1)
        }else if type == .POST {
            self.post(URLString, parameters: parameters, progress: progress, success: success1, failure: failure1)
        }
    }

因?yàn)锳FNetworking等第三方框架是用OC寫的,在swift中使用需要添加橋接文件,做法很簡單,請參考:Swift與OC混編

整體框架

框架圖

對(duì)于NavigationController, 我使用了子類,繼承自UINavigationController, 主要用來解決:
1.push時(shí)隱藏tabBar

重寫pushViewController方法,push時(shí)隱藏tabBar
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if self.childViewControllers.count != 0 {
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: animated)
    }

2.自定義NavigationBar的樣式

在viewDidLoad中修改navigationBar的樣式
override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.white
        navigationBar.barTintColor = mainRedColor
        navigationBar.tintColor = UIColor.white
        navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
}

好了, 項(xiàng)目初始化已經(jīng)完成,接下來的工作是進(jìn)行界面的搭建,請?zhí)D(zhuǎn)到從頭開始寫一款開源app上線,相互學(xué)習(xí)(二)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容