Iphone development my Sina Weibo client-User Logon preparation (1.1)

Source: Internet
Author: User
Tags oauth sqlite manager

First, let's talk about how to implement this. you can log on to multiple accounts, that is, you can save multiple Weibo accounts and choose one of them to log on. Multiple account information is stored in the sqlite database. Each account information is a record. When a user starts a Weibo client, the account information stored in the sqlite database is obtained, the list is displayed on the page. You can click an account to enter Weibo, if you check that no account in the sqlite database is available when you start the Weibo client, the program automatically displays the user authorization authentication page. This client is implemented based on oauth authentication, therefore, authorization authentication is required before using the new Weibo account, an account only needs to perform authorization authentication for the first time and then saves user_id, Access Token, Access Secret, user name and Avatar icons together to the sqlite database, you do not need to enter your account and password at next login. You can directly click the selected user icon on the page to go to Weibo. I will not explain the Access Token and Access Secret. If you don't understand it, You can google the oauth knowledge, in fact, I wrote an article about oauth in detail when I was a Weibo client of the android version.

From the above ideas, there are two possibilities for user login. The first type of database does not have any account information; the second type of database already contains one or more account information. The two cases show different user interfaces. The implementation process of the two cases is described below.

The first implementation process: 1. query sqlite database --> 2. the prompt information page for authorization authentication is displayed without an account record (6) --> 3. click Start in the previous step to go to the user authorization page --> 4. enter your Weibo account and password and click OK to close the user authorization page (7) --> 5. the program obtains the information of this account according to the user's authorization and saves it to the sqlite database --> 6. displays the user logon selection interface. The default account is the account you just authorized.

The second implementation process: 1. query sqlite database --> 2. the existing account record obtains all account information and displays the user logon selection interface --> 3. read the account of the Last Logon As the Default User display on the interface (for example, 2) --> 4. click the Add button to display the account addition page (for example, 4) --> 5. click the switch button to display the account selection list page (for example, 3) --> 6. click Delete to display the account deletion confirmation page (for example, 5)

The main knowledge points involved in the above implementation process are as follows: 1. sqlite database operations (create databases, create data tables, insert data records, read data records); 2. oauth authorization authentication (the sdk has implemented the call of relevant methods ); 3. NSUserDefaults access (used to record the Last Logon account)

Now we are officially starting to make some preparations:

1. To the http://code.google.com/p/minblog4sina/ (for this can see: iphone development my Sina Weibo client-the beginning) to my sdk project source code checkout local, and then use xcode to open this project, develop Weibo client applications based on this sdk project.

2. design the sqlite database to save user account information. I used a plug-in named SQLite Manager in firefox to create the sqlite database, this is very simple to use and I don't know how to use it when I install it. I used it to create a project named weibo. sqlite database, and then this database creates a table named loginUser to save account information, loginUser table, this table contains five fields to save user_id, account nickname, Access Token, Access Secret, and account icons, for example:

3. Open the project with xcode, and add the weibo. sqlite file completed in the previous step to the Resources folder of the project.

4. In the project, add a class named Objective-C class named Sqlite to create the sqlite library, read records, and add records, right-click Frameworks on the left side of Xcode and add the Framework named libsqlite3.dylib. This class library encapsulates the operations of the sqlite library.

5. Open Sqlite. h Add the following code. In this file, first import sqlite3.h, which is the libsqlite3.dylib class library in Frameworks, and then import the User. h. This is the user account object class in the sdk of MinBlog4Sina. The declared four methods are database initialization, getting the user record list, adding user records, and deleting user records.


# Import <Foundation/Foundation. h>

# Import <sqlite3.h>

# Import "User. h"

@ Interface Sqlite: NSObject {

Sqlite3 * database;

}

-(Sqlite *) init;

-(NSMutableArray *) getUserList;

-(BOOL) addUser :( User *) user;

-(BOOL) delUser :( NSString *) name;

@ End

6. Open Sqlite. m adds the following code. This file implements the above several methods. The so-called database creation is to add the third step to the project's weibo. the sqlite file is copied to the directory of the software and copied in the-(Sqlite *) init method below. In addition, the user records are read and added to the BLOB-type data processing. The image data is stored here, and the image is converted to the bytes type when added, converts the bytes type into an image during reading.

# Import "Sqlite. h"

@ Interface Sqlite (private)

-(Void) createDatabaseIfNeeded :( NSString *) filename;-(NSString *) dataFilePath;

-(BOOL) openDatabase;

-(Void) closeDatabase;

@ End

@ Implementation Sqlite

// Initialize the database

-(Sqlite *) init {if (self = [super init])

{

[Self createDatabaseIfNeeded: @ "weibo. sqlite"];

}

Return self;

}

-(BOOL) delUser :( NSString *) name {BOOL sucess = [self openDatabase];

If (sucess)

{

NSString * SQL = [[NSString alloc] initWithFormat: @ "delete from loginUser where screen_name = % @", name];

Char * errorMsg;

If (sqlite3_exec (database, [SQL UTF8String], NULL, NULL, & errorMsg )! = SQLITE_ OK)

{

NSLog (@ "del User error: % s", errorMsg );

Sqlite3_free (errorMsg );

}

Else {

}

[SQL release];

}

[Self closeDatabase];

Return sucess;

}

// Add User record-(BOOL) addUser :( user *) User

{

BOOL sucess = [self openDatabase];

If (sucess)

{

NSString * SQL = [[NSString alloc] initWithFormat: @ "insert into loginUser (user_id, screen_name, key, secret, icon) values (?,?,?,?,?) "];

Sqlite3_stmt * statement;

If (sqlite3_prepare_v2 (database, [SQL UTF8String],-1, & statement, nil) = SQLITE_ OK ){

NSString * uid = [NSString stringWithFor

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.