您的位置:首页 > 数据库

iOS Dev 其实iOS开发很简单之《归属地查询》软件的抛砖引玉(源码+全国号码归属地数据库)

2012-04-21 14:21 357 查看
源码地址http://www.cocoachina.com/bbs/read.php?tid=114580

学习iOS开发有一段时间了,可能很多人也会有一样的想法,究竟自己现在能写出点什么看上去能算是应用的程序,我在左思右想之后,用了8个小时,写了这个归属地查询软件,我想说的是,这个软件从代码上看其实很简单,学了iOS开发一段时间的人都应该能写出来,无非就是从一个textfield接收一个电话号码,之后对电话号码稍加转换,之后是用数据库查询。

在这里呢,我们输入的号码,无非就是几种(可能是客服电话,10XXXX;可能是固话:0XXxxxxxxxx,和0XXXxxxxxxxx,这里我们允许用户只输入区号(0XX,0XXX);可能是移动电话:1XXxxxxxxxx,861XXxxxxxxxx,+861XXxxxxxxxx,这里我们同样允许用户输入电话号码的前7位,即(1XXxxxx,861XXxxxx,+861XXxxxx)),其余输入我们一律视为格式不正确输入,显示内容为:输入的手机号码,归属地,运营商,城市区号;

我们需要一个数据库,如果想做做练习,大家可以自己随便写一个数据库,









这里我会为大家展示一些核心的代码:

第一段代码:拷贝数据库

-(void)DoCopyDatabase{
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentLibraryFolderPath = [documentsDirectory stringByAppendingPathComponent:@"location_Numbercity_citynumber.db"];
if ([[NSFileManager defaultManager] fileExistsAtPath:documentLibraryFolderPath]) {
}else {
NSString *resourceSampleImagesFolderPath =[[NSBundle mainBundle]
pathForResource:@"location_Numbercity_citynumber"
ofType:@"db"];
NSData *mainBundleFile = [NSData dataWithContentsOfFile:resourceSampleImagesFolderPath];
[[NSFileManager defaultManager] createFileAtPath:documentLibraryFolderPath
contents:mainBundleFile
attributes:nil];
}
}
这段代码,在Documents文件夹下没有我们的数据库的时候将数据库拷贝到该位置。

第二段代码:数据的整理操作

- (IBAction)SearchButton:(id)sender {
[textfieldyourphonenumber resignFirstResponder];
mylabelmobile.text = @"";
mylabellocation.text = @"";
mylabelphonenumber.text = @"";
mylabelzonecode.text = @"";

NSString *findPhonenumber = @"";
NSString *findPhoneNumberMobile = @"";
NSString *findPhoneNumberIsACall = @"";
NSString *findPhoneNumberIsMobile = @"";

NSInteger phonenumberlength = [textfieldyourphonenumber.text length];
if (phonenumberlength == 3 ||
phonenumberlength == 4 ||
phonenumberlength == 5 ||
phonenumberlength == 7 ||
phonenumberlength == 11||
phonenumberlength == 12||
phonenumberlength == 13||
phonenumberlength == 14)
{
NSString *tempstring = textfieldyourphonenumber.text;
if ((phonenumberlength == 14) && ([tempstring characterAtIndex:0] == '+') &&([tempstring characterAtIndex:1] == '8')&&([tempstring characterAtIndex:2] == '6')&&([tempstring characterAtIndex:3] == '1'))
{
NSMutableString *tempstring02 = [NSMutableString stringWithString:tempstring];
NSRange range;
range.location = 0;
range.length = 3;
[tempstring02 deleteCharactersInRange:range];
NSString *tempstring03 = [tempstring02 stringByPaddingToLength:7 withString:nil startingAtIndex:0];
NSString *findPhonenumberFull = [tempstring02 stringByPaddingToLength:11 withString:nil startingAtIndex:0];
mylabelphonenumber.text = findPhonenumberFull;
findPhoneNumberMobile = [tempstring02 stringByPaddingToLength:3 withString:nil startingAtIndex:0];
findPhonenumber = tempstring03;
}else if
.
.
.
}else if (((phonenumberlength == 12) && ([tempstring characterAtIndex:0] == '0'))||((phonenumberlength == 4) && ([tempstring characterAtIndex:0] == '0'))) {
NSMutableString *tempstring02 = [NSMutableString stringWithString:tempstring];
mylabelphonenumber.text = tempstring02;
NSMutableString *tempstring03 = [[NSMutableString alloc] initWithCapacity:1];
[tempstring03 appendString:[tempstring02 stringByPaddingToLength:4 withString:nil startingAtIndex:0]];
mylabelzonecode.text = tempstring03;
NSRange range;
range.location = 0;
range.length = 1;
[tempstring03 deleteCharactersInRange:range];
findPhoneNumberIsACall = tempstring03;
}else if
.
.
.
}else if ((phonenumberlength == 5) &&([tempstring characterAtIndex:0] == '1')) {
mylabelphonenumber.text = tempstring;
findPhoneNumberIsMobile = tempstring;
}else {
[self PhoneNumberError];
}
}else {
[self PhoneNumberError];
}
if ([findPhonenumber length] ==7 && [findPhoneNumberMobile length] ==3)
{
[self SelectInfoByPhone:findPhonenumber WithMobile:findPhoneNumberMobile];
}else if ([findPhoneNumberIsACall length] == 3||[findPhoneNumberIsACall length] == 4)
{
[self SelectInfoByCall:findPhoneNumberIsACall];

}else if ([findPhoneNumberIsMobile length] == 5)
{
NSInteger findPhoneNumberIsMobileInt = [findPhoneNumberIsMobile intValue];
[self SelectInfoByPhoneNumberIsMobile:findPhoneNumberIsMobileInt];
}
textfieldyourphonenumber.text = @"";
}
这里我为大家展示了部分对数据操作代码,省略的代码与其他大同小异,所以我们就提供一个特殊的5位,和移动电话,和固话,各一段操作。

第三段代码:查询数据库

-(void)SelectInfoByPhone:(NSString *)phonenumber WithMobile:(NSString *)phonemobile
{
NSString *SelectWhatMobile = @"SELECT mobile FROM numbermobile where uid=";
NSString *SelectWhatMobileFull = [SelectWhatMobile stringByAppendingFormat:phonemobile];
sqlite3 *database;
if (sqlite3_open([[self FindDatabase] UTF8String], &database)
!= SQLITE_OK) {
sqlite3_close(database);
NSAssert(0, @"Failed to open database");
}
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, [SelectWhatMobileFull UTF8String], -1, &stmt, nil) == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW) {
int mobilenumber = sqlite3_column_int(stmt, 0);
if (mobilenumber) {
NSString *mobileNumberString = [NSString stringWithFormat:@"%d",mobilenumber];
NSString *SelectWhatMobileName = @"SELECT mobile FROM mobilenumber WHERE uid=";
NSString *SelectWhatMobileNameFull = [SelectWhatMobileName stringByAppendingFormat:mobileNumberString];
sqlite3_stmt *stmt2;
if (sqlite3_prepare_v2(database, [SelectWhatMobileNameFull UTF8String], -1, &stmt2, nil) == SQLITE_OK) {
while (sqlite3_step(stmt2) == SQLITE_ROW) {
char *mobilename = (char *)sqlite3_column_text(stmt2, 0);
NSString *mobilenamestring = [[NSString alloc] initWithUTF8String:mobilename];
if (mobilenamestring!= NULL) {
mylabelmobile.text = mobilenamestring;
}
}
}sqlite3_finalize(stmt2);

}
}
sqlite3_finalize(stmt);
}
sqlite3_stmt *stmt3;
NSString *SelectCityNumberByPhoneNumber = @"SELECT city FROM phonenumberwithcity WHERE uid=";
NSString *SelectCityNumberByPhoneNumberFull = [SelectCityNumberByPhoneNumber stringByAppendingFormat:phonenumber];
if (sqlite3_prepare_v2(database, [SelectCityNumberByPhoneNumberFull UTF8String], -1, &stmt3, nil) == SQLITE_OK) {
if (sqlite3_step(stmt3) == SQLITE_ROW) {
int citynumber = sqlite3_column_int(stmt3, 0);
NSString *citynumberNSString = [NSString stringWithFormat:@"%d",citynumber];
if (citynumberNSString != nil) {
NSString *SelectCityNameAndCtiyZoneByCityBumber = @"SELECT city,zone FROM citywithnumber WHERE uid=";
NSString *SelectCityNameAndCtiyZoneByCityBumberFull = [SelectCityNameAndCtiyZoneByCityBumber stringByAppendingFormat:citynumberNSString];
sqlite3_stmt *stmt4;
if (sqlite3_prepare_v2(database, [SelectCityNameAndCtiyZoneByCityBumberFull UTF8String], -1, &stmt4, nil) == SQLITE_OK) {
if (sqlite3_step(stmt4) == SQLITE_ROW) {
char *cityname = (char *)sqlite3_column_text(stmt4, 0);
int cityzonecode = sqlite3_column_int(stmt4, 1);
NSString *cityNameNSString = [[NSString alloc] initWithUTF8String:cityname];
NSString *cityzonecodeNnumber = [@"0" stringByAppendingFormat:@"%d",cityzonecode];
if (cityNameNSString != nil && cityzonecodeNnumber != nil) {
mylabellocation.text = cityNameNSString;
mylabelzonecode.text = cityzonecodeNnumber;
}
}else {
[self PhoneNumberError];
}
sqlite3_finalize(stmt4);
}
}
}else {
[self PhoneNumberError];
}
sqlite3_finalize(stmt3);
}

sqlite3_close(database);

}
上面代码,对移动电话的查询方法

同样我们还有-(void)SelectInfoByCall:(NSString *) callnumber和-(void)SelectInfoByPhoneNumberIsMobile:(NSInteger)PhoneNumberIsMobile

在这里我不做太多的讲解,因为在程序内使用的都是特别基础的语法,我们还可以再继续写下去,比如添加读取最近的10条通话,供用户选择等等等等,程序运行图片我会在周一为大家添加上















内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: