ios - 有没有办法在有人进行应用内购买时收到通知?

标签 ios objective-c in-app-purchase

我希望在有人在我的应用程序中进行应用程序内购买时收到通知,而不是等到第二天再检查 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/

相关文章:

ios - 识别慢速代码以优化构建时间

ios - 文字转语音不起作用?

ios - 为非移动开发人员构建 iPhone 应用程序的最佳移动框架?

iphone - UIPickerView 选择行 :inComponent:animated With smooth animation

android - 实体商品的应用内购买 - 禁止或不推荐?

ios - Swift 通用闭包总和

ios - 准备图像时我应该关心 DPI 和 PPI 吗?

ios - 在滚动期间更改背景颜色

iOS 应用内购买收据,缺少最近的交易

windows-phone-8 - 什么样的 ID 进入 RequestProductPurchaseAsync?