【好书】项目学习笔记 1

课程网址 http://www.huachuanxueyuan.com/course/4

1.集成 Learn Cloud

不同于视频教程,我直接使用了CocoaPods

在项目目录下新建Podfile,并添加如下内容:

pod 'AVOSCloud'               # 数据存储、短信、云引擎调用等基础服务模块
pod 'AVOSCloudIM'             # 实时通信模块
pod 'AVOSCloudCrashReporting' # 错误报告模块

然后执行pod install,会有很长的安装时间,结束后就已经集成好LearnCloud的SDK了。

2.CocoaPods问题解决

发现卡在 Pod install,老是提示 Updating local specs repositories,闭着眼睛也能想到是GFW搞得👻。

我的建议是首先重装下CocoaPods,

sudo gem install cocoapods

然后执行,

pod setup --verbose

Cocoapods 会执行访问 https://github.com/CocoaPods/Specs.git ,拉取它的库文件,按 stackoverflow 上的一个回答,在2016年10月时,库的大小有841MB,所以Down下来需要很长时间。

3.LeanCloud初始化

新建头文件 bridging.h ,填入

#import <AVOSCloud/AVOSCloud.h>

在Project的Bulid Settings中搜索Objective-C Bridging Header,在后面的值填上XBook/bridging.h.

原因是LeanCloud的SDK目前是用 Objective-C 编译的,所以需要桥接。

打卡AppDelegate.swift文件,application:didFinishLaunchingWithOptions:方法中,填入

AVOSCloud.setApplicationId("App ID", clientKey: "App Key")
AVAnalytics.trackAppOpened(launchOptions: launchOptions)

OK,Leancloud的初始化完成了。

最后可以在viewDidLoad这样的方法里写入

let testObject = AVObject(className: "TestObject")
testObject.setObject("bar", forKey: "foo")
testObject.save()

执行一次模拟器后,看应用后台是不是增加了testObject数据。

4.用代码写TabBar

既然是纯代码实现UI,所以第一件要做的是删掉"Main.storyboard"。

但要注意在Info.plist文件中,对于Main storyboard file base name这一栏的数值要同步清空,不然项目会因为找不到StoryBoard文件而崩溃。

构建UI的主要代码如下:

//构建UI
        self.window = UIWindow(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        let tabBarController = UITabBarController()
        
        let rankController = UINavigationController(rootViewController: rankViewController())
        let searchController = UINavigationController(rootViewController: searchViewController())
        let pushController = UINavigationController(rootViewController: pushViewController())
        let circleController = UINavigationController(rootViewController: circleViewController())
        let moreController = UINavigationController(rootViewController: moreViewController())
        
        tabBarController.viewControllers = [rankController, searchController, pushController, circleController, moreController]
        
        let tabBarItem1 = UITabBarItem(title: "排行榜", image: UIImage(named: "bio"), selectedImage: UIImage(named: "bio_red"))
        let tabBarItem2 = UITabBarItem(title: "发现", image: UIImage(named: "timer 2"), selectedImage: UIImage(named: "timer 2_red"))
        let tabBarItem3 = UITabBarItem(title: "", image: UIImage(named: "pencil"), selectedImage: UIImage(named: "pencil_red"))
        let tabBarItem4 = UITabBarItem(title: "圈子", image: UIImage(named: "bio"), selectedImage: UIImage(named: "bio_red"))
        let tabBarItem5 = UITabBarItem(title: "更多", image: UIImage(named: "users two-2"), selectedImage: UIImage(named: "users two-2_red"))
        
        rankController.tabBarItem = tabBarItem1
        searchController.tabBarItem = tabBarItem2
        pushController.tabBarItem = tabBarItem3
        circleController.tabBarItem = tabBarItem4
        moreController.tabBarItem = tabBarItem5
        
        //设置颜色
        rankController.tabBarController?.tabBar.tintColor = MAIN_RED
        
        //显示界面
        self.window?.rootViewController = tabBarController
        self.window?.makeKeyAndVisible()
Comments
Write a Comment