- AFN添加网络指示器
- 设置缓存大小 NSURLCache
只缓存get datatask - 发送用户名密码后 返回用户token、过期日期 创建模型接收
发送accesstoken和uid 获取用户列表
//2.是回调地址,截取code
//query是回调URL 问号后面的所有字符串
if let query = request.URL!.query where query.hasPrefix("code=") {
let code = query.substringFromIndex("code=".endIndex)
print(code)
//发送请求 获取accesstoken
NetworkingTools.sharedTools.loadAccessToken(code).subscribeNext({ (result) -> Void in
print(result)
//成功获得accesstoken 和uid
//通过accesstoken和uid 获取用户数据列表
NetworkingTools.sharedTools.loadUserInfo(result["access_token"]as!String, uid: result["uid"]as!String).subscribeNext({ (result) -> Void in
//获得了用户信息 建立用户模型
print(result)
}, error: { (error) -> Void in
print(error)
})
}, error: { (error) -> Void in
print(error)
})
设置过期日期
didset通过uid获取用户头像(avatar)、昵称 并把属性加入用户模型中
loaduserinfodescription
-
用户模型归档
遵守NSCodingfunc saveAccount(){ let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last NSKeyedArchiver.archiveRootObject(self, toFile: path!) } func encodeWithCoder(aCoder: NSCoder) { aCoder.encodeObject(access_token, forKey: "access_token") aCoder.encodeObject(uid, forKey: "uid") aCoder.encodeObject(name, forKey: "name") aCoder.encodeObject(avatar_large, forKey: "avatar_large") } required init?(coder aDecoder: NSCoder) { access_token = aDecoder.decodeObjectForKey("access_token")as?String uid = aDecoder.decodeObjectForKey("uid")as?String name = aDecoder.decodeObjectForKey("name")as?String avatar_large = aDecoder.decodeObjectForKey("avatar_large")as?String }
MVVM封装网络
以前我们获取网络数据 都是在viewcontroller中进行,在MVVM中,viewcontroller不能直接访问模型数据,需要通过Viewmodel-
加载用户信息 判断过期
在accountViewModel创建类方法 从沙盒中获取用户PLIST,判断是否有accesstokenprivate static var account:UserAccount? class func loadAccount()->UserAccount? { if account == nil{ let path = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last!.stringByAppendingString("/account.plist") account = NSKeyedUnarchiver.unarchiveObjectWithFile(path)as?UserAccount } return account }
-
新特性
创建nwefeatureController继承collectionViewController
1.init 流水布局init(){ super.init(collectionViewLayout: UICollectionViewFlowLayout()) }
2.collectionView代理
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(DVCollectionCell, forIndexPath: indexPath)as!newFeatureCell
// cell.frame = view.bounds
cell.imageIndex = indexPath.item
cell.backgroundColor = (indexPath.row % 2==0) ? UIColor.redColor() : UIColor.grayColor()
return cell
}
3.注册cell
private func setupUI(){
let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
layout.itemSize = view.bounds.size
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
collectionView?.pagingEnabled = true
collectionView?.bounces = false
collectionView?.showsHorizontalScrollIndicator = false
}
-
自定义cell
private class newFeatureCell : UICollectionViewCell{ var imageIndex = 0{ didSet{ imageView.image = UIImage(named: "new_feature_\(imageIndex+1)") } } lazy var imageView = UIImageView() override init(frame: CGRect) { super.init(frame: frame) addSubview(imageView) imageView.frame = bounds }
-
显示登入按钮
func showBTN(){ button.hidden = false button.userInteractionEnabled = false button.transform = CGAffineTransformMakeScale(0, 0) UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 8, options: [], animations: { () -> Void in self.button.transform = CGAffineTransformIdentity }) { (_) -> Void in button.userInteractionEnabled = true } }