in app purchase implemention - iphone

In My app I have to implement in app purchse in below scenerio:-
App is free for all user i.e every person can download it at free of cost.
I have implemented Consumable type for this.
in application there is a option to purchase 10 coins in $1.99.once user purchase it complete transaction successfully
but when again user click on purchased to 10 coins it prompted that "You have already downloaded this app".
I want to implement in app purchase in that wat so that user can purchase 10 coin s multiple type?
What will be type and scenario for this .
Please suggest?

- (void)requestProductData {
// NSLog(#"%#",strCheck);
if ([strCheck isEqualToString:#"inApp"]) {
myString = strCheck;
SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject:#"All_labelling"]];
request.delegate = self;
[request start];
}
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
NSArray *myProduct = response.products;
SKPayment *payment = [SKPayment paymentWithProduct:[myProduct objectAtIndex:0]];
[[SKPaymentQueue defaultQueue] addPayment:payment];
[request autorelease];
}
- (void)inAppPurchase {
reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
if(remoteHostStatus == NotReachable) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"LeaderFollow!" message:#"Network is not found" delegate:nil cancelButtonTitle:nil otherButtonTitles:#"Ok", nil];
[alert show];
[progInd stopAnimating];
[progInd setHidden:YES];
}
else if ([SKPaymentQueue canMakePayments]) {
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[self requestProductData];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please enable the ability to make purchases." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)finishMyTransaction:(SKPaymentTransaction *)transaction {
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
self.view.userInteractionEnabled=YES;
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
[progInd stopAnimating];
[progInd setHidden:YES];
}
#pragma mark Payment Delegate
#pragma mark -
- (void)recordTransaction:(SKPaymentTransaction *)transaction {
}
- (void)provideContent:(NSString *)productIdentifier {
if([productIdentifier isEqualToString:#"All_labelling"]){
strCheck=#"inApp";
myString=strCheck;
}
if ([strCheck isEqualToString:#"inApp"]){
[prefs setBool:YES forKey:#"inApp"];
if(!isTapped){
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Congratulations!" message:#"You Have Successfully Purchased All Objects Pack" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
[self viewWillAppear:YES];
}
//}
[progInd stopAnimating];
[progInd setHidden:YES];
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
[self recordTransaction:transaction];
[self provideContent:transaction.payment.productIdentifier];
[self finishMyTransaction:transaction];
}
-(IBAction)btnRestoreTapped{
[progInd setHidden:NO];
[progInd startAnimating];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
[[UIApplication sharedApplication]beginIgnoringInteractionEvents];
}
- (void)restoreTransaction: (SKPaymentTransaction *)transaction {
[self recordTransaction:transaction];
[self provideContent:transaction.originalTransaction.payment.productIdentifier];
[self finishMyTransaction:transaction];
}
- (void)failedTransaction: (SKPaymentTransaction *)transaction {
if (transaction.error.code != SKErrorPaymentCancelled) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:[transaction.error localizedDescription]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
[self finishMyTransaction:transaction];
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
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)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue{
[progInd stopAnimating];
[progInd setHidden:YES];
[[UIApplication sharedApplication]endIgnoringInteractionEvents];
if([prefs boolForKey:#"inApp"]){
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"LeaderFollow!" message:#"You Have Successfully Restored Pack(s)" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
[prefs setObject:#"YES" forKey:#"Restore"];
}
else{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"LeaderFollow!" message:#"No purchases to restore" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
isTapped=NO;
}
- (void)paymentQueue:(SKPaymentQueue*)queue restoreCompletedTransactionsFailedWithError:(NSError*)error
{
[[UIApplication sharedApplication]endIgnoringInteractionEvents];
[progInd stopAnimating];
isTapped=NO;
self.view.userInteractionEnabled=YES;
[progInd setHidden:YES];
}

Related

iOS Core Bluetooth Not asking for Pair

In My recent project, I need to communicate a Hardware (Bluetooth Low energy).I have implement all the delegate methods code. I am able to Connect hardware and device, But I am not getting pairing alert (Attached screen shot). Why not it is asking for pairing? Thank you.
#import "BTWCentralConnectionManager.h"
#implementation BTWCentralConnectionManager
#synthesize cbcManager;
#synthesize discoveredPeripheral;
#synthesize findMeServiceCharacteristic;
#synthesize findMeService;
#synthesize delegate=_delegate;
static NSString *kFindMeServiceUUID=#"1802";
static NSString *kFindMeCharacteristicUUID=#"2A06";
static BTWCentralConnectionManager* connectionManager = nil;
+(BTWCentralConnectionManager *)sharedConnectionManager{
#synchronized(self)
{
if (!connectionManager){
connectionManager=[[self alloc] init];
}
return connectionManager;
}
return nil;
}
-(void)findMe {
Byte code=0x02;
if(self.discoveredPeripheral){
[self.discoveredPeripheral writeValue:[NSData dataWithBytes:&code length:1] forCharacteristic:self.findMeServiceCharacteristic type:CBCharacteristicWriteWithoutResponse];
}else{
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"Test" message:#"Invalid Charactersitcs" delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alertView show];
alertView=nil;
}
}
-(void)searchForDevices{
self.cbcManager=[[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
-(void)connect {
NSDictionary* connectOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey];
[self.cbcManager connectPeripheral:self.discoveredPeripheral options:connectOptions];
}
-(void)disconnect{
[self cleanup];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
switch (central.state) {
case CBCentralManagerStatePoweredOn:{
[self.cbcManager scanForPeripheralsWithServices:#[ [CBUUID UUIDWithString:kFindMeServiceUUID] ] options:#{CBCentralManagerScanOptionAllowDuplicatesKey : #NO }];
}
break;
// Scans for any peripheral
default:{
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"Test" message:#"Cental Manager did change state" delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alertView show];
alertView=nil;
}
break;
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
// Stops scanning for peripheral
[self.cbcManager stopScan];
if (self.discoveredPeripheral != peripheral) {
self.discoveredPeripheral = peripheral;
[self.delegate didDeviceDiscoverd:self.discoveredPeripheral.name];
}
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
[self.delegate didDeviceConnectionFailed:error];
[self cleanup];
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
[self.delegate didDeviceConnected];
[self.discoveredPeripheral setDelegate:self];
[self.discoveredPeripheral discoverServices:#[[CBUUID UUIDWithString:kFindMeServiceUUID]]];
}
- (void)peripheral:(CBPeripheral *)aPeripheral didDiscoverServices:(NSError *)error {
if (error) {
NSString *strMsg=[NSString stringWithFormat:#"didDiscoverServices: %#", error];
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"Test" message:strMsg
delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alertView show];
alertView=nil;
[self cleanup];
return;
}
for (CBService *service in aPeripheral.services) {
if ([service.UUID isEqual:[CBUUID UUIDWithString:kFindMeServiceUUID]]) {
self.findMeService=service;
[self.discoveredPeripheral discoverCharacteristics:#[[CBUUID UUIDWithString:kFindMeCharacteristicUUID]] forService:self.findMeService];
}
}
}
- (void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if(error){
NSString *strMsg=[NSString stringWithFormat:#"didDiscoverCharacteristicsForService: %#", error];
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"Test" message:strMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alertView show];
alertView=nil;
}
for(CBCharacteristic *character in [service characteristics])
{
if([[service UUID] isEqual:[CBUUID UUIDWithString:kFindMeServiceUUID]] &&
[[character UUID] isEqual:[CBUUID UUIDWithString:kFindMeCharacteristicUUID]])
{
NSString *strMsg=[NSString stringWithFormat:#"didDiscoverCharacteristicsForService: %#", character];
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"Test" message:strMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alertView show];
alertView=nil;
self.findMeServiceCharacteristic = character;
}
}
}
- (void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
NSString *strMsg=[NSString stringWithFormat:#"Did update value for characteristic %#, new value: %#, error: %#", characteristic, [characteristic value], error];
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"Test" message:strMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alertView show];
alertView=nil;
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if (error) {
NSLog(#"Error changing notification state: %#", error.localizedDescription);
}
// Exits if it's not the transfer characteristic
if (![characteristic.UUID isEqual:[CBUUID UUIDWithString:kFindMeCharacteristicUUID]]) {
return;
}
NSString *strMsg=[NSString stringWithFormat:#"didUpdateNotificationStateForCharacteristic %#, reason: %#", characteristic, error];
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"Test" message:strMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alertView show];
alertView=nil;
}
- (void) peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
if (error)
{
NSString *strMsg=[NSString stringWithFormat:#"Failed to write value for characteristic %#, reason: %#", characteristic, error];
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"Test" message:strMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alertView show];
alertView=nil;
}
else
{
NSString *strMsg=[NSString stringWithFormat:#"Did write value for characterstic %#, new value: %#", characteristic, [characteristic value]];
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:#"Test" message:strMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alertView show];
alertView=nil;
}
}
- (void)cleanup
{
if (!self.discoveredPeripheral.isConnected) {
return;
}
if (self.discoveredPeripheral.services != nil) {
for (CBService *service in self.discoveredPeripheral.services) {
if (service.characteristics != nil) {
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kFindMeServiceUUID]]) {
if (characteristic.isNotifying) {
[self.discoveredPeripheral setNotifyValue:NO forCharacteristic:characteristic];
return;
}
}
}
}
}
}
[self.cbcManager cancelPeripheralConnection:self.discoveredPeripheral];
[self.delegate didDeviceDisconnected];
}
#end
`
If I understand you right you can write a value successfully to a characteristic but you don't get a pairing request.
The pairing is triggered by the peripheral. Meaning the peripheral has to refuse the write or read request of your central to a characteristic. Your central gets the refusal "unauthorized authentication" and then tries to pair with the peripheral and showing the pairing alert pop up you are waiting for. This is all done by core bluetooth automatically. The only thing you need to do is change the characteristics options and permissions in your peripheral. This is the apple sample code to trigger a pairing:
emailCharacteristic = [[CBMutableCharacteristic alloc]
initWithType:emailCharacteristicUUID
properties:CBCharacteristicPropertyRead
| CBCharacteristicPropertyNotifyEncryptionRequired
value:nil permissions:CBAttributePermissionsReadEncryptionRequired];
source: CoreBluetooth_concepts iOS7 preview
Also check out the WWDC 2012 advanced core bluetooth video at 28 minutes they explain the concept of pairing.

update status in linkedin through iphone app

This is my code so far:
-(IBAction)postToLinkedin{
rdEngine = [RDLinkedInEngine engineWithConsumerKey:LI_kOAuthConsumerKey consumerSecret:LI_kOAuthConsumerSecret delegate:self] ;
RDLinkedInAuthorizationController* controller = [RDLinkedInAuthorizationController authorizationControllerWithEngine:rdEngine delegate:self];
if( controller) {
[self presentModalViewController:controller animated:YES];
}
else
{
NSLog(#"alreadyy authenticated");
}
}
-(void)linkedInEngineAccessToken:(RDLinkedInEngine *)engine setAccessToken:(OAToken *)token {
NSLog(#"1111111");
[token storeInUserDefaultsWithServiceProviderName:#"LinkedIn" prefix:#"Demo"];
}
- (void)linkedInEngineAccessToken:(RDLinkedInEngine *)engine removeAccessToken:(OAToken *)token {
[token removeAccessTokenUsingServiceProviderName:#"LinkedIn" prefix:#"Demo"];
}
- (OAToken *)linkedInEngineAccessToken:(RDLinkedInEngine *)engine {
return [[OAToken alloc] initWithUserDefaultsUsingServiceProviderName:#"LinkedIn" prefix:#"Demo"] ;
NSLog(#"3333");
}
- (void)linkedInEngine:(RDLinkedInEngine *)engine requestSucceeded:(RDLinkedInConnectionID *)identifier withResults:(id)results {
NSLog(#"++ LinkedIn engine reports success for connection %#\n%#", identifier, results);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Success" message:#"Successfully Tweeted" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}
- (void)linkedInEngine:(RDLinkedInEngine *)engine requestFailed:(RDLinkedInConnectionID *)identifier withError:(NSError *)error {
NSLog(#"++ LinkedIn engine reports failure for connection %#\n%#", identifier, [error localizedDescription]);
}
//#pragma mark -
//#pragma mark RDLinkedInAuthorizationControllerDelegate
- (void)linkedInAuthorizationControllerSucceeded:(RDLinkedInAuthorizationController *)controller {
NSLog(#"Authentication succeeded.");
NSLog(#"Fetching current user's profile on connection %#", [controller.engine profileForCurrentUser]);
[rdEngine updateStatus:#"hello"];
}
- (void)linkedInAuthorizationControllerFailed:(RDLinkedInAuthorizationController *)controller {
NSLog(#"Authentication failed!");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Error occurred while updating Linkedin status." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}
- (void)linkedInAuthorizationControllerCanceled:(RDLinkedInAuthorizationController *)controller {
NSLog(#"Authentication was cancelled.");
}
The code is not working because the status is not getting updated.
could you please tell me where I am going wrong?
Why are you showing the authorization controller in postToLinkedIn?
You might want to take a look at the Share API here https://developer.linkedin.com/documents/share-api

Implementing in app purchase and its restoration in iphone

I have implemented in app purchase in one of my application using MKStoreManager.Now got a new guideline from apple that if you are doing in app purchase,you have to give the user,the option for restoring the already purchased application.So i have done like this.On the 'restore' button click,this method is called.
- (void) checkPurchasedItems
{
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
and from here,this method is fired
- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSMutableArray* purchasableObjects = [[NSMutableArray alloc] init];
NSLog(#"received restored transactions: %i", queue.transactions.count);
for (SKPaymentTransaction *transaction in queue.transactions)
{
NSString *productID = transaction.payment.productIdentifier;
[purchasableObjects addObject:productID];
}
}
But now i have a doubt that how can i check this restoration is working or not.Can anyone guide me.thanks in advance.
Here is how you implement the Restoration
- (void)loadStore
{
// restarts any purchases if they were interrupted last time the app was open
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
// get the product description (defined in early sections)
[self requestProUpgradeProductData];
}
- (void)requestProUpgradeProductData
{
NSSet *productIdentifiers = [NSSet setWithObject:kInAppPurchaseProUpgradeProductId];
productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
productsRequest.delegate = self;
[productsRequest start];
// we will release the request object in the delegate callback
}
After this it will call this method
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSArray *products = response.products;
proUpgradeProduct = [products count] == 1 ? [[products objectAtIndex:0] retain] : nil;
if (proUpgradeProduct)
{
NSLog(#"Product title: %#" , proUpgradeProduct.localizedTitle);
NSLog(#"Product description: %#" , proUpgradeProduct.localizedDescription);
NSLog(#"Product price: %#" , proUpgradeProduct.price);
NSLog(#"Product id: %#" , proUpgradeProduct.productIdentifier);
if ([self canMakePurchases]) {
if ([self respondsToSelector:#selector(purchaseProUpgrade)]) {
[self purchaseProUpgrade];
}
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[self languageSelectedStringForKey:#"Error"] message:#"Cannot connect to Store.\n Please Enable the Buying in settings" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
for (NSString *invalidProductId in response.invalidProductIdentifiers)
{
[SVProgressHUD dismiss];
[cancelButton setEnabled:YES];
[buyNowButton setEnabled:YES];
[restoreButton setEnabled:YES];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"Error occured" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
NSLog(#"Invalid product id: %#" , invalidProductId);
}
// finally release the reqest we alloc/init’ed in requestProUpgradeProductData
[productsRequest release];
[[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction
{
[self recordTransaction:transaction];
[self provideContent:transaction.payment.productIdentifier];
[self finishTransaction:transaction wasSuccessful:YES];
}
- (void)purchaseProUpgrade
{
[SVProgressHUD showInView:self.view status:[self languageSelectedStringForKey:#"Connecting Store"] networkIndicator:YES];
SKPayment *payment = [SKPayment paymentWithProductIdentifier:kInAppPurchaseProUpgradeProductId];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void)recordTransaction:(SKPaymentTransaction *)transaction
{
if ([transaction.payment.productIdentifier isEqualToString:kInAppPurchaseProUpgradeProductId])
{
// save the transaction receipt to disk
[[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt forKey:#"proUpgradeTransactionReceipt" ];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
Finally this method
- (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful: (BOOL)wasSuccessful
{
// remove the transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:transaction, #"transaction" , nil];
if (wasSuccessful)
{
//Write your transaction complete statement required for your project
}

Error Domain=SKErrorDomain Code=3 "Cannot connect to iTunes Store" UserInfo=0x1aaf40 {NSLocalizedDescription=Cannot connect to iTunes Store}

Currently I'm working on in-App Purchase functionality, and I am getting below of the error
"Error Domain=SKErrorDomain Code=3 "Cannot connect to iTunes Store" UserInfo=0x1aaf40 {NSLocalizedDescription=Cannot connect to iTunes Store}"
Here is the step.
1) First I have create a one application "inAppPro" and it is under (Status) : "Prepare for upload"
2) I have added 4 Non-Consumable product. and also fill related details.
3) I have also create test user (sandbox) for test in-App purchase product.
4) I have also created provision profile with enable inApp Purchase.
5) I have also created APP ID without (*) wild card.
Here is the code which are currently I'm using.
- (void)viewDidLoad
{
Detail1 *observer = [[Detail1 alloc] init];
[[SKPaymentQueue defaultQueue] addTransactionObserver:observer];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
if ([SKPaymentQueue canMakePayments])
{
NSString *product = [NSString stringWithFormat:#"com.companyname.inAppDemo.module%d",ApplicationDelegate.objectID];
NSLog(#"In-App product for request = %#", product);
SKPayment *payment = [SKPayment paymentWithProductIdentifier:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"MyApp" message:#"You are not authorized to purchase from AppStore"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
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];
default:
break;
}
}
}
- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
if (transaction.error.code != SKErrorPaymentCancelled)
{
// Optionally, display an error here.
NSLog(#"%#",transaction.error);
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
//[[MKStoreManager sharedManager] provideContent: transaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
//[[MKStoreManager sharedManager] provideContent: transaction.originalTransaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
-(IBAction)btnpurchase:(id)sender
{
NSLog(#"ProductStatus = %#", ApplicationDelegate.productStatus);
if ([ApplicationDelegate.productStatus isEqualToString:#"FREE"])
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:#"This is free for you so ENJOY it!!!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alert show];
[alert release];
}
else if ([ApplicationDelegate.productStatus isEqualToString:#"PAID"])
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:#"You have already purchase it!!!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil,nil];
[alert show];
[alert release];
}
else
{
NSLog(#"Detail1 id for product = %d", ApplicationDelegate.objectID);
NSString *product = [NSString stringWithFormat:#"com.companyname.inAppDemo.module%d",ApplicationDelegate.objectID];
NSLog(#"In-App product-id = %#", product);
SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObjects:product,nil]];
request.delegate = self;
[request start];
}
}
Please anyone help me out.
Thanks in advance.
You must have valid contracts in effect in the "Contracts, Tax, and Banking" Section.
And of course, make sure that you use a correct Provisioning Profile and that you have enabled in App Purchase for that ID, and (last) that you have added purchasable items in iTunes Connect (which you did, as your screenshots show).

Inapp Purchase is succeeded but content is not unlock in iPhone app.. Why?

In My iphone app i have used In App purchase. All the things are working and the contents are in Resource folder. After successful transaction i have used a Sql Query to insert data in Sqlite database. I have Uploaded this app in App Store and find that after successful transaction the payment is taken from users but the content is not inserted in database and not Showed in app. Please help me. i am stressed on this Point. Due to this i have removed my app form App Store.
In this code after successful finding list of In App Purchases i am using method
For better Understanding i am putting my Some code here:
- (void)insertNewObject {
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
// NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
-(void)updateObject {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
abort();
}
}
#pragma mark -
#pragma mark Store request
- (void) requestProductData {
request= [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:[arrayProductID objectAtIndex:b-2]]];//#"Scaringmirror11"
request.delegate = self;
[request start];
}
- (void) startPurchase {
//int newB=b-2;
SKPayment *payment = [SKPayment paymentWithProductIdentifier:[arrayProductID objectAtIndex:b-2]];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
#pragma mark Store delegate
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
NSArray *myProduct = response.products;
if (myProduct == nil || [myProduct count] == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:#"Missing product from App store.\n"
delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}
SKProduct *product;
BOOL existProduct = NO;
for (int i=0; i<[myProduct count]; i++) {
product = (SKProduct*)[myProduct objectAtIndex:0];
if ([product.productIdentifier isEqualToString:[arrayProductID objectAtIndex:b-2]]) {
existProduct = YES;
//[[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt forKey:#"proUpgradeTransactionReceipt" ];
//[[NSUserDefaults standardUserDefaults] synchronize];
break;
}
}
if (existProduct == NO) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:#"Missing product from App store.No match Identifier found.\n"
delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}
[request autorelease];
[self startPurchase];
}
#pragma mark Store delegate
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
NSArray *myProduct = response.products;
if (myProduct == nil || [myProduct count] == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:#"Missing product from App store.\n"
delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}
SKProduct *product;
BOOL existProduct = NO;
for (int i=0; i<[myProduct count]; i++) {
product = (SKProduct*)[myProduct objectAtIndex:0];
if ([product.productIdentifier isEqualToString:[arrayProductID objectAtIndex:b-2]]) {
existProduct = YES;
//[[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt forKey:#"proUpgradeTransactionReceipt" ];
//[[NSUserDefaults standardUserDefaults] synchronize];
break;
}
}
if (existProduct == NO) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:#"Missing product from App store.No match Identifier found.\n"
delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[alert show];
[alert release];
return;
}
[request autorelease];
[self startPurchase];
}
#pragma mark observer delegate
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
NSLog(#"Success");
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
NSLog(#"Failed");
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
default:
break;
}
}
}
- (void) completeTransaction: (SKPaymentTransaction *)transaction {
//[[NSUserDefaults standardUserDefaults] setBool:YES forKey:REGISTRATION_KEY];
registered = YES;
//NSData *receiptData = [transaction transactionReceipt];
//NSString *str =[NSString string
//[[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt forKey:#"proUpgradeTransactionReceipt" ];
//[self registeredEnable];
// Remove the transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
[sqlite insertIntoScaryImage:[arrayGetMoreScaryImage objectAtIndex:b] add:[arrayGetMoreScarySound objectAtIndex:b]];
}
- (void) restoreTransaction: (SKPaymentTransaction *)transaction {
//[[NSUserDefaults standardUserDefaults] setBool:YES forKey:REGISTRATION_KEY];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:#"Purchase success."
delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[alert show];
[alert release];
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
//[sq];
}
- (void) failedTransaction: (SKPaymentTransaction *)transaction {
if (transaction.error.code != SKErrorPaymentCancelled)
{
// Optionally, display an error here.
NSString *stringError = [NSString stringWithFormat:#"Payment Cancelled\n\n%#", [transaction.error localizedDescription]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:ALERT_TITLE message:stringError
delegate:self cancelButtonTitle:#"Close" otherButtonTitles:nil];
[alert show];
[alert release];
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
Please have a look and tell me where should i insert data in database so that i will able to provide data to users after Successful in app Purchase.
Thanks in Advance
You should implement your unlocking functionality into the payment observer, right after you receive confirmation that the payment is all right.
Your implementation of the observer is kinda strange, I guess it's not working.
EDIT:
your observer class
#import Foundation/Foundation.h
#import StoreKit/StoreKit.h
#interface InAppPurchaseObserver : NSObject {
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions;
#end
#import "InAppPurchaseObserver.h"
#implementation InAppPurchaseObserver
// The transaction status of the SKPaymentQueue is sent here.
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for(SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
// Item is still in the process of being purchased
NSLog(#"Processing!!!");
break;
case SKPaymentTransactionStatePurchased:
// Item was successfully purchased!
// --- UNLOCK FEATURE OR DOWNLOAD CONTENT HERE ---
// The purchased item ID is accessible via
// transaction.payment.productIdentifier
// After customer has successfully received purchased content,
// remove the finished transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
break;
case SKPaymentTransactionStateRestored:
// Verified that user has already paid for this item.
// Ideal for restoring item across all devices of this customer.
// --- UNLOCK FEATURE OR DOWNLOAD CONTENT HERE ---
// The purchased item ID is accessible via
// transaction.payment.productIdentifier
// After customer has restored purchased content on this device,
// remove the finished transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
break;
case SKPaymentTransactionStateFailed:
// Purchase was either cancelled by user or an error occurred.
NSLog(#"Failed!!!");
if (transaction.error.code != SKErrorPaymentCancelled) {
// A transaction error occurred, so notify user.
NSLog(#"Cancelled!!!");
}
// Finished transactions should be removed from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
break;
}
}
}
#end
implementation
- (void) requestProductData {
inappObserver = [[InAppPurchaseObserver alloc] init];
request= [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:[arrayProductID objectAtIndex:b-2]]];//#"Scaringmirror11"
request.delegate = self;
[request start];
}
- (void) startPurchase {
//int newB=b-2;
SKPayment *payment = [SKPayment paymentWithProductIdentifier:[arrayProductID objectAtIndex:b-2]];
[[SKPaymentQueue defaultQueue] addTransactionObserver:inappObserver];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}