Navi
Navi

Reputation: 1686

How can I call a category method from my UIViewcontroller class?

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

Upvotes: 0

Views: 3608

Answers (4)

shwetha p
shwetha p

Reputation: 165

 NSString *tsr=[NSString base64StringFromData:transaction.transactionReceipt length:[transaction.transactionReceipt length]];
    NSLog(@"%@",tsr);

Upvotes: 1

NoahJi
NoahJi

Reputation: 1

change method:

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

to:

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

Because the receiptStr did not init.

Upvotes: 0

Florian Mielke
Florian Mielke

Reputation: 3360

First you need to import your category in your UIViewController implementation file

#import "NSString+addition.h"

In your method you create a new string object and call the method like all other NSString methods:

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

Please also note that there is are some coding-conventions regarding categories:

  1. The name of a category starts with a capital letter. In your case you should rename it to Additions

  2. The file name has the following format NameOfClassToAddTheCategoryTo+DescriptiveNameOfNewFunctionality. In your case it might be "NSString+Base64".


Edit: Due to the new information posted, please first read the Apple documentation regarding Categories.

Then change your base64StringFromData:dataObject:length: method to be a class method (What is the difference between class and instance methods?):

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

Finally you can call

- (void)completeTransaction:(SKPaymentTransaction *)transaction 
{
    [...]
    NSData *receiptData = [transaction transactionReceipt];
    int receiptDataLength = [transaction transactionReceipt] length];
    NSString *base64String = [NSString base64StringFromData:receiptData length:receiptDataLength];
    [...]
}

Upvotes: 1

lakshmen
lakshmen

Reputation: 29094

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.

Upvotes: 4

Related Questions