如何获得的iOS appStoreReceiptURL进入基地64恩codeD字符串? [英] How to get iOS appStoreReceiptURL into Base 64 Encoded String?

查看:1435
本文介绍了如何获得的iOS appStoreReceiptURL进入基地64恩codeD字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用appStoreReceiptURL看到购买的应用程序的人的版本。我怎样才能成为一个字符串呢?

我被从商店下载应用程序,然后运行从X code中的应用程序的新版本测试这一点。以下是我已经试过:

  NSURL * receiptUrl = [[一个NSBundle mainBundle] appStoreReceiptURL]
的NSLog(@receiptUrl%@,[receiptUrl路径]);
如果([的NSFileManager defaultManager] fileExistsAtPath:[receiptUrl路径]){
        的NSLog(@存在);
         NSError *错误;
         * NSString的receiptString = [[NSString的页头] initWithContentsOfFile:[receiptUrl路径]编码:NSUTF8StringEncoding错误:&放大器;错误]
         如果(receiptString ==无){
              的NSLog(@错误:%@,[错误localizedDescription]);
         }其他{
             的NSLog(@回执:%@,receiptString);
        }}其他{
        的NSLog(@不存在);
}

这是我所得到的:

  receiptUrl的/ var /移动/应用/ E612F261-2D30-416E-BF82-F24xxxx8860 / StoreKit /收据
存在
错误:操作无法完成。 (可可错误261.)


解决方案

我想通了,关键是要阅读收据作为数据基础,然后带64 code将其从数据。这里是为我工作,如果它可以帮助任何人。也感谢这个线程基地64编码: NSData的转换为Base64

  //此方法返回应用程序的商店收据,状态= 0好,一个NSDictionary -1坏
- (*的NSDictionary)getStoreReceipt:(BOOL)沙箱{    NSArray的对象*;
    NSArray的*键;
    * NSDictionary的字典;    BOOL gotreceipt = FALSE;    @try {        NSURL * receiptUrl = [[一个NSBundle mainBundle] appStoreReceiptURL];        如果([的NSFileManager defaultManager] fileExistsAtPath:[receiptUrl路径]){            NSData的* receiptData = [NSData的dataWithContentsOfURL:receiptUrl];            * NSString的receiptString = [自我base64forData:receiptData];            如果(receiptString!=无){                对象= [[NSArray的页头] initWithObjects:receiptString,无]
                键= [[NSArray的页头] initWithObjects:@收据数据零]
                词典= [[NSDictionary的页头] initWithObjects:对象forKeys:键];                * NSString的POSTDATA = [自我getJsonStringFromDictionary:字典]                * NSString的urlSting = @https://buy.itunes.apple.com/verifyReceipt
                如果(沙盒)urlSting = @https://sandbox.itunes.apple.com/verifyReceipt                词典= [自我getJsonDictionaryWithPostFromUrlString:urlSting andDataString:POSTDATA];                如果([字典objectForKey:@状态]!=无){                    如果([字典objectForKey:@状态]的intValue] == 0){                        gotreceipt = TRUE;                    }
                }            }        }    } @catch(NSException * E){
        gotreceipt = FALSE;
    }    如果(!gotreceipt){
        对象= [[NSArray的页头] initWithObjects:@ - 1,无]
        键= [[NSArray的页头] initWithObjects:@状态,无]
        词典= [[NSDictionary的页头] initWithObjects:对象forKeys:键];
    }    返回字典;
} - (*的NSDictionary)getJsonDictionaryWithPostFromUrlString:(* NSString的)urlString andDataString:(* NSString的)dataString {
    * NSString的jsonString = [自我getStringWithPostFromUrlString:urlString andDataString:dataString];
    的NSLog(@%@,jsonString); //看到响应是什么样子
    返回[自我getDictionaryFromJsonString:jsonString];
}
- (*的NSDictionary)getDictionaryFromJsonString:(* NSString的)jsonstring {
    NSError * jsonError;
    *的NSDictionary词典=(的NSDictionary *)[NSJSONSerialization JSONObjectWithData:[jsonstring dataUsingEncoding:NSUTF8StringEncoding]选项:0错误:&放大器; jsonError]。
    如果(jsonError){
       词典= [[NSDictionary的页头]初始化];
    }
    返回字典;
}
- (的NSString *)getStringWithPostFromUrlString:(* NSString的)urlString andDataString:(* NSString的)dataString {
    的NSString * S = @;
    @try {
        NSData的* POSTDATA = [dataString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        * NSString的postlength = [的NSString stringWithFormat:@%D,[POSTDATA长度]];
        NSMutableURLRequest *请求= [[NSMutableURLRequest的alloc]初始化];
        [要求setURL:[NSURL URLWithString:urlString]];
            [要求setTimeoutInterval:60];
        [要求setHTTPMethod:@POST];
        [申请的setValue:postlength forHTTPHeaderField:@内容长度];
        [申请的setValue:@应用程序/ x-WWW的形式urlen codeDforHTTPHeaderField:@的Content-Type];
        [要求setHTTPBody:POSTDATA];
        NSData的*数据= [NSURLConnection的sendSynchronousRequest:要求returningResponse:无错误:无];
        如果(数据!=无){
            S = [[NSString的页头] initWithData:数据编码:NSUTF8StringEncoding];
        }
    }
    @catch(NSException *除外){
        S = @;
    }
    返回S;
}
//从http://stackoverflow.com/questions/2197362/converting-nsdata-to-base64
- (的NSString *)base64forData:(NSData的*){海图
    常量uint8_t有*输入=(常量uint8_t有*)[海图字节]
    NSInteger的长度= [海图长度]
    静态字符表[] =ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + / =;
    NSMutableData *数据= [NSMutableData dataWithLength:((长+ 2)/ 3)* 4];
    uint8_t有*输出=(uint8_t有*)data.mutableBytes;
    NSInteger的我;
    对于(i = 0; I<长度;我+ = 3){
        NSInteger的值= 0;
        NSInteger的焦耳;
        为(J =; J&下;第(i + 3); J ++){
            值<< = 8;            如果(J<长度){
                值| =(0xFF的&安培;输入[J]);
            }
        }
        NSInteger的theIndex =(I / 3)* 4;
        输出[theIndex + 0] =表[(价值>> 18)及0x3F的];
        输出[theIndex + 1] =表[(价值>> 12)及0x3F的];
        输出[theIndex + 2] =第(i + 1) - ;长度 ?表[(价值>> 6)及0x3F的]:'=';
        输出[theIndex + 3] =(I + 2)下;长度 ?表[(价值>> 0)及0x3F的]:'=';
    }
    返回[[的NSString页头] initWithData:数据编码:NSASCIIStringEncoding];
}

I want to use appStoreReceiptURL to see which version of the app someone purchased. How can I get this into a string?

I'm testing this by downloading the app from the store, then running a new version of the app from Xcode. Here is what I've tried:

NSURL *receiptUrl = [[NSBundle mainBundle] appStoreReceiptURL];
NSLog(@"receiptUrl %@",[receiptUrl path]);
if ([[NSFileManager defaultManager] fileExistsAtPath:[receiptUrl path]]) {
        NSLog(@"exists");
         NSError *error;
         NSString *receiptString = [[NSString alloc] initWithContentsOfFile:[receiptUrl path] encoding:NSUTF8StringEncoding error:&error];
         if (receiptString == nil) {
              NSLog(@"Error: %@", [error localizedDescription]);
         } else {
             NSLog(@"Receipt: %@",receiptString);
        }

} else {
        NSLog(@"does not exist");
}

This is what I get:

receiptUrl /var/mobile/Applications/E612F261-2D30-416E-BF82-F24xxxx8860/StoreKit/receipt
exists
Error: The operation couldn’t be completed. (Cocoa error 261.)

解决方案

I figured out the trick is to read the receipt as data then base 64 encode it from the data. Here's what worked for me if it helps anyone else. Also thanks to this thread for the base 64 encoding: Converting NSData to base64

// this returns an NSDictionary of the app's store receipt, status=0 for good, -1 for bad
- (NSDictionary *) getStoreReceipt:(BOOL)sandbox {

    NSArray *objects;
    NSArray *keys;
    NSDictionary *dictionary;

    BOOL gotreceipt = false;

    @try {

        NSURL *receiptUrl = [[NSBundle mainBundle] appStoreReceiptURL];

        if ([[NSFileManager defaultManager] fileExistsAtPath:[receiptUrl path]]) {

            NSData *receiptData = [NSData dataWithContentsOfURL:receiptUrl];

            NSString *receiptString = [self base64forData:receiptData];

            if (receiptString != nil) {

                objects = [[NSArray alloc] initWithObjects:receiptString, nil];
                keys = [[NSArray alloc] initWithObjects:@"receipt-data", nil];
                dictionary = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];

                NSString *postData = [self getJsonStringFromDictionary:dictionary];

                NSString *urlSting = @"https://buy.itunes.apple.com/verifyReceipt";
                if (sandbox) urlSting = @"https://sandbox.itunes.apple.com/verifyReceipt";

                dictionary = [self getJsonDictionaryWithPostFromUrlString:urlSting andDataString:postData];

                if ([dictionary objectForKey:@"status"] != nil) {

                    if ([[dictionary objectForKey:@"status"] intValue] == 0) {

                        gotreceipt = true;

                    }
                }

            }

        }

    } @catch (NSException * e) {
        gotreceipt = false;
    }

    if (!gotreceipt) {
        objects = [[NSArray alloc] initWithObjects:@"-1", nil];
        keys = [[NSArray alloc] initWithObjects:@"status", nil];
        dictionary = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
    }

    return dictionary;
}



- (NSDictionary *) getJsonDictionaryWithPostFromUrlString:(NSString *)urlString andDataString:(NSString *)dataString {
    NSString *jsonString = [self getStringWithPostFromUrlString:urlString andDataString:dataString];
    NSLog(@"%@", jsonString); // see what the response looks like
    return [self getDictionaryFromJsonString:jsonString];
}


- (NSDictionary *) getDictionaryFromJsonString:(NSString *)jsonstring {
    NSError *jsonError;
    NSDictionary *dictionary = (NSDictionary *) [NSJSONSerialization JSONObjectWithData:[jsonstring dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&jsonError];
    if (jsonError) {
       dictionary = [[NSDictionary alloc] init];
    }
    return dictionary;
}


- (NSString *) getStringWithPostFromUrlString:(NSString *)urlString andDataString:(NSString *)dataString {
    NSString *s = @"";
    @try {
        NSData *postdata = [dataString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        NSString *postlength = [NSString stringWithFormat:@"%d", [postdata length]];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:urlString]];
            [request setTimeoutInterval:60];
        [request setHTTPMethod:@"POST"];
        [request setValue:postlength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postdata];
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        if (data != nil) {
            s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        }
    }
    @catch (NSException *exception) { 
        s = @"";
    } 
    return s;
}


// from http://stackoverflow.com/questions/2197362/converting-nsdata-to-base64
- (NSString*)base64forData:(NSData*)theData {
    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];
    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;
    NSInteger i;
    for (i=0; i < length; i += 3) {
        NSInteger value = 0;
        NSInteger j;
        for (j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }
        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] =                    table[(value >> 18) & 0x3F];
        output[theIndex + 1] =                    table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }
    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}

这篇关于如何获得的iOS appStoreReceiptURL进入基地64恩codeD字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆