0

ERROR

enter image description here

Hi I have a category with base64 encoding, I need to call thing from my main class method. The following is my category

CODE

#import <Foundation/Foundation.h>

@interface NSString (addition)
- (NSString *) base64StringFromData:(NSData *)data length:(int)length;

@end

in .m file

#import "NSString+addition.h"
static char base64EncodingTable[64] = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
    'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
};

@implementation NSString (addition)

- (NSString *) base64StringFromData: (NSData *)data length: (int)length {
    unsigned long ixtext, lentext;
    long ctremaining;
    unsigned char input[3], output[4];
    short i, charsonline = 0, ctcopy;
    const unsigned char *raw;
    NSMutableString *result;

    lentext = [data length];
    if (lentext < 1)
        return @"";
    result = [NSMutableString stringWithCapacity: lentext];
    raw = [data bytes];
    ixtext = 0;

    while (true) {
        ctremaining = lentext - ixtext;
        if (ctremaining <= 0)
            break;
        for (i = 0; i < 3; i++) {
            unsigned long ix = ixtext + i;
            if (ix < lentext)
                input[i] = raw[ix];
            else
                input[i] = 0;
        }
        output[0] = (input[0] & 0xFC) >> 2;
        output[1] = ((input[0] & 0x03) << 4) | ((input[1] & 0xF0) >> 4);
        output[2] = ((input[1] & 0x0F) << 2) | ((input[2] & 0xC0) >> 6);
        output[3] = input[2] & 0x3F;
        ctcopy = 4;
        switch (ctremaining) {
            case 1:
                ctcopy = 2;
                break;
            case 2:
                ctcopy = 3;
                break;
        }

        for (i = 0; i < ctcopy; i++)
            [result appendString: [NSString stringWithFormat: @"%c", base64EncodingTable[output[i]]]];

        for (i = ctcopy; i < 4; i++)
            [result appendString: @"="];

        ixtext += 3;
        charsonline += 4;

        if ((length > 0) && (charsonline >= length))
            charsonline = 0;
    }     
    return result;
}

@end

My UIViewcontroller methode CODE

I need to call ategory method from this method

- (void)completeTransaction:(SKPaymentTransaction *)transaction {
    NSLog(@"completeTransaction...");

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Transaction completed." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    [self provideContentForProductIdentifier:transaction.payment.productIdentifier];
    NSLog(@"naveen%@",transaction.transactionReceipt);

   //CALL CATEGORY METHOD HERE

    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}

enter image description here

4

4 回答 4

4

First import the category into the viewcontroller you need like this:

#import "NSString+addition.h"

Calling it:

NSString *newString = [myString base64StringFromData:data length:length];

Just an example. Change it according to your situation...

Your code shld look like this:

NSString *tsr = [receiptStr base64StringFromData:transaction.transactionReceipt length:[transaction.transactionReceipt length]];

EDIT:

You need to add the category in the "NSString+addition.m" under the Compile Sources in Build Phases tab under Project Settings.

于 2013-07-22T07:23:14.033 回答
1
 NSString *tsr=[NSString base64StringFromData:transaction.transactionReceipt length:[transaction.transactionReceipt length]];
    NSLog(@"%@",tsr);
于 2013-07-22T07:47:20.720 回答
1

UIViewController首先,您需要在实现文件中导入您的类别

#import "NSString+addition.h"

在您的方法中,您创建一个新的字符串对象并像所有其他 NSString 方法一样调用该方法:

- (void)completeTransaction:(SKPaymentTransaction *)transaction 
{
    [...]
    NSString *base64String = [stringToConvert base64StringFromData:dataObject length:length];
    [...]
}

另请注意,有一些关于类别的编码约定:

  1. 类别名称以大写字母开头。在您的情况下,您应该将其重命名为 Additions

  2. 文件名具有以下格式NameOfClassToAddTheCategoryTo+DescriptiveNameOfNewFunctionality。在您的情况下,它可能是“NSString+Base64”。


编辑:由于发布的新信息,请先阅读有关Categories的 Apple 文档。

然后将您的base64StringFromData:dataObject:length:方法更改为类方法(类方法和实例方法有什么区别?):

+ (NSString *)base64StringFromData:(NSData *)data length:(int)length 
{
    [...]
    return [result copy];
}

最后你可以打电话

- (void)completeTransaction:(SKPaymentTransaction *)transaction 
{
    [...]
    NSData *receiptData = [transaction transactionReceipt];
    int receiptDataLength = [transaction transactionReceipt] length];
    NSString *base64String = [NSString base64StringFromData:receiptData length:receiptDataLength];
    [...]
}
于 2013-07-22T07:32:54.350 回答
0

改变方法:

- (NSString *) base64StringFromData: (NSData *)data length: (int)length;

至:

+ (NSString *) base64StringFromData: (NSData *)data length: (int)length;

因为receiptStr没有初始化。

于 2013-07-22T08:38:44.040 回答