InAppPurchase 从另一个 nsobject
我建立了一个应用程序内购买并且运行良好。
然后我创建了一个新的 Objective C 类,并将应用内购买的所有编码放入新类中。
好的,现在我想从我的主视图控制器调用应用程序内购买。这怎么可能?感谢
viewcontroller.m
- (void)viewDidLoad
{
InAppPurchaseManager *IAPmanager = [[InAppPurchaseManager alloc]init];
if([SKPaymentQueue canMakePayments])
{
//sets payment observer to class.
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
//initializes products array.
products = [[NSMutableArray alloc]init];
[IAPmanager loadStore];
}
else
{
NSLog(@"failed");
}
}
InAppPurchaseManager.m
-(void)loadStore
{
//restarts any purchases if they were interupted last time the was open
[[SKPaymentQueue defaultQueue]addTransactionObserver:self];
//get the product description
ViewController *viewcontroller = [[ViewController alloc]init];
[viewcontroller requestProductData];
}
//request product
-(void)requestProductData
{
NSSet *productIdentifiers = [NSSet setWithObjects:@"com.company.myApp.App1",nil ];
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
productsRequest.delegate = self;
[productsRequest start];
NSLog(@"product request start");
}
它在此之前停止运行
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
myproducts = response.products;
for(proUpgradeProduct in myproducts)
{
//debug log.
NSLog(@"Product title: %@" , proUpgradeProduct.localizedTitle);
NSLog(@"Product description: %@" , proUpgradeProduct.localizedDescription);
NSLog(@"Product price: %@" , proUpgradeProduct.price);
NSLog(@"Product id: %@" , proUpgradeProduct.productIdentifier);
}
// invalid purchase
for (NSString *invalidProductId in response.invalidProductIdentifiers)
{
NSLog(@"Invalid product id: %@" , invalidProductId);
}
[[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];
NSLog(@"receive respond");
}
i built an in app purchase and it work fine.
then i created a new objective c class and put all the coding for in app purchase in the new class.
ok, now i want to call the in app purchase from my main view controller. how is it possible? thanks
viewcontroller.m
- (void)viewDidLoad
{
InAppPurchaseManager *IAPmanager = [[InAppPurchaseManager alloc]init];
if([SKPaymentQueue canMakePayments])
{
//sets payment observer to class.
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
//initializes products array.
products = [[NSMutableArray alloc]init];
[IAPmanager loadStore];
}
else
{
NSLog(@"failed");
}
}
InAppPurchaseManager.m
-(void)loadStore
{
//restarts any purchases if they were interupted last time the was open
[[SKPaymentQueue defaultQueue]addTransactionObserver:self];
//get the product description
ViewController *viewcontroller = [[ViewController alloc]init];
[viewcontroller requestProductData];
}
//request product
-(void)requestProductData
{
NSSet *productIdentifiers = [NSSet setWithObjects:@"com.company.myApp.App1",nil ];
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
productsRequest.delegate = self;
[productsRequest start];
NSLog(@"product request start");
}
it stopped running before this
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
myproducts = response.products;
for(proUpgradeProduct in myproducts)
{
//debug log.
NSLog(@"Product title: %@" , proUpgradeProduct.localizedTitle);
NSLog(@"Product description: %@" , proUpgradeProduct.localizedDescription);
NSLog(@"Product price: %@" , proUpgradeProduct.price);
NSLog(@"Product id: %@" , proUpgradeProduct.productIdentifier);
}
// invalid purchase
for (NSString *invalidProductId in response.invalidProductIdentifiers)
{
NSLog(@"Invalid product id: %@" , invalidProductId);
}
[[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];
NSLog(@"receive respond");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您想要的任何类中调用此方法
- (IBAction) buy:(id)sender
{
InAppPurchaseManager *inapp = [[InAppPurchaseManager alloc] init];
[应用内加载存储];
}
}
Call This method from any class you want-
-(IBAction) purchase:(id)sender
{
InAppPurchaseManager *inapp = [[InAppPurchaseManager alloc] init];
[inapp loadStore];
}
}