我在我的应用中设置的应用内购买工作正常,但我想打电话给产品价格'用本地货币到viewController上。我之前已经发布了这个问题,并得到了反馈:
" proUpgradeProduct是一个私有变量。通过添加is作为方法或属性/ sythesize对来使其公开。您的问题与应用内购买无关,而是缺乏基本的客观知识。"
我对此很陌生,所以会感激一些帮助。基本上,productPrice变量在InAppPurchaseSS.m文件中,我想把它作为UILabel称为DIFFERENT viewController。
我有这两个文件:
InAppPurchaseSS.h
#import "StoreKit/StoreKit.h"
#define kProductPurchasedNotification @"ProductPurchased"
#define kProductPurchaseFailedNotification @"ProductPurchaseFailed"
#define kProductPurchaseCancelledNotification @"ProductPurchaseCancelled"
@interface InAppPurchaseSS : NSObject <SKProductsRequestDelegate,SKPaymentTransactionObserver,UIAlertViewDelegate>
{
SKProductsRequest* productsRequest;
SKProduct *proUpgradeProduct;
UIAlertView* waitingAlert;
BOOL isTransactionOngoing;
IBOutlet UILabel *productPriceLabel;
}
@property (retain) SKProductsRequest* productsRequest;
@property (retain) NSArray * products;
@property (retain) SKProductsRequest *request;
@property (assign) BOOL isTransactionOngoing;
+ (InAppPurchaseSS *) sharedHelper;
-(id)init;
- (void)buyProductIdentifier:(NSString *)productIdentifier;
- (BOOL)canMakePurchases;
-(void)restoreInAppPurchase;
@end
InAppPurchaseSS.m
//
#import "InAppPurchaseSS.h"
#import "Reachability.h"
@implementation InAppPurchaseSS
@synthesize products;
@synthesize request;
@synthesize productsRequest;
@synthesize isTransactionOngoing;
static InAppPurchaseSS * _sharedHelper;
+ (InAppPurchaseSS *) sharedHelper {
if (_sharedHelper != nil) {
return _sharedHelper;
}
_sharedHelper = [[InAppPurchaseSS alloc] init];
return _sharedHelper;
}
-(id)init {
if( (self=[super init]))
{
isTransactionOngoing=NO;
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
return self;
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
NSLog(@"Received products results...");
self.products = response.products;
self.request = nil;
NSLog(@"Number of product:%i : %@",[response.products count],response.products);
NSArray *product = response.products;
proUpgradeProduct = [product count] == 1 ? [[product firstObject] retain] : nil;
if (proUpgradeProduct)
{
NSLog(@"Product title: %@" , proUpgradeProduct.localizedTitle);
NSLog(@"Product description: %@" , proUpgradeProduct.localizedDescription);
NSLog(@"Product price: %@" , proUpgradeProduct.price);
NSLog(@"Product id: %@" , proUpgradeProduct.productIdentifier);
}
for (NSString *invalidProductId in response.invalidProductIdentifiers)
{
NSLog(@"Invalid product id: %@" , invalidProductId);
}
[productPriceLabel setText:[NSString stringWithFormat:
@"Product Price: %@",proUpgradeProduct.price]];
}
-(void)restoreInAppPurchase
{
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus == NotReachable) {
NSLog(@"No internet connection!");
[[[UIAlertView alloc] initWithTitle:@"No Internet" message:@"Sorry, no internet connection found" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil] show];
return;
}
waitingAlert = [[UIAlertView alloc] initWithTitle:@"Restoring..." message:@"Please Wait...\n\n" delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
[waitingAlert show];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void)buyProductIdentifier:(NSString *)productIdentifier {
if ([productIdentifier isEqual: @""]) {
NSLog(@"No IAP Product ID specified");
return;
}
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [reach currentReachabilityStatus];
if (netStatus == NotReachable) {
NSLog(@"No internet connection!");
[[[UIAlertView alloc] initWithTitle:@"No Internet" message:@"Sorry, no internet connection found" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil] show];
return;
}
isTransactionOngoing=YES;
waitingAlert = [[UIAlertView alloc] initWithTitle:@"Purchasing..." message:@"Please Wait...\n\n" delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
[waitingAlert show];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0) {
SKMutablePayment *payment = [[SKMutablePayment alloc] init];
payment.productIdentifier = productIdentifier;
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
else {
SKPayment *payment = [SKPayment paymentWithProductIdentifier:productIdentifier];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
}
-(void)enableFeature
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"PurchaseSuccess"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
{
[waitingAlert dismissWithClickedButtonIndex:0 animated:YES];
NSLog(@"Restore completed transaction failed");
}
- (BOOL)canMakePurchases
{
return [SKPaymentQueue canMakePayments];
}
//
- (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful:(BOOL)wasSuccessful
{
isTransactionOngoing=NO;
[waitingAlert dismissWithClickedButtonIndex:0 animated:YES];
// remove the transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
if (wasSuccessful)
{
[self enableFeature];
[[[UIAlertView alloc] initWithTitle:@"Congratulations!!" message:@"You have succesfully Purchases." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil] show];
[[NSNotificationCenter defaultCenter] postNotificationName:@"successbuy" object:self];
}
else
{
[[[UIAlertView alloc] initWithTitle:@"Error!" message:transaction.error.localizedDescription delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil] show];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
[[NSNotificationCenter defaultCenter] postNotificationName:@"TransCancel" object: self];
}
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction
{
NSLog(@"succesfull transaction");
[self finishTransaction:transaction wasSuccessful:YES];
}
- (void)restoreTransaction:(SKPaymentTransaction *)transaction
{
NSLog(@"transaction is restored");
[self finishTransaction:transaction wasSuccessful:YES];
}
// called when a transaction has failed
- (void)failedTransaction:(SKPaymentTransaction *)transaction
{
isTransactionOngoing=NO;
NSLog(@"failed transaction called");
if (transaction.error.code != SKErrorPaymentCancelled)
{
NSLog(@"Transaction failed called");
NSLog(@"Transaction error: %@", transaction.error.localizedDescription);
[self finishTransaction:transaction wasSuccessful:NO];
}
else
{
[waitingAlert dismissWithClickedButtonIndex:0 animated:YES];
NSLog(@"user cancel transaction");
// this is fine, the user just cancelled, so don’t notify
[[NSNotificationCenter defaultCenter] postNotificationName:@"TransCancel" object: self];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
}
#pragma mark -
#pragma mark SKPaymentTransactionObserver methods
// called when the transaction status is updated
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
NSLog(@"transaction status updated");
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
break;
default:
break;
}
}
}
- (void) dealloc
{
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
[super dealloc];
}
@end
我有另一个viewController设置,我想用UILabel调用本地产品价格。我们假设这被称为SecondViewController。我知道在InAppPurchaseSS.m中我有一个proUpgradeProduct.price的字段,但我需要将其称为带有本地货币的UI标签
这可能是一个显而易见的东西我可以忽略,但我很欣赏这方面的帮助
谢谢
答案 0 :(得分:0)
在加载产品的方法中,每个产品都应该有一个包含价格和标签的变量。不幸的是,我不知道哪一个,但我认为这是SKProduct。看它是否包含任何变量。
修改强>
您正在寻找proProduct.localizedLabel
和proProduct.localizedPrice
!
再次编辑:
我再次看了一眼,看到你对你的问题更加准确。我建议你将它们保存为NSString。
例如:
在您的.h文件中创建一个属性。
@interface SomethingController : UIViewController {
//random stuff
}
@property NSString price;
@property NSString title;
@end
现在设置
self.price = proProduct.localizedPrice
和
self.title = proProduct.localizedLabel
.m文件中的某个地方。
现在,在您想要显示这些内容的另一个viewcontroller中,您可以在viewcontroller.m文件中的某个位置创建实例。
首先导入SomethingController.h,其中包含您要查找的变量。
在viewcontroller的viewcontroller.m文件的顶部,您要显示您编写的数据:#import SomethingController.h
然后创建一个实例来获取数据。
SomethingController * something = [SomethingController alloc];
现在,您可以通过编写:
来访问其他viewcontroller中的数据something.price
访问价格和
something.title
访问标签。