iOS開發(fā)之《歸屬地查詢》軟件的拋磚引玉
學(xué)習(xí)iOS開發(fā)有一段時(shí)間了,可能很多人也會(huì)有一樣的想法,究竟自己現(xiàn)在能寫出點(diǎn)什么看上去能算是應(yīng)用的程序,我在左思右想之后,用了8個(gè)小時(shí),寫了這個(gè)歸屬地查詢軟件,我想說(shuō)的是,這個(gè)軟件從代碼上看其實(shí)很簡(jiǎn)單,學(xué)了iOS開發(fā)一段時(shí)間的人都應(yīng)該能寫出來(lái),無(wú)非就是從一個(gè)textfield接收一個(gè)電話號(hào)碼,之后對(duì)電話號(hào)碼稍加轉(zhuǎn)換,之后是用數(shù)據(jù)庫(kù)查詢。
在這里呢,我們輸入的號(hào)碼,無(wú)非就是幾種(可能是客服電話,10XXXX;可能是固話:0XXxxxxxxxx,和0XXXxxxxxxxx,這里我們?cè)试S用戶只輸入?yún)^(qū)號(hào)(0XX,0XXX);可能是移動(dòng)電話:1XXxxxxxxxx,861XXxxxxxxxx,+861XXxxxxxxxx,這里我們同樣允許用戶輸入電話號(hào)碼的前7位,即(1XXxxxx,861XXxxxx,+861XXxxxx)),其余輸入我們一律視為格式不正確輸入,顯示內(nèi)容為:輸入的手機(jī)號(hào)碼,歸屬地,運(yùn)營(yíng)商,城市區(qū)號(hào);
我們需要一個(gè)數(shù)據(jù)庫(kù),如果想做做練習(xí),大家可以自己隨便寫一個(gè)數(shù)據(jù)庫(kù),
這里我會(huì)為大家展示一些核心的代碼:
***段代碼:拷貝數(shù)據(jù)庫(kù)
- -(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文件夾下沒有我們的數(shù)據(jù)庫(kù)的時(shí)候?qū)?shù)據(jù)庫(kù)拷貝到該位置。
第二段代碼:數(shù)據(jù)的整理操作
- - (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 = @"";
- }
這里我為大家展示了部分對(duì)數(shù)據(jù)操作代碼,省略的代碼與其他大同小異,所以我們就提供一個(gè)特殊的5位,和移動(dòng)電話,和固話,各一段操作。
第三段代碼:查詢數(shù)據(jù)庫(kù)
- -(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);
- }
上面代碼,對(duì)移動(dòng)電話的查詢方法
同樣我們還有-(void)SelectInfoByCall:(NSString *) callnumber和-(void)SelectInfoByPhoneNumberIsMobile:(NSInteger)PhoneNumberIsMobile
在這里我不做太多的講解,因?yàn)樵诔绦騼?nèi)使用的都是特別基礎(chǔ)的語(yǔ)法,我們還可以再繼續(xù)寫下去,比如添加讀取最近的10條通話,供用戶選擇等等等等,程序運(yùn)行圖片我會(huì)在周一為大家添加上