iOS开发之百度免费API使用案例(身份证号获取地址、手机号查归属地、MD5解密等)

来源:互联网 发布:动漫图软件 编辑:程序博客网 时间:2024/05/18 03:57

先附上百度APIStore的地址:APIStore

下面是其中几个使用案例:考虑到我的博客看的人比较少,里面的apikey就不隐藏了。万一有一天如果apikey使用频率过高被封了,请自行去百度APIStore申请

(身份证号码这俩,不太严谨,不能判断出生日期是否正确,只能解析出所在省市区,和判断最后一位校验码)

#import "ViewController.h"#import "AFNetworking.h"#define index 7@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    #if index == 1// 天气    NSString *httpUrl = @"http://apis.baidu.com/heweather/weather/free";    NSString *httpArg = @"city=beijing";#elif index == 2// 身份证    NSString *httpUrl = @"http://apis.baidu.com/apistore/idservice/id";    NSString *httpArg = @"id=511702188000111114";#elif index == 3// 生成二维码    NSString *httpUrl = @"http://apis.baidu.com/3023/qr/qrcode";    NSString *httpArg = @"qr=http://www.romzhijia.net/Cooperater/22";#elif index == 4// 手机号归属地    NSString *httpUrl = @"http://apis.baidu.com/apistore/mobilenumber/mobilenumber";    NSString *httpArg = @"phone=15330000000";#elif index == 5// 汉字转拼音    NSString *httpUrl = @"http://apis.baidu.com/xiaogg/changetopinyin/topinyin";    NSString *httpArg = [NSString stringWithFormat:@"str=%@&type=json&traditional=0&accent=0&letter=0&only_chinese=0", [@"哈哈" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];#elif index == 6// MD5解密    NSString *httpUrl = @"http://apis.baidu.com/chazhao/md5decod/md5decod";    NSString *httpArg = @"md5=e807f1fcf82d132f9bb018ca6738a19f";#elif index == 7// 身份证(带生肖和星座)    NSString *httpUrl = @"http://apis.baidu.com/chazhao/idcard/idcard";    NSString *httpArg = @"idcard=210905188000111112";#endif        AFHTTPSessionManager *httpSessionManager = [AFHTTPSessionManager manager];    [httpSessionManager.requestSerializer setValue:@"4a4332acbba8d97aa1f3940a8b093965" forHTTPHeaderField:@"apikey"];    httpSessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/plain", nil];    [httpSessionManager GET:[NSString stringWithFormat:@"%@?%@", httpUrl, httpArg] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {        NSLog(@"%@", responseObject);#if index == 1        #elif index == 2        NSNumber *num = responseObject[@"errNum"];        if (num.intValue == 0) {            NSLog(@"%@", responseObject[@"retData"][@"address"]);        }#elif index == 4        NSDictionary *dict = responseObject[@"retData"];        for (NSString *key in dict.allKeys) {            NSLog(@"%@\t\t%@", key, dict[key]);        }#elif index == 7        NSDictionary *dict = responseObject[@"data"];        for (NSString *key in dict.allKeys) {            NSLog(@"%@\t\t%@", key, dict[key]);        }#endif    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {        NSLog(@"%@", error);    }];}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end


1 0
原创粉丝点击