我希望在有人在我的应用程序中进行应用程序内购买时收到通知,而不是等到第二天再检查 iTunes Connect 以了解我是否有任何销售。
有谁知道这样做的方法吗?如果没有,那就太酷了!
谢谢
最佳答案
跟踪 StoreKit 购买事件
购买发生时,给自己发送一个数据点(在此处跟踪...)
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transation in transactions {
switch transation.transactionState {
case .purchased:
queue.finishTransaction(transation)
// Track here...
case .purchasing: break
case .restored: break
case .deferred: break
case .failed: break
}
}
}
利用库
使用分析。将上面的 //Track here...
注释替换为下面的任何 block 。按字母顺序排列的非详尽列表:
参与
NSString *currencyCode = [<SKProduct.priceLocale>
objectForKey:NSLocaleCurrencyCode];
BMA4SPurchasedItem* item =
[BMA4SPurchasedItem itemWithId(t.payment.productIdentifier)
label:t.payment.productName
category:<product.type>
price:@(<SKProduct.price>)
quantity:t.payment.quantity
];
[BMA4STracker trackPurchaseWithId:transaction.identifier
currency:currencyCode
items:@[item]];
分支机构
NSDictionary *state = @{
@"itemId": @(t.payment.productIdentifier),
@"price": <SKProduct.price>,
@"itemName": <SKProduct.name>,
@"currency":currencyCode };
[[Branch getInstance] userCompletedAction:@"purchase" withState:state];
织物(Crashlytics)
NSString *currencyCode = [<SKProduct.priceLocale>
objectForKey:NSLocaleCurrencyCode];
[Answers logPurchaseWithPrice:<SKProduct.price>
currency:currencyCode
success:@YES
itemName:<product name>
itemType:@"Purchase"
itemId:@(t.payment.productIdentifier)
customAttributes:@{}];
飞行记录器
FlightRecorder.sharedInstance().trackEventWithCategory(
"Actions",
action: "Purchase",
label: "productIdentifier",
value: t.payment.productIdentifier)
Flurry 分析
let properties = ["productIdentifier":t.payment.productIdentifier]
Flurry.logEvent("Purchase", withParameters: properties)
谷歌分析
#import "GAI.h"
#import "GAIDictionaryBuilder.h"
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
NSString *currencyCode = [<SKProduct.priceLocale>
objectForKey:NSLocaleCurrencyCode];
[tracker send:[[GAIDictionaryBuilder
createItemWithTransactionId:transactionIdentifier
name:<product.localizedTitle>
sku:t.payment.productIdentifier
category:@"Purchase"
price:<SKProduct.price>
quantity:@(t.payment.quantity)
currencyCode:currencyCode]
build]];
参见 In-app purchase tracking with Google Analytics iOS SDK .
堆分析
[Heap track:@"Purchase"
withProperties:@{@"productIdentifier":@(t.payment.productIdentifier)}
];
Mixpanel 分析(*)
Mixpanel.sharedInstance().track("Purchased",
properties: ["productIdentifier":transation.payment.productIdentifier])
properties:@{@"productIdentifier":@(t.payment.productIdentifier)};
(*) 提供对 WiFi 报告的支持(允许推迟所有报告,直到 WiFi 网络可用,而不使用蜂窝数据)。请参阅下面的 mixpanelWillFlush
。
Parse.com
NSDictionary *dimensions =
@{@"productIdentifier":@(t.payment.productIdentifier)};
[PFAnalytics trackEvent:@“Purchase” dimensions:dimensions];
从服务器发送邮件
POST
购买到 URL,然后服务器向您发送邮件或其他通知。
使用 URLSession
的 iOS 实现:
if let url = URL(string: "https://<yoursite>/php/purchase.php") {
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody =
"{\"productIdentifier\":\"\(transaction.payment.productIdentifier)\"}"
.data(using: .utf8)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: request as URLRequest,
completionHandler: {_,_,_ in })
task.resume()
}
purchase.php
电子邮件发件人:
<?php
try {
header('Content-type: application/json');
$to = 'bounce@stackoverflow.com';
$subject = 'Purchase';
$message = $_POST['productIdentifier'];
$headers = "From: " . $to . "\n";
@mail($to, $subject, $message, $headers)
} catch (Exception $e) {}
?>
► 在 GitHub 上找到此解决方案以及有关 Swift Recipes 的更多详细信息.
关于ios - 有没有办法在有人进行应用内购买时收到通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32021855/