码迷,mamicode.com
首页 > 移动开发 > 详细

手机归属地查询(云平台开发)

时间:2014-12-13 18:06:14      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

概要

       本章主要简示了使用聚合云API获取指定手机号的归属地信息,这次找了个可以免费查询很多次的平台。开发主要根据聚合的官方文档,由于获取的查询结果是Json格式,所以涉及到了Json解析,但现在的IOS开发内置了Json解析库,所以事情就简单多了。


结果展示

bubuko.com,布布扣


流程概要

1.在聚合云平台上注册账号并创建应用,下载对应的SDK

2.查看SDK文档,根据文档描述创建应用添加头文件、库、框架,官当文档描述如下:

将JuheApisSDK.a以及头文件“include”文件夹添加到自己的工程中来,添加依赖库CoreTelephony.framework, AdSupport.framework, CoreLocation.framework。
   注:
      1,开发环境使用xCode6.0以上版本进行开发,
      2,将AppDelegate.m改为AppDelegate.mm,或者选中项目,在右侧的设置窗口中选择:TARGETS->XXX(项目名)->Build Phases->Link Binary With Libraries,添加libc++.dylib。

bubuko.com,布布扣

添加聚合数据SDK以及依赖的包(Objective-C)

bubuko.com,布布扣


3.把OpenID和AppKey放到工程里面,布局界面

4.构建请求URL,使用NSURLConnection发出请求,使用对应的代理保存获取的结果,并使用NSJSONSerialization解析请求结果,注意Json解析后的结果是个字典树类型。



主要代码

h文件

//
//  ViewController.h
//  WhatPhone
//
//  Created by God Lin on 14/12/13.
//  Copyright (c) 2014年 arbboter. All rights reserved.
//

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
    NSString* _openID;
    NSString* _keyID;
    
    UITextField* _textPhone;
    UITextView* _textResult;
    
    NSString* _stringRecived;
}

@property (nonatomic, retain) NSString* _openID;
@property (nonatomic, retain) NSString* _keyID;
@property (nonatomic, retain) UITextField* _textPhone;
@property (nonatomic, retain) UITextView* _textResult;
@property (nonatomic, retain) NSString* _stringRecived;

@end


m文件

//
//  ViewController.m
//  WhatPhone
//
//  Created by God Lin on 14/12/13.
//  Copyright (c) 2014年 arbboter. All rights reserved.
//

#import "ViewController.h"
#import "JHAPISDK.h"
#import "JHOpenidSupplier.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize _openID;
@synthesize _keyID;
@synthesize _textPhone;
@synthesize _textResult;
@synthesize _stringRecived;

#pragma 实现协议NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSString* strData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    //NSLog(@"%@", strData);
    self._stringRecived = [self._stringRecived stringByAppendingString:strData];

    [strData release];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"All data : \r\n%@", self._stringRecived);
    [self parserResult];
}

// 解析查询结果(Json格式)
/*
 {
    "resultcode": "200",
    "reason": "Return Successd!",
    "result": 
             {
             "province": "山东",
             "city": "临沂",
             "areacode": "0539",
             "zip": "276000",
             "company": "中国联通",
             "card": "未知"
             },
    "error_code": 0
 }
 */
-(BOOL)parserResult
{
    BOOL bOK = NO;
    // 解析Json数据
    NSData* data = [self._stringRecived dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];;
    if (json == nil)
    {
        NSLog(@"json parse failed \r\n");
        bOK = NO;
    }
    else
    {
        NSDictionary *result = [json objectForKey:@"result"];
        
        NSMutableString* str = [[NSMutableString alloc] initWithString:self._textResult.text];
        
        [str appendFormat:@"========%@========\n", self._textPhone.text];
        // 成功
        if((NSNull*)result != [NSNull null] && [result count])
        {
            
            for (NSString* key in result)
            {
                [str appendFormat:@"%@:%@\n", key, [result objectForKey:key]];
                bOK = YES;
            }
            
        }
        // 失败
        else
        {
            [str appendFormat:@"error !!!\n"];
            for (NSString* key in json)
            {
                [str appendFormat:@"%@:%@\n", key, [json objectForKey:key]];
            }
        }
        [str appendFormat:@"========%@========\n\n", self._textPhone.text];
        
        self._textResult.text = str;
        [str release];
    }
    
    self._textPhone.text = @"";
    return bOK;
}

// 发出查询请求
-(IBAction)OnPhoneInfo:(id)sender
{
    [self._textPhone resignFirstResponder];
    self._stringRecived = @"";
    NSString* strUrl = [[NSString alloc] initWithFormat:@"http://apis.juhe.cn/mobile/get?phone=%@&key=%@", self._textPhone.text, self._keyID];
    NSURL* url = [[NSURL alloc] initWithString:strUrl];
    NSURLRequest* request = [[NSURLRequest alloc] initWithURL:url];
    NSURLConnection* connect = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
    NSLog(@"Request url : \r\n %@", strUrl);
    [connect release];
    [request release];
    [url release];
    [strUrl release];
}

// 布局
-(void)resetLayout
{
    CGRect frame = self.view.frame;
    
    CGFloat _x = frame.origin.x;
    CGFloat _y = frame.origin.y;
    CGFloat _w = frame.size.width;
    CGFloat _h = frame.size.height;
    
    CGFloat yEdge = 10;
    CGFloat xEdge = 20;
    CGFloat x = _x + xEdge;
    CGFloat y = _y + 40;
    CGFloat w = _w - 2* xEdge;
    CGFloat h = 30;
    
    self._textPhone.frame = CGRectMake(x, y, w, h);
    self._textPhone.layer.cornerRadius = 10;
    self._textPhone.placeholder = @"phone number";
    self._textPhone.textAlignment = NSTextAlignmentCenter;
    self._textPhone.layer.borderWidth = 1;
    
    y = self._textPhone.frame.origin.y + self._textPhone.frame.size.height + yEdge;
    self._textResult.frame = CGRectMake(x, y, w, _y + _h - y - yEdge);
    self._textResult.editable = NO;
    self._textResult.layer.borderWidth = 1;
    self._textResult.layer.cornerRadius = 1;
    self._textResult.layer.borderColor = [[UIColor blackColor] CGColor];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self._keyID = @"你的AppKey";
    self._openID = @"你的OpenID";
    [[JHOpenidSupplier shareSupplier] registerJuheAPIByOpenId:self._openID];
    
    // new UI
    self._textPhone = [[UITextField alloc] init];
    [self.view addSubview:self._textPhone];
    [self._textPhone addTarget:self action:@selector(OnPhoneInfo:) forControlEvents:UIControlEventEditingDidEndOnExit];
    
    self._textResult = [[UITextView alloc] init];
    [self.view addSubview:self._textResult];
    
    // 切屏
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(doRotateAction:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
    
    [self resetLayout];
}

-(void) doRotateAction:(NSNotification *) notification
{
  [self resetLayout];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc
{
    [_textPhone release];
    [_textResult release];
    [_keyID release];
    [_openID release];
    [_stringRecived release];
    
    [super dealloc];
}
@end


工程代码

手机归属地查询(云平台开发)

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://blog.csdn.net/arbboter/article/details/41909959

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!