ios 手机查询
首先用到一个手机归属地查询的服务网站。http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
SOAP是简单对象访问协议,它可看成是HTTP与XML的结合,其中XML部分是作为HTTP报文的实体主体部分
[webData setLength: 0]; //注意这里需要改正一下:webData=[NSMutableData alloc ]init];
1、运行Xcode 4.4.1,新建一个Single View Application,名称为SOAP Test:
2、界面设计:打开ViewController.xib,设计界面如下所示:
在文本输入框的Attribute Inspector中设置其Keyboard属性为Number Pad。
3、之后向ViewController.h中,为文本输入框创建OutLet映射,名称为:phoneNumber;为“查询”按钮创建Action映射,事件类型为Touch Up Inside,名称为:doQuery。建立映射的方法就是打开Assistant Editor,选中某一控件,按住Ctrl,拖向ViewController.h,可以参考前面的文章。
4、在ViewController.h中添加代码:
4.1 在@interface那行最后添加代码
<NSXMLParserDelegate, NSURLConnectionDelegate>
使ViewController遵守这两个协议。前者用来解析XML,后者用于网络连接。
4.2 在@end之前添加代码
@property (strong, nonatomic) NSMutableData *webData; @property (strong, nonatomic) NSMutableString *soapResults; @property (strong, nonatomic) NSXMLParser *xmlParser; @property (nonatomic) BOOL elementFound; @property (strong, nonatomic) NSString *matchingElement; @property (strong, nonatomic) NSURLConnection *conn;
5、在ViewController.m中添加代码:
5.1 在@implementation之后添加代码
@synthesize webData; @synthesize soapResults; @synthesize xmlParser; @synthesize elementFound; @synthesize matchingElement; @synthesize conn;
5.2 实现doQuery方法
- (IBAction)doQuery:(id)sender { NSString *number = phoneNumber.text; // 设置我们之后解析XML时用的关键字,与响应报文中Body标签之间的getMobileCodeInfoResult标签对应 matchingElement = @"getMobileCodeInfoResult"; // 创建SOAP消息,内容格式就是网站上提示的请求报文的实体主体部分 NSString *soapMsg = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"utf-8\"?>" "<soap12:Envelope " "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " "xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" "<soap12:Body>" "<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">" "<mobileCode>%@</mobileCode>" "<userID>%@</userID>" "</getMobileCodeInfo>" "</soap12:Body>" "</soap12:Envelope>", number, @""]; // 将这个XML字符串打印出来 NSLog(@"%@", soapMsg); // 创建URL,内容是前面的请求报文报文中第二行主机地址加上第一行URL字段 NSURL *url = [NSURL URLWithString: @"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"]; // 根据上面的URL创建一个请求 NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]]; // 添加请求的详细信息,与请求报文前半部分的各字段对应 [req addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [req addValue:msgLength forHTTPHeaderField:@"Content-Length"]; // 设置请求行方法为POST,与请求报文第一行对应 [req setHTTPMethod:@"POST"]; // 将SOAP消息加到请求中 [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]]; // 创建连接 conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; if (conn) { webData = [NSMutableData data]; }}5.3 在@end之前添加代码
#pragma mark -#pragma mark URL Connection Data Delegate Methods // 刚开始接受响应时调用-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *) response{ [webData setLength: 0]; //注意这里需要改正一下:webData=[NSMutableData alloc ]init];} // 每接收到一部分数据就追加到webData中-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *) data { [webData appendData:data];} // 出现错误时-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *) error { conn = nil; webData = nil;} // 完成接收数据时调用-(void) connectionDidFinishLoading:(NSURLConnection *) connection { NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; // 打印出得到的XML NSLog(@"%@", theXML); // 使用NSXMLParser解析出我们想要的结果 xmlParser = [[NSXMLParser alloc] initWithData: webData]; [xmlParser setDelegate: self]; [xmlParser setShouldResolveExternalEntities: YES]; [xmlParser parse];}
5.4 在@end之前添加代码#pragma mark -#pragma mark XML Parser Delegate Methods // 开始解析一个元素名-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict { if ([elementName isEqualToString:matchingElement]) { if (!soapResults) { soapResults = [[NSMutableString alloc] init]; } elementFound = YES; }} // 追加找到的元素值,一个元素值可能要分几次追加-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string { if (elementFound) { [soapResults appendString: string]; }} // 结束解析这个元素名-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if ([elementName isEqualToString:matchingElement]) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"手机号码信息" message:[NSString stringWithFormat:@"%@", soapResults] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; elementFound = FALSE; // 强制放弃解析 [xmlParser abortParsing]; }} // 解析整个文件结束后- (void)parserDidEndDocument:(NSXMLParser *)parser { if (soapResults) { soapResults = nil; }} // 出错时,例如强制结束解析- (void) parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError { if (soapResults) { soapResults = nil; }}
#parm mark=======下面都是重复的了。=============
// 开始查询- (IBAction)doQuery:(id)sender { NSString *number = phoneNumber.text; // 设置我们之后解析XML时用的关键字,与响应报文中Body标签之间的getMobileCodeInfoResult标签对应 matchingElement = @"getMobileCodeInfoResult"; // 创建SOAP消息,内容格式就是网站上提示的请求报文的实体主体部分 NSString *soapMsg = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"utf-8\"?>" "<soap12:Envelope " "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " "xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">" "<soap12:Body>" "<getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">" "<mobileCode>%@</mobileCode>" "<userID>%@</userID>" "</getMobileCodeInfo>" "</soap12:Body>" "</soap12:Envelope>", number, @""]; // 将这个XML字符串打印出来 NSLog(@"%@", soapMsg); // 创建URL,内容是前面的请求报文报文中第二行主机地址加上第一行URL字段 NSURL *url = [NSURL URLWithString: @"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"]; // 根据上面的URL创建一个请求 NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]]; // 添加请求的详细信息,与请求报文前半部分的各字段对应 [req addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [req addValue:msgLength forHTTPHeaderField:@"Content-Length"]; // 设置请求行方法为POST,与请求报文第一行对应 [req setHTTPMethod:@"POST"]; // 将SOAP消息加到请求中 [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]]; // 创建连接 conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; if (conn) { webData = [NSMutableData data]; }}5.3 在@end之前添加代码#pragma mark -#pragma mark URL Connection Data Delegate Methods // 刚开始接受响应时调用-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *) response{ [webData setLength: 0];} // 每接收到一部分数据就追加到webData中-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *) data { [webData appendData:data];} // 出现错误时-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *) error { conn = nil; webData = nil;} // 完成接收数据时调用-(void) connectionDidFinishLoading:(NSURLConnection *) connection { NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; // 打印出得到的XML NSLog(@"%@", theXML); // 使用NSXMLParser解析出我们想要的结果 xmlParser = [[NSXMLParser alloc] initWithData: webData]; [xmlParser setDelegate: self]; [xmlParser setShouldResolveExternalEntities: YES]; [xmlParser parse];}
5.4 在@end之前添加代码#pragma mark -#pragma mark XML Parser Delegate Methods // 开始解析一个元素名-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict { if ([elementName isEqualToString:matchingElement]) { if (!soapResults) { soapResults = [[NSMutableString alloc] init]; } elementFound = YES; }} // 追加找到的元素值,一个元素值可能要分几次追加-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string { if (elementFound) { [soapResults appendString: string]; }} // 结束解析这个元素名-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if ([elementName isEqualToString:matchingElement]) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"手机号码信息" message:[NSString stringWithFormat:@"%@", soapResults] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; elementFound = FALSE; // 强制放弃解析 [xmlParser abortParsing]; }} // 解析整个文件结束后- (void)parserDidEndDocument:(NSXMLParser *)parser { if (soapResults) { soapResults = nil; }} // 出错时,例如强制结束解析- (void) parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError { if (soapResults) { soapResults = nil; }}
6、运行
其中,输入号码时单击查询,打印出的响应XML如下:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/"> <getMobileCodeInfoResult>151898XXXXX:江苏 南京 江苏移动全球通卡 </getMobileCodeInfoResult> </getMobileCodeInfoResponse> </soap:Body></soap:Envelope>
上面的XML进行了缩进处理,实际上打印出来的是一行。
相关推荐
iOS 手机查询归属地 在程序中可以植入的小功能,感觉不错。
可以查询手机号码的ios小项目,能够查询手机归属地。
iPhone iOS 数据库查询 源代码
一个输入手机号然后请求服务器返回归属地的小demo
IOS应用源码之Soap请求查询手机号归属地.zip
这是基于STM32F103ZET6设计的遥控小车手机APP源码,采用QT框架设计,跨平台设计,支持windows、Android、IOS、Linux平台编译运行,代码不变,一套代码编译到任何平台。通过ESP8266与手机APP相连接,手机APP可以控制...
简单的手机的型号 和 系统的版本查询。 支持ios8等
项目中需要使用到获取手机通讯录中的联系人信息,适配9.0以下,iOS9以下使用通讯录获取名称和电话号码
可以下载ios各个历史版本app。 查询版本使用接口 API接口1:https://api.unlimapps.com/v1/apple_apps/ + id+ /versions 接口2数据源:https://tools.lancely.tech/apple/app-search
通过ITUNES的APP链接查询D版数据库,然后给出破解版的下载链接,使用installos的数据库。通常国外的稀有app通过这个都可以查询到。非常强大~
PassFab iOS Password Manager For Mac_v1.3.2是一款iOS密码管理工具。如果你忘记手机上的密码就可以通过它查找,主要提供邮箱密码、网站密码、应用程序密码、WiFi密码等内容查询。
1.首先获取到微信的openID,然后通过openID去后台数据库查询该微信的openID有没有绑定好的手机号. 2.如果没有绑定,首相第一步就是将微信用户的头像、昵称等等基本信息添加到数据库;然后通过手机获取验证码;最后绑定...
ios开发,用FMDB数据库开发的一个手机通讯录,支持查询,滑动删除等。
软件简介:中国联通手机营业厅是为中国联通用户提供查询办理业务的一款客户端软件。目前可以提供:话费查询、业务查询、充值卡充值、3G套餐变更、密码修改、积分兑换、营业厅分布查询、手机号码归属地、puk查询、3G...
在iOS或Android等移动端开发过程中,经常遇到很多需要我们输入信息的情况,例如登录时要输入账号密码、查询时要输入查询信息、注册或申请时需要填写一些信息等都是通过我们键盘来进行输入的,在iOS开发过程中,一般...
本Demo内包含 Java端、PHP端,以及 iOS本地搭建服务器,安装 描述文件获取手机唯一UDID 以及回调的方法
在iOS项目开发的过程中,如果版本迭代开发的时间比较长,那么在很多版本开发以后或者说有多人开发参与以后,工程中难免有一些垃圾资源,未被使用却占据着api包的大小! 这里我通过Python脚本来查找项目中未被使用的...
安卓手机客户端App源程序,演示如何用在手机App查询数据库 跨平台开发,你也可以发布在IOS,安卓和Win平台上. XE7以上版本通用.
比如搜索,全选、反选和删除等在实际开发中可能需要实现的功能,本篇博客是小巫从一个别人开源的一个项目抽取出来的部分内容,把它给简化出来,可以让需要的朋友清楚知道如何对Android数据库操作,异步查询数据库...
私聊、群聊、会话列表,文字、表情,图片,语音,视频消息,支持发送自定义业务消息,比如处方,订单,红包,地理位置等,手机通知栏提醒,厂商通道支持:iOS,谷歌FCM,华为,小米,VIVO,OPPO,魅族,消息已读未读...