美文网首页
iOS 手机号码归属地

iOS 手机号码归属地

作者: 不说谎的匹诺曹Y | 来源:发表于2017-08-24 10:49 被阅读0次

    @interface ViewController ()<NSURLConnectionDataDelegate>

    UIButton *_oneBtn,*_twoBtn;

    UILabel *_theLb;

    //创建一个data 用来接收回调回来的数据

    NSMutableData *myDatas;

    }

    @property (nonatomic,strong)UITextView *theTV;

    @property (nonatomic,strong)UITextField *theTf;

    @end

    #define TEST_URL @"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=%@&userID="

    #define POST_URL @"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"

    @implementation ViewController

    -(UITextView *)_theTV{

    if (!_theTV) {

    _theTV = [[UITextView alloc]initWithFrame:CGRectMake(60, 300, 280, 300) ];

    _theTV.backgroundColor = [UIColor lightGrayColor];

    _theTV.textColor  =[UIColor blackColor];

    }

    return _theTV;

    }

    -(UITextField *)_theTf{

    if (!_theTf) {

    _theTf = [[UITextField alloc]initWithFrame:CGRectMake(100, 50, 200, 40)];

    _theTf.backgroundColor = [UIColor colorWithRed:184/255.0 green:242/255.0 blue:38/255.0 alpha:1];

    }

     return _theTf;

    }

    - (void)viewDidLoad {

    [super viewDidLoad];

    _oneBtn = [UIButton buttonWithType: UIButtonTypeRoundedRect];

    _oneBtn.frame = CGRectMake(100, 200, 50, 40);

    [_oneBtn setTitle:@"GET" forState:UIControlStateNormal];

    [_oneBtn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

    _twoBtn = [UIButton buttonWithType: UIButtonTypeRoundedRect];

    _twoBtn.frame = CGRectMake(200, 200, 50, 40);

    [_twoBtn setTitle:@"POST" forState:UIControlStateNormal];

    [_twoBtn addTarget:self action:@selector(clicktwo) forControlEvents:UIControlEventTouchUpInside];

    _theLb = [[UILabel alloc]initWithFrame:CGRectMake(20, 50, 80, 40)];

    _theLb.textColor = [UIColor redColor];

    [_theLb setText:@"手机号:"];

    [self.view addSubview:_theLb];

    [self.view addSubview:_twoBtn];

    [self.view addSubview:_oneBtn];

    [self.view addSubview:self._theTV];

    [self.view addSubview:self._theTf];

    }

    //GET方法

    -(void)click{

    //将网络连接 用str拼接

    NSString *urlStr = [NSString stringWithFormat:TEST_URL,self.theTf.text];

    //创建网络连接

    NSURL *url = [[NSURL alloc]initWithString:urlStr];

    //创建置网络请求

    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:0 timeoutInterval:5];

    // connectionWithRequest  连接与请求  设置网络连接

    [NSURLConnection connectionWithRequest:request delegate:self];

    }

    //POST方法

    -(void)clicktwo{

    //创建网络连接

    NSURL *url = [[NSURL alloc]initWithString:POST_URL];

    //封装一个请求对象

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:0 timeoutInterval:5];

    //设置请求方式  Method 方法

    [request setHTTPMethod:@"POST"];

    //参数

    NSString *str =[NSString stringWithFormat:@"%@",self.theTf.text];

    //转换格式  usingEncoding 数据使用编码

    NSData *data = [ str dataUsingEncoding:NSUTF8StringEncoding];

    //设置需要传递的参数

    [request setHTTPBody:data];

    //sendSynchronousRequest 发送同步请求

    NSData *Mydata = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    //展示信息

    self.theTV.text = [[NSString alloc]initWithData:myDatas encoding:NSUTF8StringEncoding];

    }

    //请求服务器成功后自动回调 执行一次

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    //初始化 数据库

    myDatas = [[NSMutableData alloc]init];

    }

    //成功读取服务器端数据后自动回调 执行多次

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    //拼接数据 拼接数据

    [myDatas appendData:data];

    }

    //请求结束后自动回调

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection{

    //展示信息  展示在UITextView上

    self.theTV.text = [[NSString alloc]initWithData:myDatas encoding:NSUTF8StringEncoding];

    }

    相关文章

      网友评论

          本文标题:iOS 手机号码归属地

          本文链接:https://www.haomeiwen.com/subject/apisdxtx.html