壹、申請API(拿好appid和private_key)
二、解讀《SmartWeatherAPI<Lite> WebAPI版接口使用說明書》
三、準備好areaid、type、date、appid、urlencode($key)(註意,這裏經加密的key是需要encodeurl之後的才能成為接口鏈接的壹部分)
好了下面的編碼開始:
1、從附件中的areaid_list中找到妳想要的地方的areaid,並且選擇要查詢天氣的類型
NSString *areaid = @"101010100";
NSString *type =
@"index_f";
/**
* 官方文檔更新的數據類型號
* 指數:index_f(基礎接口);index_v(常規接口)
3天預報:forecast_f(基礎接口);forecast_v(常規接口)
*
*/
2、獲得當前天氣date
NSDate
*_date = [NSDate date];
NSDateFormatter *dateFormatter =
[[NSDateFormatter alloc] init];
[dateFormatter
setDateFormat:@"yyyyMMddHHmmss"];//註意日期的格式
NSString *date =
[[dateFormatter stringFromDate:_date]
substringToIndex:12];//用到的精確到分,24小時制60分鐘制
3、申請的appid,和private_key
NSString *appid =
@"15ds45s13a465s";//這裏是樓主隨便輸入的,瞎編的
NSString *private_key =
@"46s4ds_SmartWeatherAPI_45s44d6";//也是瞎編的
4、算出經過urlencode後的key,這步比較重要,步驟也多,請耐心看完。
在原來的的基礎上是在PHP的環境中算出的,代碼如下,可在“
/data/?areaid=101010100&type=index_f&date=201409041509&appid=15ds45s13a465s",
"46s4ds_SmartWeatherAPI_45s44d6",
TRUE)));
首先定義得到public_key和API的方法,還有就是對key進行encodeurl操作的方法
註意,這裏的方法都是被我定義在getTime的類裏面,後面是在main中實例化出來的
//獲得publicky
- (NSString*)
getPublicKey:(NSString*)areaid :(NSString*)type :(NSString*)date
:(NSString*)appid {
NSString *Key = [[NSString alloc]
initWithFormat:@"/data/?areaid=%@&type=%@&date=%@&appid=%@",
areaid, type, [date substringToIndex:12], appid];
return
Key;
}
//獲得完整的API
- (NSString*) getAPI:(NSString*)areaid
:(NSString*)type :(NSString*)date :(NSString*)appid :(NSString*)key
{
NSString *API = [[NSString alloc]
initWithFormat:@"/data/?areaid=%@&type=%@&date=%@&appid=%@&key=%@",
areaid, type, [date substringToIndex:12], [appid substringToIndex:6],
key];
//-------------這裏需要主要的是只需要appid的前6位!!!
return
API;
}
//將獲得的key進性urlencode操作
- (NSString
*)stringByEncodingURLFormat:(NSString*)_key{
NSString *encodedString
= (__bridge NSString
*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)_key,
nil, (CFStringRef) @"!$&'()*+,-./:;=?@_~%#[]",
kCFStringEncodingUTF8);
//由於ARC的存在,這裏的轉換需要添加__bridge,原因我不明。求大神講解
return
encodedString;
}
重點來了,在oc下的算法如下,記得把附件的Base64.h
加進來並引用到工程裏面
//對publickey和privatekey進行加密
- (NSString *)
hmacSha1:(NSString*)public_key :(NSString*)private_key{
NSData*
secretData = [private_key
dataUsingEncoding:NSUTF8StringEncoding];
NSData* stringData = [public_key
dataUsingEncoding:NSUTF8StringEncoding];
const void* keyBytes =
[secretData bytes];
const void* dataBytes = [stringData
bytes];
///#define CC_SHA1_DIGEST_LENGTH 20 /* digest
length in bytes */
void* outs =
malloc(CC_SHA1_DIGEST_LENGTH);
CCHmac(kCCHmacAlgSHA1, keyBytes,
[secretData length], dataBytes, [stringData length], outs);
//
Soluion 1
NSData* signatureData = [NSData dataWithBytesNoCopy:outs
length:CC_SHA1_DIGEST_LENGTH freeWhenDone:YES];
return
[signatureData
base64EncodedString];
}
這裏只是初步算出來的key,還未encodeurl,鏈接不能被瀏覽器識別,所以現在經過算法得到的_key還有壹步操作才能的到真正的key。
NSString *_key = [getTime hmacSha1:[getTime
getPublicKey:areaid :type :date :appid] :private_key];
NSString *key =
[getTime
stringByEncodingURLFormat:_key];
最後壹步了吧!拼接API
NSString *weatherAPI = [getTime getAPI:areaid :type :date
:appid
:key];
//OK,我們的API就可以用啦。
最後,通過API返回的值是JSON文件,通過解析,就能得到我們想要的數據了,下面拿壹個開發的接口舉例
NSDictionary *weatherDic = [getTime
getWeatherDic:@"/data/cityinfo/101010100.html"];
// weatherDic字典中存放的數據也是字典型,從它裏面通過鍵值取值
NSDictionary
*weatherInfo = [weatherDic
objectForKey:@"weatherinfo"];
NSLog(@"今天是 %@ %@ %@ 的天氣狀況是:%@ %@ -
%@",[newDateOne substringWithRange:NSMakeRange(0, 4)],[newDateOne
substringWithRange:NSMakeRange(4, 2)] ,[newDateOne
substringWithRange:NSMakeRange(6, 2)],[weatherInfo
objectForKey:@"weather"],[weatherInfo objectForKey:@"temp1"],[weatherInfo
objectForKey:@"temp2"]);
輸出:2014-09-04 23:40:23.243
WeatherAPP[5688:201108] 今天是 2014-09-04 的天氣狀況是:晴 17℃ - 30℃
weatherInfo字典裏面的內容是--->{"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"17℃","temp2":"30℃","weather":"晴","img1":"n0.gif","img2":"d0.gif","ptime":"18:00"}}