iOS:如何检测用户是否订阅了可自动更新的订阅

时间:2023-01-12 21:34:47

Hopefully the title is self-explanatory. I'm trying to do something like this:

希望标题是不言自明的。我正在尝试做这样的事情:

checkIfUserIsSubscribedToProduct(productID, transactionID: "some-unique-transaction-string", completion: { error, status in
    if error == nil {
        if status ==  .Subscribed {
            // do something fun
        }
    }
 }

does anything like the hypothetical code I've provided exist? I feel like I'm taking crazy pills

做了我提供的假设代码吗?我觉得我正在服用疯狂的药片

Edit

编辑

In similar questions I keep seeing a generic answer of "oh you gotta validate the receipt" but no explanation on how, or even what a receipt is. Could someone provide me with how to "validate the receipt"? I tried this tutorial but didn't seem to work.

在类似的问题中,我一直看到一个通用的答案“哦,你必须验证收据”但没有解释如何,甚至收据是什么。有人可以向我提供如何“验证收据”吗?我试过这个教程,但似乎没有用。

Edit - For Bounty

编辑 - 为赏金

Please address the following situation: A user subscribes to my auto-renewable subscription and gets more digital content because of it - cool, implemented. But how do I check whether that subscription is still valid (i.e. they did not cancel their subscription) each time they open the app? What is the simplest solution to check this? Is there something like the hypothetical code I provided in my question? Please walk me through this and provide any further details on the subject that may be helpful.

请解决以下情况:用户订阅我的自动续订订阅并因此获得更多数字内容 - 很酷,实施。但是,如何在每次打开应用程序时检查该订阅是否仍然有效(即他们没有取消订阅)?检查这个的最简单的解决方案是什么?有没有像我在问题中提供的假设代码?请引导我完成此操作,并提供有关该主题的任何进一步详细信息。

3 个解决方案

#1


19  

I know everyone was very concerned about me and how I was doing on this - fear not, solved my problem. Main problem was that I tried the garbage and outdated code in this link, but it wasn't working so I gave up on it. Then I came back to it and implemented it with Alamofire and it works great. Here's the code solution:

我知道每个人都非常关心我以及我在这方面做了什么 - 不要害怕,解决了我的问题。主要问题是我在这个链接中尝试了垃圾和过时的代码,但它没有工作,所以我放弃了它。然后我回到它并用Alamofire实现它并且效果很好。这是代码解决方案:

Swift 3:

斯威夫特3:

let receiptURL = Bundle.main.appStoreReceiptURL
let receipt = NSData(contentsOf: receiptURL!)
let requestContents: [String: Any] = [
    "receipt-data": receipt!.base64EncodedString(options: []),
    "password": "your iTunes Connect shared secret"
]

let appleServer = receiptURL?.lastPathComponent == "sandboxReceipt" ? "sandbox" : "buy"

let stringURL = "https://\(appleServer).itunes.apple.com/verifyReceipt"

print("Loading user receipt: \(stringURL)...")

Alamofire.request(stringURL, method: .post, parameters: requestContents, encoding: JSONEncoding.default)
    .responseJSON { response in
        if let value = response.result.value as? NSDictionary {
            print(value)
        } else {
            print("Receiving receipt from App Store failed: \(response.result)")
        }
}

#2


1  

What are you trying to achieve in particular? Do you want to check for a specific Apple ID?

你想要实现什么目标?您想检查特定的Apple ID吗?

I highly doubt that this is possible through the SDK. Referring to Is it possible to get the user's apple ID through the SDK? you can see that you can't even ask for the ID directly but rather services attached to it.

我非常怀疑这可以通过SDK实现。参考是否可以通过SDK获取用户的苹果ID?你可以看到你甚至不能直接询问身份证,而是附加服务。

What would work is caching all transactions on your own server and search its database locally but that would require the app to ask for the user's Apple ID so the app could update the subscription state whenever it launches as it can check for IAP of the ID associated with the device.

什么工作是缓存您自己的服务器上的所有事务并在本地搜索其数据库,但这将要求应用程序询问用户的Apple ID,以便应用程序可以在启动时更新订阅状态,因为它可以检查相关ID的IAP与设备。

However, the user could just type whatever he wanted - and it's unlikely to get this through Apple's app review process.

但是,用户可以只输入他想要的任何内容 - 而且不太可能通过Apple的应用审核流程获得此信息。

#3


1  

I am using MKSoreKit https://github.com/MugunthKumar/MKStoreKit for auto-renew subscriptions.but it is in objective c you can check the library code for solution.I am using it in my code and it is working fine.

我正在使用MKSoreKit https://github.com/MugunthKumar/MKStoreKit进行自动续订订阅。但是在目标c中你可以检查库代码的解决方案。我在我的代码中使用它并且它工作正常。

using below method you can easily check subscription status..

使用以下方法,您可以轻松检查订阅状态..

if([MKStoreManager isProductPurchased:productIdentifier]) {
 //unlock it
}

It gets the apple id from device and I think that is user specific

它从设备获取苹果ID,我认为这是用户特定的

#1


19  

I know everyone was very concerned about me and how I was doing on this - fear not, solved my problem. Main problem was that I tried the garbage and outdated code in this link, but it wasn't working so I gave up on it. Then I came back to it and implemented it with Alamofire and it works great. Here's the code solution:

我知道每个人都非常关心我以及我在这方面做了什么 - 不要害怕,解决了我的问题。主要问题是我在这个链接中尝试了垃圾和过时的代码,但它没有工作,所以我放弃了它。然后我回到它并用Alamofire实现它并且效果很好。这是代码解决方案:

Swift 3:

斯威夫特3:

let receiptURL = Bundle.main.appStoreReceiptURL
let receipt = NSData(contentsOf: receiptURL!)
let requestContents: [String: Any] = [
    "receipt-data": receipt!.base64EncodedString(options: []),
    "password": "your iTunes Connect shared secret"
]

let appleServer = receiptURL?.lastPathComponent == "sandboxReceipt" ? "sandbox" : "buy"

let stringURL = "https://\(appleServer).itunes.apple.com/verifyReceipt"

print("Loading user receipt: \(stringURL)...")

Alamofire.request(stringURL, method: .post, parameters: requestContents, encoding: JSONEncoding.default)
    .responseJSON { response in
        if let value = response.result.value as? NSDictionary {
            print(value)
        } else {
            print("Receiving receipt from App Store failed: \(response.result)")
        }
}

#2


1  

What are you trying to achieve in particular? Do you want to check for a specific Apple ID?

你想要实现什么目标?您想检查特定的Apple ID吗?

I highly doubt that this is possible through the SDK. Referring to Is it possible to get the user's apple ID through the SDK? you can see that you can't even ask for the ID directly but rather services attached to it.

我非常怀疑这可以通过SDK实现。参考是否可以通过SDK获取用户的苹果ID?你可以看到你甚至不能直接询问身份证,而是附加服务。

What would work is caching all transactions on your own server and search its database locally but that would require the app to ask for the user's Apple ID so the app could update the subscription state whenever it launches as it can check for IAP of the ID associated with the device.

什么工作是缓存您自己的服务器上的所有事务并在本地搜索其数据库,但这将要求应用程序询问用户的Apple ID,以便应用程序可以在启动时更新订阅状态,因为它可以检查相关ID的IAP与设备。

However, the user could just type whatever he wanted - and it's unlikely to get this through Apple's app review process.

但是,用户可以只输入他想要的任何内容 - 而且不太可能通过Apple的应用审核流程获得此信息。

#3


1  

I am using MKSoreKit https://github.com/MugunthKumar/MKStoreKit for auto-renew subscriptions.but it is in objective c you can check the library code for solution.I am using it in my code and it is working fine.

我正在使用MKSoreKit https://github.com/MugunthKumar/MKStoreKit进行自动续订订阅。但是在目标c中你可以检查库代码的解决方案。我在我的代码中使用它并且它工作正常。

using below method you can easily check subscription status..

使用以下方法,您可以轻松检查订阅状态..

if([MKStoreManager isProductPurchased:productIdentifier]) {
 //unlock it
}

It gets the apple id from device and I think that is user specific

它从设备获取苹果ID,我认为这是用户特定的