手機歸屬地查詢(雲平台開發)

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

概要

       本章主要簡示了使用彙總Cloud API擷取指定手機號的歸屬地資訊,這次找了個可以免費查詢很多次的平台。開發主要根據彙總的官方文檔,由於擷取的查詢結果是Json格式,所以涉及到了Json解析,但現在的IOS開發內建了Json解析庫,所以事情就簡單多了。


結果展示



流程概要

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。


添加彙總資料SDK以及依賴的包(Objective-C)



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


工程代碼

手機歸屬地查詢(雲平台開發)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.