1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
| #import "IAPManager.h"
@implementation IAPManager { UIView *_viewBg; UIView *_viewBorder; UIActivityIndicatorView * _activityIndicator; UIWindow *_window; }
-(void) attachObserver{ [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; }
-(BOOL) CanMakePayment{ return [SKPaymentQueue canMakePayments]; }
-(void) requestProductData:(NSString *)productIdentifiers{ NSArray *idArray = [productIdentifiers componentsSeparatedByString:@"\t"]; NSSet *idSet = [NSSet setWithArray:idArray]; [self sendRequest:idSet]; }
-(void)sendRequest:(NSSet *)idSet{ SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:idSet]; request.delegate = self; [request start]; }
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{ NSArray *products = response.products; for (SKProduct *p in products) { UnitySendMessage("IOSIAPMgr", "ShowProductList", [[self productInfo:p] UTF8String]); } }
-(void)buyRequest:(NSString *)productIdentifier{ productIndentify=productIdentifier; SKPayment *payment = [SKPayment paymentWithProductIdentifier:productIdentifier]; [[SKPaymentQueue defaultQueue] addPayment:payment]; [self showWatting]; }
-(NSString *)productInfo:(SKProduct *)product{ NSArray *info = [NSArray arrayWithObjects:product.localizedTitle,product.localizedDescription,product.price,product.productIdentifier, nil]; return [info componentsJoinedByString:@"\t"]; }
NSMutableArray*transArray;
-(void)verifyPurchaseWithPaymentTransaction:(SKPaymentTransaction*)tran{ NSURL *receiptUrl=[[NSBundle mainBundle] appStoreReceiptURL]; NSData *receiptData=[NSData dataWithContentsOfURL:receiptUrl]; NSString *receiptString=[receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]; NSString *receipt1=[receiptString stringByReplacingOccurrencesOfString:@"\\n" withString:@""]; NSString *receipt2=[receipt1 stringByReplacingOccurrencesOfString:@"\\r" withString:@""]; NSString *receipt3=[receipt2 stringByReplacingOccurrencesOfString:@"%2B" withString:@"+"];
NSMutableDictionary *mdic =[NSMutableDictionary dictionaryWithObject:tran.transactionIdentifier forKey:@"TransactionID"]; [mdic setObject:receipt3 forKey:@"Payload"]; NSData *data=[NSJSONSerialization dataWithJSONObject:mdic options:NSJSONWritingPrettyPrinted error:nil]; NSString *str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; if (transArray==nil) { transArray=[NSMutableArray array]; } [transArray addObject:tran]; UnitySendMessage("IOSIAPMgr", "BuyProductSucessCallBack", str.UTF8String); }
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transaction{ for(SKPaymentTransaction *tran in transaction){ switch (tran.transactionState) { case SKPaymentTransactionStatePurchased:{ [self verifyPurchaseWithPaymentTransaction:tran]; } break; case SKPaymentTransactionStatePurchasing:{ [self showWatting]; } break; case SKPaymentTransactionStateRestored:{ [[SKPaymentQueue defaultQueue] finishTransaction:tran]; [self hideWatting]; } break; case SKPaymentTransactionStateFailed:{ UIAlertController *alertview=[UIAlertController alertControllerWithTitle:@"交易提示" message:@"交易失败" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *defult = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]; [alertview addAction:defult]; if (_window==nil) { _window = [UIApplication sharedApplication].keyWindow; } [_window.rootViewController presentViewController:alertview animated:YES completion:nil]; [[SKPaymentQueue defaultQueue] finishTransaction:tran]; [self hideWatting]; } break; default: { [[SKPaymentQueue defaultQueue] finishTransaction:tran]; [self showWatting]; } break; } } }
-(void)gameServerSendProductEnd:(NSString *)transactionID;{ if (transArray==nil||transArray.count==0) { return; } for (int i=0; i<[transArray count]; i++) { SKPaymentTransaction*tran = transArray[i]; if ([tran.transactionIdentifier isEqualToString:transactionID ]) { [transArray removeObject:tran]; [[SKPaymentQueue defaultQueue] finishTransaction:tran]; } } if (transArray.count==0) { [self hideWatting]; } }
-(void)showWatting{ [_activityIndicator startAnimating]; [_window addSubview:_viewBg]; }
-(void)hideWatting{ [_activityIndicator stopAnimating]; [_viewBg removeFromSuperview]; }
-(void)InitWatting{ _window = [UIApplication sharedApplication].keyWindow; _viewBg = [[UIView alloc ] initWithFrame:CGRectMake(0,0, UIScreen.mainScreen.bounds.size.width,UIScreen.mainScreen.bounds.size.height)]; _viewBg.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.01]; _viewBorder = [[UIView alloc ] initWithFrame:CGRectMake(0,0,100,100)]; [_viewBg addSubview:_viewBorder]; _viewBorder.center= CGPointMake(_viewBg.frame.size.width/2.0 ,_viewBg.frame.size.height/2.0); _viewBorder.backgroundColor =[UIColor colorWithRed:0 green:0 blue:0 alpha:0.9]; _viewBorder.layer.cornerRadius=8; _viewBorder.layer.masksToBounds=true; _activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; [_viewBorder addSubview:_activityIndicator]; _activityIndicator.frame= CGRectMake(0,0,200,200); _activityIndicator.center= CGPointMake(_viewBorder.frame.size.width/2.0 ,_viewBorder.frame.size.height/2.0); _activityIndicator.hidesWhenStopped = NO; }
@end
|