AppleのStoreKitで作業する方法を学ぶためのサンプルプロジェクトがありますので、自動更新サービスを自分のアプリとアプリ内サービスに一般的に適用する方法を学ぶことができます。StoreKit:Swift 1.2からSwift 3への変換でエラーが発生しました
サンプルプロジェクトがSwift 1.2に入っていて、それをSwift 3に変換するといくつかのエラーが発生しましたが、2つの警告と2つのエラーが発生しました。うまくいけば誰かが私を助けることができます。
また、コードをSwift 3に変換すると、コードはまだ動作しますか?それは古いですから?アプリの購入が大幅に変化しましたか?
警告とエラーストアキットのコードの
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
var products = response.products
if (products.count != 0) {
for i in 0 ..< products.count
{
self.product = products[i] as? SKProduct
self.productsArray.append(product!)
}
self.tableView.reloadData()
} else {
print("No products found")
}
products = response.invalidProductIdentifiers
for product in products
{
print("Product not found: \(product)")
}
}
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
print("Transactions Restored")
var purchasedItemIDS = []
for transaction:SKPaymentTransaction in queue.transactions {
if transaction.payment.productIdentifier == "com.brianjcoleman.testiap1"
{
print("Consumable Product Purchased")
// Unlock Feature
}
else if transaction.payment.productIdentifier == "com.brianjcoleman.testiap2"
{
print("Non-Consumable Product Purchased")
// Unlock Feature
}
else if transaction.payment.productIdentifier == "com.brianjcoleman.testiap3"
{
print("Auto-Renewable Subscription Product Purchased")
// Unlock Feature
}
else if transaction.payment.productIdentifier == "com.brianjcoleman.testiap4"
{
print("Free Subscription Product Purchased")
// Unlock Feature
}
else if transaction.payment.productIdentifier == "com.brianjcoleman.testiap5"
{
print("Non-Renewing Subscription Product Purchased")
// Unlock Feature
}
}
let alert = UIAlertView(title: "Thank You", message: "Your purchase(s) were restored.", delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
休憩
var tableView = UITableView()
let productIdentifiers = Set(["com.brianjcoleman.testiap1", "com.brianjcoleman.testiap2", "com.brianjcoleman.testiap3", "com.brianjcoleman.testiap4", "com.brianjcoleman.testiap5"])
var product: SKProduct?
var productsArray = Array<SKProduct>()
func requestProductData()
{
if SKPaymentQueue.canMakePayments() {
let request = SKProductsRequest(productIdentifiers:
self.productIdentifiers as Set<String>)
request.delegate = self
request.start()
} else {
let alert = UIAlertController(title: "In-App Purchases Not Enabled", message: "Please enable In App Purchase in Settings", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Settings", style: UIAlertActionStyle.default, handler: { alertAction in
alert.dismiss(animated: true, completion: nil)
let url: URL? = URL(string: UIApplicationOpenSettingsURLString)
if url != nil
{
UIApplication.shared.openURL(url!)
}
}))
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: { alertAction in
alert.dismiss(animated: true, completion: nil)
}))
self.present(alert, animated: true, completion: nil)
}
}
func buyProduct(_ sender: UIButton) {
let payment = SKPayment(product: productsArray[sender.tag])
SKPaymentQueue.default().add(payment)
}
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case SKPaymentTransactionState.purchased:
print("Transaction Approved")
print("Product Identifier: \(transaction.payment.productIdentifier)")
self.deliverProduct(transaction)
SKPaymentQueue.default().finishTransaction(transaction)
case SKPaymentTransactionState.failed:
print("Transaction Failed")
SKPaymentQueue.default().finishTransaction(transaction)
default:
break
}
}
}
func deliverProduct(_ transaction:SKPaymentTransaction) {
if transaction.payment.productIdentifier == "com.brianjcoleman.testiap1"
{
print("Consumable Product Purchased")
// Unlock Feature
}
else if transaction.payment.productIdentifier == "com.brianjcoleman.testiap2"
{
print("Non-Consumable Product Purchased")
// Unlock Feature
}
else if transaction.payment.productIdentifier == "com.brianjcoleman.testiap3"
{
print("Auto-Renewable Subscription Product Purchased")
// Unlock Feature
}
else if transaction.payment.productIdentifier == "com.brianjcoleman.testiap4"
{
print("Free Subscription Product Purchased")
// Unlock Feature
}
else if transaction.payment.productIdentifier == "com.brianjcoleman.testiap5"
{
print("Non-Renewing Subscription Product Purchased")
// Unlock Feature
}
}
func restorePurchases(_ sender: UIButton) {
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().restoreCompletedTransactions()
}
とコードテーブルビュー
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cellFrame = CGRect(x: 0, y: 0, width: self.tableView.frame.width, height: 52.0)
let retCell = UITableViewCell(frame: cellFrame)
if self.productsArray.count != 0
{
if indexPath.row == 5
{
let restoreButton = UIButton(frame: CGRect(x: 10.0, y: 10.0, width: UIScreen.main.bounds.width - 20.0, height: 44.0))
restoreButton.titleLabel!.font = UIFont (name: "HelveticaNeue-Bold", size: 20)
restoreButton.addTarget(self, action: #selector(ViewController.restorePurchases(_:)), for: UIControlEvents.touchUpInside)
restoreButton.backgroundColor = UIColor.black
restoreButton.setTitle("Restore Purchases", for: UIControlState())
retCell.addSubview(restoreButton)
}
else
{
let singleProduct = productsArray[indexPath.row]
let titleLabel = UILabel(frame: CGRect(x: 10.0, y: 0.0, width: UIScreen.main.bounds.width - 20.0, height: 25.0))
titleLabel.textColor = UIColor.black
titleLabel.text = singleProduct.localizedTitle
titleLabel.font = UIFont (name: "HelveticaNeue", size: 20)
retCell.addSubview(titleLabel)
let descriptionLabel = UILabel(frame: CGRect(x: 10.0, y: 10.0, width: UIScreen.main.bounds.width - 70.0, height: 40.0))
descriptionLabel.textColor = UIColor.black
descriptionLabel.text = singleProduct.localizedDescription
descriptionLabel.font = UIFont (name: "HelveticaNeue", size: 12)
retCell.addSubview(descriptionLabel)
let buyButton = UIButton(frame: CGRect(x: UIScreen.main.bounds.width - 60.0, y: 5.0, width: 50.0, height: 20.0))
buyButton.titleLabel!.font = UIFont (name: "HelveticaNeue", size: 12)
buyButton.tag = indexPath.row
buyButton.addTarget(self, action: #selector(ViewController.buyProduct(_:)), for: UIControlEvents.touchUpInside)
buyButton.backgroundColor = UIColor.black
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
numberFormatter.locale = Locale.current
buyButton.setTitle(numberFormatter.string(from: singleProduct.price), for: UIControlState())
retCell.addSubview(buyButton)
}
}
return retCell
}
ありがとうございました!残りのコードは最新のものですか? – BroSimple
私は慎重に見ていませんでした。あなたが投稿したエラーに対処しました。私はあなたが[ 'UIAlertController'](https://developer.apple.com/documentation/uikit/uialertcontroller)に' UIAlertView'を更新する必要があります気付かなかった、といくつかの文体のものがあります。これはGithubやAppleか、誰かから得たzipファイルですか? – Kevin
チュートリアルブログ – BroSimple