在iOS中找到手机信号塔的位置

时间:2016-02-17 08:34:08

标签: ios objective-c json xcode networking

现在我试图使用cellID,MNC,MCC和LAC找到塔位置。 如果您拥有 cellId,MNC,MCC和LAC ,则可以轻松找到iOS中的手机信号塔位置。经过一番努力,我得到了这个问题的答案。

1 个答案:

答案 0 :(得分:0)

现在这是这个问题的答案。

#define google_geo_location @"https://www.googleapis.com/geolocation/v1/geolocate?key="

谷歌API位置

NSString * urlString = [NSString stringWithFormat:@"google API key ",google_geo_location];

创建 JSON STRING

NSString *json=[NSString stringWithFormat:@"[{\"homeMobileCountryCode\": \"%@\",\"homeMobileNetworkCode\": \"%@\",\"cellTowers\": [{\"cellId\": \"%@\",\"locationAreaCode\": \"%@\",\"mobileCountryCode\": \"%@\",\"mobileNetworkCode\": \"%@\" }]}]",_mcctext,_mnctext,_cellidtext,_lactext,_mcctext,_mnctext];

JSON STRING 转换为 JSON DICTIONARY

NSError *jsonError;
 NSData *objectData = [json dataUsingEncoding:NSUTF8StringEncoding];
 NSDictionary *jsondic = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&jsonError];

从字符串

创建网址
NSURL *urlStr=[NSURL URLWithString:urlString];
    if (urlStr == nil)
    {urlStr = [[NSURL alloc] initWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    }
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:urlStr];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

设置请求正文,如果方法类型为“POST”,则仅使用 POST

    NSData *data=[NSJSONSerialization dataWithJSONObject:jsondic options:NSUTF8StringEncoding error:nil];
NSString* jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"jsonString.....%@",jsonString);
    NSData *requestBody = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

// for set requestBody
[request setHTTPBody:requestBody];

现在您从服务器获得响应

NSHTTPURLResponse *response = NULL;
    NSError *requestError = NULL;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];

    NSMutableDictionary *resultantDict;

    if (responseData != nil)
    {
        resultantDict=[NSJSONSerialization
                       JSONObjectWithData:responseData
                       options:NSJSONReadingMutableLeaves
                       error:nil];
        NSLog(@"resultantDict=%@",resultantDict);


        NSString *errorCode=@"";
        NSString *errorMessage=@"";
        if ([[resultantDict allKeys] containsObject:@"error"])
        {
            errorCode=[NSString stringWithFormat:@"%@", [[resultantDict valueForKey:@"error"]valueForKey:@"code"]];
            errorMessage= [[resultantDict valueForKey:@"error"]valueForKey:@"message"];
        }


    }
相关问题