标签:style blog http color io os ar for strong
首先是一个库:
然后设置一个工具类
.h
1 @interface HMFoundLocation : NSObject 2 3 AS_SINGLETON(HMFoundLocation) 4 5 @property(nonatomic,strong)NSString *address; 6 7 @property(nonatomic,strong)NSString *operater; 8 9 // 设置归属地 和 运营商两个属性 10 - (void)setAddressAndOperater:(NSString *)telephoneNumber; 11 12 @end
.m
1 // 2 // HMFoundLocation.m 3 // contactModel 4 // 5 // Created by mac_mini on 14-10-24. 6 // Copyright (c) 2014年 mac_mini. All rights reserved. 7 // 8 9 #import "HMFoundLocation.h" 10 #import <sqlite3.h> 11 12 @implementation HMFoundLocation 13 14 DEF_SINGLETON(HMFoundLocation) 15 16 - (void)setAddressAndOperater:(NSString *)telephoneNumber{ 17 NSInteger lengthOftel = [telephoneNumber length]; 18 if (lengthOftel == 5) { 19 [self _doAsServiceNumber:telephoneNumber]; 20 } else if ((lengthOftel == 11) && ([telephoneNumber characterAtIndex:0] == ‘1‘)) { 21 // 11位手机号 不含+86 22 NSString *findNumber = [telephoneNumber stringByPaddingToLength:7 withString:nil startingAtIndex:0]; 23 NSString *findNumberMobile = [telephoneNumber stringByPaddingToLength:3 withString:nil startingAtIndex:0]; 24 [self _selectInfoByPhone:findNumber WithMobile:findNumberMobile]; 25 } else if ((lengthOftel == 11) && ([telephoneNumber characterAtIndex:0] == ‘0‘)) { 26 // 固话以零开头 区号为3位 27 NSString *zoneNumber = [telephoneNumber stringByPaddingToLength:3 withString:nil startingAtIndex:0]; 28 NSRange range; 29 range.location = 0; 30 range.length = 1; 31 [self _doAsLinePhone:zoneNumber]; 32 self.operater = @"固话"; 33 } else if (((lengthOftel == 12) && ([telephoneNumber characterAtIndex:0] == ‘0‘)) || ((lengthOftel == 3) && ([telephoneNumber characterAtIndex:0] == ‘0‘))) { 34 // 固话以零开头 区号为4位 35 NSString *zoneNumber = [telephoneNumber stringByPaddingToLength:4 withString:nil startingAtIndex:0]; 36 NSRange range; 37 range.location = 0; 38 range.length = 1; 39 [self _doAsLinePhone:zoneNumber]; 40 self.operater = @"固话"; 41 } 42 else{ 43 [self _numberUnknown]; 44 } 45 } 46 47 - (void)_selectInfoByPhone:(NSString *)findNumber WithMobile:(NSString *)findNumberMobile{ 48 NSString *SelectWhatMobile = @"SELECT mobile FROM numbermobile where uid="; 49 NSString *SelectWhatMobileFull = [SelectWhatMobile stringByAppendingFormat:@"%@",findNumberMobile]; 50 sqlite3 *database; 51 if (sqlite3_open([[self _findDatabase] UTF8String], &database) 52 != SQLITE_OK) { 53 sqlite3_close(database); 54 NSAssert(0, @"Failed to open database"); 55 } 56 // 获取运营商名称 57 sqlite3_stmt *stmt; 58 if (sqlite3_prepare_v2(database, [SelectWhatMobileFull UTF8String], -1, &stmt, nil) == SQLITE_OK) { 59 while (sqlite3_step(stmt) == SQLITE_ROW) { 60 int mobilenumber = sqlite3_column_int(stmt, 0); 61 if (mobilenumber) { 62 NSString *mobileNumberString = [NSString stringWithFormat:@"%d",mobilenumber]; 63 NSString *SelectWhatMobileName = @"SELECT mobile FROM mobilenumber WHERE uid="; 64 NSString *SelectWhatMobileNameFull = [SelectWhatMobileName stringByAppendingFormat:@"%@",mobileNumberString]; 65 sqlite3_stmt *stmt2; 66 if (sqlite3_prepare_v2(database, [SelectWhatMobileNameFull UTF8String], -1, &stmt2, nil) == SQLITE_OK) { 67 while (sqlite3_step(stmt2) == SQLITE_ROW) { 68 char *mobilename = (char *)sqlite3_column_text(stmt2, 0); 69 NSString *mobilenamestring = [[NSString alloc] initWithUTF8String:mobilename]; 70 if (mobilenamestring!= NULL) { 71 self.operater = mobilenamestring; 72 } 73 } 74 }sqlite3_finalize(stmt2); 75 } 76 } 77 sqlite3_finalize(stmt); 78 } 79 // 获取手机号码归属地 80 sqlite3_stmt *stmt3; 81 NSString *SelectCityNumberByPhoneNumber = @"SELECT city FROM phonenumberwithcity WHERE uid="; 82 NSString *SelectCityNumberByPhoneNumberFull = [SelectCityNumberByPhoneNumber stringByAppendingFormat:@"%@",findNumber]; 83 if (sqlite3_prepare_v2(database, [SelectCityNumberByPhoneNumberFull UTF8String], -1, &stmt3, nil) == SQLITE_OK) { 84 if (sqlite3_step(stmt3) == SQLITE_ROW) { 85 int citynumber = sqlite3_column_int(stmt3, 0); 86 NSString *citynumberNSString = [NSString stringWithFormat:@"%d",citynumber]; 87 if (citynumberNSString != nil) { 88 NSString *SelectCityNameAndCtiyZoneByCityBumber = @"SELECT city,zone FROM citywithnumber WHERE uid="; 89 NSString *SelectCityNameAndCtiyZoneByCityBumberFull = [SelectCityNameAndCtiyZoneByCityBumber stringByAppendingFormat:@"%@",citynumberNSString]; 90 sqlite3_stmt *stmt4; 91 if (sqlite3_prepare_v2(database, [SelectCityNameAndCtiyZoneByCityBumberFull UTF8String], -1, &stmt4, nil) == SQLITE_OK) { 92 if (sqlite3_step(stmt4) == SQLITE_ROW) { 93 char *cityname = (char *)sqlite3_column_text(stmt4, 0); 94 int cityzonecode = sqlite3_column_int(stmt4, 1); 95 NSString *cityNameNSString = [[NSString alloc] initWithUTF8String:cityname]; 96 NSString *cityzonecodeNnumber = [@"0" stringByAppendingFormat:@"%d",cityzonecode]; 97 if (cityNameNSString != nil && cityzonecodeNnumber != nil) { 98 self.address = cityNameNSString; 99 } 100 }else { 101 [self _numberUnknown]; 102 } 103 sqlite3_finalize(stmt4); 104 } 105 } 106 }else { 107 [self _numberUnknown]; 108 } 109 sqlite3_finalize(stmt3); 110 } 111 sqlite3_close(database); 112 } 113 114 // 私有方法 115 -(NSString *)_findDatabase{ 116 NSString *path = [[NSBundle mainBundle] pathForResource:@"location_Numbercity_citynumber" ofType:@"db"]; 117 return path; 118 } 119 120 - (void)_doAsServiceNumber:(NSString *)telephoneNumber{ 121 if([telephoneNumber isEqualToString:@"10000"]){ 122 self.address = @"中国电信客服"; 123 self.operater = @"中国电信"; 124 }else if([telephoneNumber isEqualToString:@"10001"]){ 125 self.address = @"中国电信自助服务热线"; 126 self.operater = @"中国电信"; 127 }else if([telephoneNumber isEqualToString:@"10010"]){ 128 self.address = @"中国联通客服"; 129 self.operater = @"中国联通"; 130 }else if([telephoneNumber isEqualToString:@"10011"]){ 131 self.address = @"中国联通充值"; 132 self.operater = @"中国联通"; 133 }else if([telephoneNumber isEqualToString:@"10086"]){ 134 self.address = @"中国移动客服"; 135 self.operater = @"中国移动"; 136 }else{ 137 [self _numberUnknown]; 138 } 139 } 140 141 - (void)_doAsLinePhone:(NSString*)telephoneNumber{ 142 NSString *SelectCityNameByCityZoneCode = @"SELECT city FROM citywithnumber WHERE zone="; 143 NSString *SelectCityNameByCityZoneCodeFull = [SelectCityNameByCityZoneCode stringByAppendingString:telephoneNumber ]; 144 sqlite3 *database; 145 if (sqlite3_open([[self _findDatabase] UTF8String], &database) 146 != SQLITE_OK) { 147 sqlite3_close(database); 148 NSAssert(0, @"Failed to open database"); 149 } 150 sqlite3_stmt *stmt; 151 if (sqlite3_prepare_v2(database, [SelectCityNameByCityZoneCodeFull UTF8String], -1, &stmt, nil) == SQLITE_OK) { 152 if (sqlite3_step(stmt) == SQLITE_ROW) { 153 char *cityname = (char *)sqlite3_column_text(stmt, 0); 154 NSString *cityNameNSString = [[NSString alloc] initWithUTF8String:cityname]; 155 if (cityname != nil) { 156 self.address = cityNameNSString; 157 } 158 }else { 159 [self _numberUnknown]; 160 } 161 sqlite3_finalize(stmt); 162 } 163 sqlite3_close(database); 164 } 165 166 - (void)_numberUnknown{ 167 self.address = @"未知"; 168 self.operater = @"未知"; 169 } 170 171 @end
这个实例,用到了数据库,首先要把数据库拖到工程中来,然后在Build Phases的Copy Bundle Resources中添加进来。
然后在用到数据库的地方,import sqlite3.h 这个文件夹,里面是系统提供的数据库管理类。具体用法可在网上寻找。
标签:style blog http color io os ar for strong
原文地址:http://www.cnblogs.com/chaiwentao/p/4048641.html