baseStr = [[NSString alloc] initWithData:[QNUrlSafeBase64 decodeString:HIMAStr] encoding:NSUTF8StringEncoding]; [QNUrlSafeBase64 decodeString:HIMAStr]是有值的呀回來卻是nil 但是為什麼這到底是為什麼?這是我的程式碼 請大神們指著!感激不盡! ! !
PHPz2017-04-25 09:05:48
運氣好,剛好最近也用到七牛:
- (NSString *)genToken
{
static NSUInteger expireTime = 0;
static NSString *cachedSign = nil;
NSUInteger now = (NSUInteger)[NSDate date].timeIntervalSince1970;
if (now > expireTime) {
expireTime = now + 3600 - 60;
NSDictionary *policy = @{
@"scope": _bucket,
@"deadline": @(expireTime + 60),
@"returnBody": @"{\"key\":$(key),\"name\":$(fname),\"size\":$(fsize),\"w\":$(imageInfo.width),\"h\":$(imageInfo.height),\"hash\":$(etag)}"
};
NSString *encodedPolicy = [[NSJSONSerialization dataWithJSONObject:policy
options:0
error:NULL] base64EncodedStringWithOptions:0];
encodedPolicy = [encodedPolicy stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
encodedPolicy = [encodedPolicy stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
const char *text = [encodedPolicy cStringUsingEncoding:NSASCIIStringEncoding];
const char *key = [_secretKey cStringUsingEncoding:NSASCIIStringEncoding];
char cHMAC[CC_SHA1_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA1, key, strlen(key), text, strlen(text), cHMAC);
NSString *sign = [[NSString alloc] initWithData:[NSData dataWithBytes:cHMAC
length:sizeof(cHMAC)]
encoding:NSASCIIStringEncoding];
NSString *encodedSign = sign.toBase64String;
encodedSign = [encodedSign stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
encodedSign = [encodedSign stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
cachedSign = [NSString stringWithFormat:@"%@:%@:%@", _accessKey, encodedSign, encodedPolicy];
}
return cachedSign;
}