Overview
This chapter mainly shows the use of the aggregation cloud API to obtain the designated mobile phone number of the attribution information, this time to find a free query many times the platform. Development is mainly based on aggregated official documents, because the obtained query results are in JSON format, so it involves JSON parsing, but now the iOS development has built-in JSON parsing library, so things are much simpler.
results show
Process Overview
1. Register your account on the converged cloud platform and create an app to download the corresponding SDK
2. Review the SDK documentation to create an app add header file, library, and framework as described in document description as follows:
Add the juheapissdk.a and header file "include" folder to your project, add dependent libraries coretelephony.framework, adsupport.framework, Corelocation.framework. Note: 1, the development environment uses xCode6.0 above version development, 2, changes appdelegate.m to APPDELEGATE.MM, or selects the item, in the right setting window selects: Targets->xxx ( Project name)->build Phases->link Binary with Libraries, adding libc++.dylib.
Add aggregation data SDK and dependent packages (OBJECTIVE-C)
3. Put OpenID and Appkey in the project, layout interface
4. Build the request URL, use Nsurlconnection to make the request, use the corresponding proxy to save the obtained results, and use the Nsjsonserialization Parse The request result, note that the result of JSON parsing is a dictionary tree type.
Main codeh file
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 file
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 implementation protocol nsurlconnectiondatadelegate-(void) connection: ( Nsurlconnection *) connection didreceivedata: (NSData *) data{nsstring* strdata = [[NSString alloc] Initwithdata:data enc Oding:nsutf8stringencoding]; NSLog (@ "%@", strdata); self._stringrecived = [self._stringrecived stringbyappendingstring:strdata]; [Strdata release];} -(void) connectiondidfinishloading: (nsurlconnection *) connection{NSLog (@ "All data: \r\n%@", self._stringrecived); [Self parserresult];} Parse query result (JSON format)/* {"ResultCode": "$", "Reason": "Return successd!", "result": {"PR Ovince ":" Shandong ", "City": "Linyi", "AreaCode": "0539", "Zip": "276000", "Company": "China Unicom", "Card": "Unknown"}, "Error_code": 0} */-(bool) parserresult{BOOL BOK = NO; Parse JSON data nsdata* [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]; Successful if ((nsnull*) result! = [NSNull null] && [result Count]) {for (nsstring* Key in result) {[str appendformat:@ '%@:%@\n", key, [result Objectforkey:key]]; BOK = YES; }}//Failed 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;} Issue Query Request-(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];} Layout-(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]; Additional setup after loading the view, typically from a nib. Self._keyid = @ "Your Appkey"; Self._openid = @ "Your 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]; Cutting screen [[Nsnotificationcenter defaultcenter] addobserver:self selector: @sele ctor (dorotateaction:) name:uideviceorientationdidchangenotification Object:nil]; [Self resetlayout];} -(void) Dorotateaction: (nsnotification *) notification{[self resetlayout];} -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} -(void) dealloc{[_textphone release]; [_textresult release]; [_keyid release]; [_openid release]; [_stringrecived release]; [Super Dealloc];} @end
Engineering Code
Mobile phone Attribution query (Cloud Platform development)