# Building a WeChat-style iOS app, part 4

Language: en
Canonical: [Building a WeChat-style iOS app, part 4](<https://allluckly.cn/en/journal/2016-02-03-building-a-wechat-style-ios-app-part-4/>)
中文：[中文 HTML](<https://allluckly.cn/%E6%8A%95%E7%A8%BF/tuogao16>)
English: [English HTML](<https://allluckly.cn/en/journal/2016-02-03-building-a-wechat-style-ios-app-part-4/>)
Published: 2016-02-03T00:00:00+08:00
Modified: 2026-10-23T10:34:13+08:00
Author: [WzxJiang](<http://www.jianshu.com/users/389c20d5a244/latest_articles>)

Adds cloud-storage and instant-messaging dependencies, wraps sign-in and registration methods, and records troubleshooting while querying user data. An early experiment with a password field is included as part of this historical development account.

Originally credited to WzxJiang; edited and archived by Bison.

[Source listed in the original](<http://www.jianshu.com/users/389c20d5a244/latest_articles>)

English translation of the [Chinese original](<https://allluckly.cn/%E6%8A%95%E7%A8%BF/tuogao16>), translated on 2026-10-23. The article body uses automated translation; app descriptions, historical reading notes and video subtitles receive separate editorial checks. Original publication dates, attribution, code and version notes are retained.

**Before you read** This technical note dates from 2015–2017. The examples have not been rerun as part of this archive update. Uses the LeanCloud-related APIs available at the time and records an experiment with a custom passWord field. It is not an account-security design.

Editor: [Bison](<https://allluckly.cn/en/>)  
 Contributor: [WzxJiang](<http://www.jianshu.com/users/389c20d5a244/latest_articles>)

> "I only got home today and attended banquets at both lunchtime and evening. LearnCloud also caused real difficulties, so today’s additions are limited:"

![1](<https://allluckly.cn/images/blog/tuogao/tougao12/1.png>)

Completed today

– Import the LeadCloud storage and IM dynamic libraries; I do not know backend development and wanted to try IM.

– Write login and registration methods, beginning the networking work.

For an independent iOS developer without backend experience, LearnCloud is useful: free enough and simple enough. I will skip importing its dynamic libraries and describe my login and registration implementation and the pitfalls encountered.

- Let’s proceed step by step.

Create an NSObject class called WWeChatApi.

- Create a singleton.

```css
+ (WWeChatApi *)giveMeApi
{
    static WWeChatApi * api = nil;
    if (api == nil) {
        api = [[WWeChatApi alloc]init];
    }
    return api;
}
```

- Write the login method first; this is where the pitfall appears.

```
- (void)loginWithUserName:(NSString *)userName andPassWord:(NSString *)passWord andSuccess:(void (^)(id))successBlock andFailure:(void (^)(NSError *))failureBlock
{
    AVQuery *query = [AVQuery queryWithClassName:@"_User"];

    [query whereKey:@"username" equalTo:userName];

    [query whereKey:@"password" equalTo:passWord];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            if (objects.count>0)
            {
                successBlock(nil);
            }
        } else {
            failureBlock(error);
        }
    }];
}
```

AVQuery here is a LearnCloud method; ignore it for now.

![1](<https://allluckly.cn/images/blog/tuogao/tougao16/1.png>)

```
//这里也是LearnCloud动态库的方法
//@"username"的值等于@"WzxJiang" And @"password"的值等于@"123456"
[query whereKey:@"username" equalTo:@“WzxJiang”];

[query whereKey:@"password" equalTo:@"123456"];
```

```
//然后通过这个方法获取符合的AVObject Arr
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        if (objects.count>0)
        {
            successBlock(nil);
        }
    } else {
        failureBlock(error);
    }
}];
```

In principle this should return the one matching AVObject, but it actually returns nothing!

After considerable debugging, I found that

```
[query whereKey:@"password" equalTo:@"123456"]
```

password cannot be queried using whereKey:equalTo here.  
 Surely this is a joke! If the built-in password cannot be checked, how can I validate login?

Solution: add a passWord class in the LearnCloud backend and use it to store the password. The built-in password cannot even be deleted.

- Registration

```
- (void)registerWithUserName:(NSString *)userName andPassWord:(NSString *)passWord andSuccess:(void (^)(id response))successBlock andFailure:(void (^)(NSError * error))failureBlock
{
    AVObject * user = [AVObject objectWithClassName:@"_User"];

    user[@"username"] = userName;

    user[@"password"] = @"123";

    user[@"passWord"] = passWord;

    AVQuery *query = [AVQuery queryWithClassName:@"_User"];

    [query whereKey:@"username" equalTo:userName];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            if (objects.count == 0)
            {
                [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                    if (successBlock)
                    {
                    successBlock(nil);
                    }
                    else
                    {
                    failureBlock(error);
                    }
                }];
            
            }
            else
            {
                failureBlock(nil);
            }
            } else {
                failureBlock(error);
            }
        }];
}
```

Registration has another pitfall here:

```
user[@"password"] = @"123"，password
```

It refuses registration unless a value is supplied.

That is all for today!

Current code, to be updated until the project is complete  
 [GitHub:WzxJiang](<https://github.com/Wzxhaha/WWeChat>)

If you have suggestions or questions, please leave a comment!

---

Recommended reading: [A beginner developer's review of 2015](<https://allluckly.cn/en/journal/2016-02-01-a-beginner-developer-s-review-of-2015/>)

