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

本文介绍如何利用百度APIStore提供的API接口进行多种类型的数据查询和服务调用,包括天气查询、身份证信息解析、生成二维码等功能,并提供了具体的iOS代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先附上百度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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值