1. 程式人生 > >iOS開發之百度免費API使用案例(身份證號獲取地址、手機號查歸屬地、MD5解密等)

iOS開發之百度免費API使用案例(身份證號獲取地址、手機號查歸屬地、MD5解密等)

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