极客班项目:蓦然

遇到的点

  • 参考微信朋友圈,发布新照片后,动态插入原有的朋友圈顶部, 我的理解是存入本地缓存数据库,后台异步上次到服务器.

源码见 GitHub:https://github.com/gewill/Mo-Ran

v0.1 b1

date: 2015.09.20

  • 发布照片按钮 和 发布照片页面
  • 复用先峰小组的 login.StoryBoard
  • AppDelegate 分别实现:跳转登陆和主界面方面

登陆按钮

使用 AFNetworking 的 AFHTTPRequestOperationManager
- POST:parameters:success:failure: 方法,提交 json
格式数据,收到成功或者失败的数据 json。具体格式参考文档。

主要是处理 AFHTTPRequestOperation 的 responseObject 的 json 数据。

遇到网络问题,直接想到 Google,但是搜索的不得要领,无果。最后想到 Charles 网络调试, Request 和 Response 一目了然。非常棒的工具。然后认真查看 API 相关参数的类型的:属性和方法。

成功

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"status": 1,
"data": {
"user_id": "2",
"user_name": "testProjectNew1",
"token": "cec5a0a5e7a1951c87099b96d16851bf6f9f0a21",
"avatar": "",
"project_id": "1",
"last_login": "2015-08-30 17:35:49",
"login_times": 3
},
"message": "Login success"
}

失败

1
2
3
4
5
{
"status": 0,
"error_code": 400,
"message": "No such user"
}

登陆按钮

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#pragma mark - 登陆按钮
- (IBAction)login:(UIButton *)sender {

NSString *url = @"http://moran.chinacloudapp.cn/moran/web/user/login";

NSDictionary *patameters = @{
@"email" : self.email.text,
@"password" : self.password.text,
@"gdid" : @"Ge Will"
};

AFHTTPRequestOperationManager *manager =
[AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager POST:url
parameters:patameters
success:^(AFHTTPRequestOperation *_Nonnull operation,
id _Nonnull responseObject) {
NSLog(@"Login success:%@, message:%@", responseObject,
[responseObject objectForKey:@"message"]);

if ([[responseObject objectForKey:@"status"] isEqual:@"1"]) {

// 1 - 获取用户信息
[GWUser shareUser].userID =
[[responseObject objectForKey:@"data"] objectForKey:@"user_id"];

// 2 - 保存用户相关信息到 NSUserDefaults
[[NSUserDefaults standardUserDefaults]
setObject:[GWUser shareUser].userID
forKey:@"userID"];
[[NSUserDefaults standardUserDefaults] synchronize];

// 3 - 视图跳转到 Main.storyboard 主界面
AppDelegate *appDelegate =
(AppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate loadMainView];
}

}
failure:^(AFHTTPRequestOperation *_Nonnull operation,
NSError *_Nonnull error) {
NSLog(@"Login error:%@", error);
NSLog(@"%@", operation.responseObject);

__unused NSString *status =
[operation.responseObject objectForKey:@"status"];
__unused NSString *errorCode =
[operation.responseObject objectForKey:@"error_code"];
NSString *message = [operation.responseObject objectForKey:@"message"];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:message
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];

}];
}

if ([[responseObject objectForKey:@"status"] isEqual:@"1"])改为
if ([[responseObject objectForKey:@"status"] intValue] == 1)

v0.2 b2

date: 2015-10-11

Cocoapods改为本地添加,网络不稳定也是无奈。