首先是一个库:(有时间在上传)

然后设置一个工具类

.h

 @interface HMFoundLocation : NSObject

 AS_SINGLETON(HMFoundLocation)

 @property(nonatomic,strong)NSString *address;

 @property(nonatomic,strong)NSString *operater;

 //  设置归属地 和 运营商两个属性
- (void)setAddressAndOperater:(NSString *)telephoneNumber; @end

.m

 //
// HMFoundLocation.m
// contactModel
//
// Created by mac_mini on 14-10-24.
// Copyright (c) 2014年 mac_mini. All rights reserved.
// #import "HMFoundLocation.h"
#import <sqlite3.h> @implementation HMFoundLocation DEF_SINGLETON(HMFoundLocation) - (void)setAddressAndOperater:(NSString *)telephoneNumber{
NSInteger lengthOftel = [telephoneNumber length];
if (lengthOftel == ) {
[self _doAsServiceNumber:telephoneNumber];
} else if ((lengthOftel == ) && ([telephoneNumber characterAtIndex:] == '')) {
// 11位手机号 不含+86
NSString *findNumber = [telephoneNumber stringByPaddingToLength: withString:nil startingAtIndex:];
NSString *findNumberMobile = [telephoneNumber stringByPaddingToLength: withString:nil startingAtIndex:];
[self _selectInfoByPhone:findNumber WithMobile:findNumberMobile];
} else if ((lengthOftel == ) && ([telephoneNumber characterAtIndex:] == '')) {
// 固话以零开头 区号为3位
NSString *zoneNumber = [telephoneNumber stringByPaddingToLength: withString:nil startingAtIndex:];
NSRange range;
range.location = ;
range.length = ;
[self _doAsLinePhone:zoneNumber];
self.operater = @"固话";
} else if (((lengthOftel == ) && ([telephoneNumber characterAtIndex:] == '')) || ((lengthOftel == ) && ([telephoneNumber characterAtIndex:] == ''))) {
// 固话以零开头 区号为4位
NSString *zoneNumber = [telephoneNumber stringByPaddingToLength: withString:nil startingAtIndex:];
NSRange range;
range.location = ;
range.length = ;
[self _doAsLinePhone:zoneNumber];
self.operater = @"固话";
}
else{
[self _numberUnknown];
}
} - (void)_selectInfoByPhone:(NSString *)findNumber WithMobile:(NSString *)findNumberMobile{
NSString *SelectWhatMobile = @"SELECT mobile FROM numbermobile where uid=";
NSString *SelectWhatMobileFull = [SelectWhatMobile stringByAppendingFormat:@"%@",findNumberMobile];
sqlite3 *database;
if (sqlite3_open([[self _findDatabase] UTF8String], &database)
!= SQLITE_OK) {
sqlite3_close(database);
NSAssert(, @"Failed to open database");
}
// 获取运营商名称
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, [SelectWhatMobileFull UTF8String], -, &stmt, nil) == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW) {
int mobilenumber = sqlite3_column_int(stmt, );
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], -, &stmt2, nil) == SQLITE_OK) {
while (sqlite3_step(stmt2) == SQLITE_ROW) {
char *mobilename = (char *)sqlite3_column_text(stmt2, );
NSString *mobilenamestring = [[NSString alloc] initWithUTF8String:mobilename];
if (mobilenamestring!= NULL) {
self.operater = mobilenamestring;
}
}
}sqlite3_finalize(stmt2);
}
}
sqlite3_finalize(stmt);
}
// 获取手机号码归属地
sqlite3_stmt *stmt3;
NSString *SelectCityNumberByPhoneNumber = @"SELECT city FROM phonenumberwithcity WHERE uid=";
NSString *SelectCityNumberByPhoneNumberFull = [SelectCityNumberByPhoneNumber stringByAppendingFormat:@"%@",findNumber];
if (sqlite3_prepare_v2(database, [SelectCityNumberByPhoneNumberFull UTF8String], -, &stmt3, nil) == SQLITE_OK) {
if (sqlite3_step(stmt3) == SQLITE_ROW) {
int citynumber = sqlite3_column_int(stmt3, );
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], -, &stmt4, nil) == SQLITE_OK) {
if (sqlite3_step(stmt4) == SQLITE_ROW) {
char *cityname = (char *)sqlite3_column_text(stmt4, );
int cityzonecode = sqlite3_column_int(stmt4, );
NSString *cityNameNSString = [[NSString alloc] initWithUTF8String:cityname];
NSString *cityzonecodeNnumber = [@"" stringByAppendingFormat:@"%d",cityzonecode];
if (cityNameNSString != nil && cityzonecodeNnumber != nil) {
self.address = cityNameNSString;
}
}else {
[self _numberUnknown];
}
sqlite3_finalize(stmt4);
}
}
}else {
[self _numberUnknown];
}
sqlite3_finalize(stmt3);
}
sqlite3_close(database);
} // 私有方法
-(NSString *)_findDatabase{
NSString *path = [[NSBundle mainBundle] pathForResource:@"location_Numbercity_citynumber" ofType:@"db"];
return path;
} - (void)_doAsServiceNumber:(NSString *)telephoneNumber{
if([telephoneNumber isEqualToString:@""]){
self.address = @"中国电信客服";
self.operater = @"中国电信";
}else if([telephoneNumber isEqualToString:@""]){
self.address = @"中国电信自助服务热线";
self.operater = @"中国电信";
}else if([telephoneNumber isEqualToString:@""]){
self.address = @"中国联通客服";
self.operater = @"中国联通";
}else if([telephoneNumber isEqualToString:@""]){
self.address = @"中国联通充值";
self.operater = @"中国联通";
}else if([telephoneNumber isEqualToString:@""]){
self.address = @"中国移动客服";
self.operater = @"中国移动";
}else{
[self _numberUnknown];
}
} - (void)_doAsLinePhone:(NSString*)telephoneNumber{
NSString *SelectCityNameByCityZoneCode = @"SELECT city FROM citywithnumber WHERE zone=";
NSString *SelectCityNameByCityZoneCodeFull = [SelectCityNameByCityZoneCode stringByAppendingString:telephoneNumber ];
sqlite3 *database;
if (sqlite3_open([[self _findDatabase] UTF8String], &database)
!= SQLITE_OK) {
sqlite3_close(database);
NSAssert(, @"Failed to open database");
}
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, [SelectCityNameByCityZoneCodeFull UTF8String], -, &stmt, nil) == SQLITE_OK) {
if (sqlite3_step(stmt) == SQLITE_ROW) {
char *cityname = (char *)sqlite3_column_text(stmt, );
NSString *cityNameNSString = [[NSString alloc] initWithUTF8String:cityname];
if (cityname != nil) {
self.address = cityNameNSString;
}
}else {
[self _numberUnknown];
}
sqlite3_finalize(stmt);
}
sqlite3_close(database);
} - (void)_numberUnknown{
self.address = @"未知";
self.operater = @"未知";
} @end

这个实例,用到了数据库,首先要把数据库拖到工程中来,然后在Build Phases的Copy Bundle Resources中添加进来。

然后在用到数据库的地方,import sqlite3.h 这个文件夹,里面是系统提供的数据库管理类。具体用法可在网上寻找。

IOS 完成来电归属地的更多相关文章

  1. IOS获取来电去电来电归属系统通知达到效果(一)

    这里的原理是获得到呼叫系统通知,然后根据本地呼叫电话号码,相应的电话联系. 一世.   来电显示是不是在地址簿中的联系人, 这是当第一个加入一个临时的联系人在您的电话簿(它是创建一个新的接触,并保存相 ...

  2. ios应用来电监听

    先导入这两个头文件,库文件不用导可以 #import <CoreTelephony/CTCallCenter.h> #import <CoreTelephony/CTCall.h&g ...

  3. iOS 判断来电状态CTCallCenter代码块不执行问题的解决

    项目中需要检测来电状态,使用了CTCallCenter,在AppDelegate中,代码如下: 
 CTCallCenter *callCenter = [[CTCallCenter alloc] i ...

  4. C# 号码归属地查询算法(根据Android来电归属地二进制文件查询修改)

    前言 近期有个项目需要用到号码归属查询,归属地数据库可能比不上ip138,淘宝上也有卖的-,-! 文本提供一个279188条记录并压缩成562KB的归属地数据.我在互联网上搜索了相关文章,要不是数据库 ...

  5. 关于iOS7越狱的整理

    目前越狱非常的不稳定,已经白苹果第三次了.中途遇见了不少问题,去各大论坛找了下解决办法,算是搬运工. iOS7越狱过程中打开手机上的“evasi0n7”闪退,怎么办?1. 请先尝试卸载手机“evasi ...

  6. iphone5s越狱之后必装

    一.iphone5s完美越狱之后必装插件一览表 由于iPhone5s配置了强大的64位的A7处理器,所以在iOS7完美越狱后,不少iPhone5s用户发现之前安装的大部分人们插件在越狱后难以兼容.但是 ...

  7. 2016WWDC详解

    今年苹果WWDC 2016上把所有系统都更新了个遍,watchOS.tvOS.macOS 和 iOS 都或多或少带来了新功能. 本文的主角是更新最多的 iOS 10,第一时间在一部 iPhone 6s ...

  8. Android核心基础(手机卫士的一个知识点总结)

    注意:有些功能是需要权限的,在这里并没有写出来,在程序运行中,根据程序报的错误,添加相应的权限即可,里面的具体里面可能有一些小细节,没有明确的写出来,具体的需要在程序中自己调试,解决. 这个总结涵盖了 ...

  9. C# 号码归属地查询算法

    C# 号码归属地查询算法(根据Android来电归属地二进制文件查询修改) 前言 近期有个项目需要用到号码归属查询,归属地数据库可能比不上ip138,淘宝上也有卖的-,-! 文本提供一个279188条 ...

随机推荐

  1. Luogu-3829 [SHOI2012]信用卡凸包

    这道题的转化很巧妙,可以把信用卡四个角的圆心看做平面上的点来做凸包,\(ans\)就是凸包周长加上一个圆的周长 // luogu-judger-enable-o2 #include<cmath& ...

  2. EntityFramework 学习 一 DBEntityEntry

    DbEntityEntry是一个重要的类,用来获取各种各样的实体信息 可以通过DBContext的Entry方法获取DbEntityEntry的实例 DBEntityEntry studentEntr ...

  3. HTML5学习笔记——1

    1.视频 <vedio src  controls  height width autoplay loop preload > </vedio> <source src ...

  4. vim乱码的解决

    解决vim文件乱码,打开文件乱码,菜单,提示信息乱码: 有四个跟字符编码方式有关的选项,encoding.fileencoding.fileencodings.termencoding 在linux中 ...

  5. JavaUtil_02_二维码的生成与解析

    1.引入jar包 zxing-core-1.7.jar  :   http://viralpatel.net/blogs/download/jar/zxing-core-1.7.jar zxing-j ...

  6. 关于MFC的DLL调用方法问题

    参考资料: 一.dll导出方式: MFC的DLL函数导出方法有两种:一种是通过模块定义文件DEF文件:另一种是在导出函数前加_declspec(dllexport). 1.def文件方法: 只需要在E ...

  7. jitter buffer QoS的解决方案

    本文主要介绍一种QoS的解决方案,文章来自博客园RTC.Blacker,欢迎关注微信公众号blacker,更多详见www.rtc.help QoS出现的背景: 而当网络发生拥塞的时候,所有的数据流都有 ...

  8. BZOJ5372: [Pkusc2018]神仙的游戏

    BZOJ5372: [Pkusc2018]神仙的游戏 https://lydsy.com/JudgeOnline/problem.php?id=5372 分析: 如果\(len\)为\(border\ ...

  9. python setuptools安装

    执行# python setup.py install 时发生如下错误 Traceback (most recent call last): File "setup.py", li ...

  10. Python:更改字典的key

    思路:先删除原键值对,保存值,然后以新键插入字典 格式:dict[newkey] = dict.pop(key) d = {'a':1, 'aa':11} d['b'] = d.pop('a') d[ ...