AppCommons.h
#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>
@interface AppCommons : NSObject <SKProductsRequestDelegate,SKRequestDelegate>
- (void) getInAppPrice : (NSString *) inAppIdentifier;
@end
AppCommons.m
#import "Config.h"
#import "AppCommons.h"
#import "AppDelegate.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
@implementation AppCommons
SKProductsRequest *productsRequest;
- (void) getInAppPrice : (NSString *) inAppIdentifier {
NSSet *productIdentifiers = [NSSet setWithObject:inAppIdentifier ];
productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
productsRequest.delegate = self;
[productsRequest start];
// we will release the request object in the delegate callback
}
#pragma mark -
#pragma mark SKProductsRequestDelegate methods
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
for(SKProduct *product in response.products)
{
NSLog(@"Product Price : %@",product.price);
NSLog(@"Product Price Locale : %@",product.priceLocale);
}
}
@end
这是我上课的目的,是要获得应用内购买的费用。我正在使用下面的行从另一个类中调用getInAppPrice方法
AppCommons *appCommon = [[AppCommons alloc] init];
[appCommon getInAppPrice:inAppProductIdentifier];
收到响应时出现以下错误
“ *-[AppCommons responsesToSelector:]:消息发送到已释放实例0xd4f0580
请帮助..谢谢。
最佳答案
我怀疑您是在方法范围内执行此操作,如下所示:
- (void)myMethod {
AppCommons *appCommon = [[AppCommons alloc] init];
[appCommon getInAppPrice:inAppProductIdentifier];
}
这意味着
appCommon
将在方法返回时被销毁。而是创建一个实例变量或属性,以使
AppCommons
对象的生存期超过此方法调用。MyClass.h:
@class AppCommons;
@interface MyClass : NSObject {
AppCommons *_appCommons;
}
...
@end
MyClass.m:
#import "MyClass.h"
#import "AppCommons.h"
@implementation MyClass
- (id)init {
self = [super init];
if (self) {
_appCommons = [[AppCommons alloc] init];
}
return self;
}
- (void)myMethod {
[_appCommons getInAppPrice:inAppProductIdentifier];
}
@end
关于ios - [AppCommons responsesToSelector:]:发送到已释放实例的消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21384592/