自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS開發(fā)之《歸屬地查詢》軟件的拋磚引玉

移動(dòng)開發(fā) iOS
學(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ù)查詢。

學(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ù)

 

  1. -(void)DoCopyDatabase{ 
  2.     NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,NSUserDomainMask, YES);  
  3.     NSString *documentsDirectory = [paths objectAtIndex:0];      
  4.     NSString *documentLibraryFolderPath = [documentsDirectory stringByAppendingPathComponent:@"location_Numbercity_citynumber.db"];  
  5.     if ([[NSFileManager defaultManager] fileExistsAtPath:documentLibraryFolderPath]) {  
  6.     }else {  
  7.         NSString *resourceSampleImagesFolderPath =[[NSBundle mainBundle]  
  8.                                                    pathForResource:@"location_Numbercity_citynumber"  
  9.                                                    ofType:@"db"];  
  10.         NSData *mainBundleFile = [NSData dataWithContentsOfFile:resourceSampleImagesFolderPath];  
  11.         [[NSFileManager defaultManager] createFileAtPath:documentLibraryFolderPath  
  12.                                                 contents:mainBundleFile  
  13.                                               attributes:nil]; 
  14.     } 

這段代碼,在Documents文件夾下沒有我們的數(shù)據(jù)庫(kù)的時(shí)候?qū)?shù)據(jù)庫(kù)拷貝到該位置。

第二段代碼:數(shù)據(jù)的整理操作

 

  1. - (IBAction)SearchButton:(id)sender { 
  2.     [textfieldyourphonenumber resignFirstResponder]; 
  3.     mylabelmobile.text = @""
  4.     mylabellocation.text = @""
  5.     mylabelphonenumber.text = @""
  6.     mylabelzonecode.text = @""
  7.      
  8.     NSString *findPhonenumber = @""
  9.     NSString *findPhoneNumberMobile = @""
  10.     NSString *findPhoneNumberIsACall = @""
  11.     NSString *findPhoneNumberIsMobile = @""
  12.      
  13.     NSInteger phonenumberlength = [textfieldyourphonenumber.text length]; 
  14.     if (phonenumberlength == 3 || 
  15.         phonenumberlength == 4 || 
  16.         phonenumberlength == 5 || 
  17.         phonenumberlength == 7 || 
  18.         phonenumberlength == 11||  
  19.         phonenumberlength == 12|| 
  20.         phonenumberlength == 13|| 
  21.         phonenumberlength == 14)  
  22.     { 
  23.         NSString *tempstring = textfieldyourphonenumber.text; 
  24.         if ((phonenumberlength == 14) && ([tempstring characterAtIndex:0] == '+') &&([tempstring characterAtIndex:1] == '8')&&([tempstring characterAtIndex:2] == '6')&&([tempstring characterAtIndex:3] == '1'))  
  25.         { 
  26.             NSMutableString *tempstring02 = [NSMutableString stringWithString:tempstring]; 
  27.             NSRange range; 
  28.             range.location = 0; 
  29.             range.length = 3; 
  30.             [tempstring02 deleteCharactersInRange:range]; 
  31.             NSString *tempstring03 = [tempstring02 stringByPaddingToLength:7 withString:nil startingAtIndex:0]; 
  32.             NSString *findPhonenumberFull = [tempstring02 stringByPaddingToLength:11 withString:nil startingAtIndex:0]; 
  33.             mylabelphonenumber.text = findPhonenumberFull; 
  34.             findPhoneNumberMobile = [tempstring02 stringByPaddingToLength:3 withString:nil startingAtIndex:0]; 
  35.             findPhonenumber = tempstring03; 
  36.         }else if 
  37. }else if (((phonenumberlength == 12) && ([tempstring characterAtIndex:0] == '0'))||((phonenumberlength == 4) && ([tempstring characterAtIndex:0] == '0'))) { 
  38.             NSMutableString *tempstring02 = [NSMutableString stringWithString:tempstring]; 
  39.             mylabelphonenumber.text = tempstring02; 
  40.             NSMutableString *tempstring03 = [[NSMutableString alloc] initWithCapacity:1]; 
  41.             [tempstring03 appendString:[tempstring02 stringByPaddingToLength:4 withString:nil startingAtIndex:0]]; 
  42.             mylabelzonecode.text = tempstring03; 
  43.             NSRange range; 
  44.             range.location = 0; 
  45.             range.length = 1; 
  46.             [tempstring03 deleteCharactersInRange:range]; 
  47.             findPhoneNumberIsACall = tempstring03; 
  48.         }else if 
  49. }else if ((phonenumberlength == 5) &&([tempstring characterAtIndex:0] == '1')) { 
  50.             mylabelphonenumber.text = tempstring; 
  51.             findPhoneNumberIsMobile = tempstring; 
  52.         }else { 
  53.             [self PhoneNumberError]; 
  54.         } 
  55.     }else { 
  56.         [self PhoneNumberError]; 
  57.     } 
  58.     if ([findPhonenumber length] ==7 && [findPhoneNumberMobile length] ==3)  
  59.     { 
  60.         [self SelectInfoByPhone:findPhonenumber WithMobile:findPhoneNumberMobile]; 
  61.     }else if ([findPhoneNumberIsACall length] == 3||[findPhoneNumberIsACall length] == 4) 
  62.     { 
  63.         [self SelectInfoByCall:findPhoneNumberIsACall]; 
  64.  
  65.     }else if ([findPhoneNumberIsMobile length] == 5) 
  66.     { 
  67.         NSInteger findPhoneNumberIsMobileInt = [findPhoneNumberIsMobile intValue]; 
  68.         [self SelectInfoByPhoneNumberIsMobile:findPhoneNumberIsMobileInt]; 
  69.     } 
  70.     textfieldyourphonenumber.text = @""

這里我為大家展示了部分對(duì)數(shù)據(jù)操作代碼,省略的代碼與其他大同小異,所以我們就提供一個(gè)特殊的5位,和移動(dòng)電話,和固話,各一段操作。

第三段代碼:查詢數(shù)據(jù)庫(kù)

 

  1. -(void)SelectInfoByPhone:(NSString *)phonenumber WithMobile:(NSString *)phonemobile 
  2.     NSString *SelectWhatMobile = @"SELECT mobile FROM numbermobile where uid="
  3.     NSString *SelectWhatMobileFull = [SelectWhatMobile stringByAppendingFormat:phonemobile]; 
  4.     sqlite3 *database; 
  5.     if (sqlite3_open([[self FindDatabase] UTF8String], &database) 
  6.         != SQLITE_OK) { 
  7.         sqlite3_close(database); 
  8.         NSAssert(0, @"Failed to open database"); 
  9.     } 
  10.     sqlite3_stmt *stmt; 
  11.     if (sqlite3_prepare_v2(database, [SelectWhatMobileFull UTF8String], -1, &stmt, nil) == SQLITE_OK) { 
  12.         while (sqlite3_step(stmt) == SQLITE_ROW) { 
  13.             int mobilenumber = sqlite3_column_int(stmt, 0); 
  14.             if (mobilenumber) { 
  15.                 NSString *mobileNumberString = [NSString stringWithFormat:@"%d",mobilenumber]; 
  16.                 NSString *SelectWhatMobileName = @"SELECT mobile FROM mobilenumber WHERE uid="
  17.                 NSString *SelectWhatMobileNameFull = [SelectWhatMobileName stringByAppendingFormat:mobileNumberString]; 
  18.                 sqlite3_stmt *stmt2; 
  19.                 if (sqlite3_prepare_v2(database, [SelectWhatMobileNameFull UTF8String], -1, &stmt2, nil) == SQLITE_OK) { 
  20.                     while (sqlite3_step(stmt2) == SQLITE_ROW) { 
  21.                         char *mobilename = (char *)sqlite3_column_text(stmt2, 0); 
  22.                         NSString *mobilenamestring = [[NSString alloc] initWithUTF8String:mobilename]; 
  23.                         if (mobilenamestring!= NULL) { 
  24.                             mylabelmobile.text = mobilenamestring; 
  25.                         } 
  26.                     } 
  27.                 }sqlite3_finalize(stmt2); 
  28.                  
  29.             } 
  30.         } 
  31.         sqlite3_finalize(stmt); 
  32.     } 
  33.     sqlite3_stmt *stmt3; 
  34.     NSString *SelectCityNumberByPhoneNumber = @"SELECT city FROM phonenumberwithcity WHERE uid="
  35.     NSString *SelectCityNumberByPhoneNumberFull = [SelectCityNumberByPhoneNumber stringByAppendingFormat:phonenumber]; 
  36.     if (sqlite3_prepare_v2(database, [SelectCityNumberByPhoneNumberFull UTF8String], -1, &stmt3, nil) == SQLITE_OK) { 
  37.         if (sqlite3_step(stmt3) == SQLITE_ROW) { 
  38.             int citynumber = sqlite3_column_int(stmt3, 0); 
  39.             NSString *citynumberNSString = [NSString stringWithFormat:@"%d",citynumber];  
  40.             if (citynumberNSString != nil) { 
  41.                 NSString *SelectCityNameAndCtiyZoneByCityBumber = @"SELECT city,zone FROM citywithnumber WHERE uid="
  42.                 NSString *SelectCityNameAndCtiyZoneByCityBumberFull = [SelectCityNameAndCtiyZoneByCityBumber stringByAppendingFormat:citynumberNSString]; 
  43.                 sqlite3_stmt *stmt4; 
  44.                 if (sqlite3_prepare_v2(database, [SelectCityNameAndCtiyZoneByCityBumberFull UTF8String], -1, &stmt4, nil) == SQLITE_OK) { 
  45.                     if (sqlite3_step(stmt4) == SQLITE_ROW) { 
  46.                         char *cityname = (char *)sqlite3_column_text(stmt4, 0); 
  47.                         int cityzonecode = sqlite3_column_int(stmt4, 1); 
  48.                         NSString *cityNameNSString = [[NSString alloc] initWithUTF8String:cityname]; 
  49.                         NSString *cityzonecodeNnumber = [@"0" stringByAppendingFormat:@"%d",cityzonecode]; 
  50.                         if (cityNameNSString != nil && cityzonecodeNnumber != nil) { 
  51.                             mylabellocation.text = cityNameNSString; 
  52.                             mylabelzonecode.text = cityzonecodeNnumber; 
  53.                         } 
  54.                     }else { 
  55.                         [self PhoneNumberError]; 
  56.                     } 
  57.                     sqlite3_finalize(stmt4); 
  58.                 } 
  59.             } 
  60.         }else { 
  61.             [self PhoneNumberError]; 
  62.         } 
  63.         sqlite3_finalize(stmt3); 
  64.     } 
  65.      
  66.     sqlite3_close(database); 
  67.      
  68.      
  69.      

上面代碼,對(duì)移動(dòng)電話的查詢方法

同樣我們還有-(void)SelectInfoByCall:(NSString *) callnumber和-(void)SelectInfoByPhoneNumberIsMobile:(NSInteger)PhoneNumberIsMobile

在這里我不做太多的講解,因?yàn)樵诔绦騼?nèi)使用的都是特別基礎(chǔ)的語(yǔ)法,我們還可以再繼續(xù)寫下去,比如添加讀取最近的10條通話,供用戶選擇等等等等,程序運(yùn)行圖片我會(huì)在周一為大家添加上

責(zé)任編輯:張葉青 來(lái)源: 開源社區(qū)
相關(guān)推薦

2010-07-01 09:38:31

jQuery

2016-12-02 19:14:16

數(shù)據(jù)科學(xué)大數(shù)據(jù)

2009-06-26 15:56:26

2015-10-15 09:33:55

3Dtouch蘋果游戲開發(fā)

2023-09-14 15:37:11

2011-03-11 15:31:52

數(shù)據(jù)中心虛擬接入

2010-04-12 16:24:15

Oracle表查詢

2010-06-18 13:56:10

SQL Server日

2009-05-18 10:57:35

.NETString特性

2009-10-09 14:24:27

2014-06-17 09:42:05

軟件開發(fā)軟件設(shè)計(jì)

2022-07-20 15:10:38

Docker大數(shù)據(jù)平臺(tái)

2009-08-24 09:55:24

C#集成開發(fā)環(huán)境

2010-10-26 09:23:03

Web Service

2012-06-12 09:53:14

HTML5

2009-07-01 09:41:00

無(wú)線路由器固件番茄固件

2016-10-14 13:10:35

2010-07-23 16:03:36

SQL Server存

2011-08-10 10:06:54

存儲(chǔ)過(guò)程SQL Server IP地址歸屬地查詢

2010-07-13 15:29:24

SQL Server
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)