我使用自动续订订阅为该应用程序创建了应用程序内购买功能。问题是我不确定如何检查购买状态,订阅是否已用完或仍然有效。
这是我尝试保存购买状态的地方:
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("add payment")
for transaction: AnyObject in transactions {
let trans = transaction as! SKPaymentTransaction
print(trans.error)
switch trans.transactionState {
case .purchased:
print("buy ok, unlock IAP HERE")
print(p.productIdentifier)
let prodID = p.productIdentifier
switch prodID {
case "com.test.UnlockTools.Subscription1":
print("tool set 1 unlocked")
uTool1()
print("tool set 2 unlocked")
uTool2()
print("tool set 3 unlocked")
uTool3()
UserDefaults.standard.set(true, forKey: "isSubbed")
default:
print("IAP not found")
}
queue.finishTransaction(trans)
case .failed:
print("buy error")
queue.finishTransaction(trans)
break
default:
print("Default")
break
}
} …Run Code Online (Sandbox Code Playgroud) 我使用导航控制器从一个视图推送到另一个视图,但没有弹出任何我推送的视图。因此,在加载视图时,我想从堆栈中弹出上一个视图。
推送视图的代码:
var identities = [String]()
identities = ["A", "B", "C", "D", "E"]
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
用于弹出视图的测试代码(不是我想要的):
self.navigationController?.viewControllers.remove(at: 0)
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。
解决了:
@DonMag帮助我逐步了解了该行代码的正确使用和放置。
memory-leaks uinavigationcontroller viewcontroller ios swift
我正在尝试做的是将所有以前的视图控制器从堆栈弹出到“菜单”控制器。我有一段代码应该执行此操作,但是单击“菜单”时,应用程序崩溃。
崩溃的原因:“试图弹出到不存在的视图控制器。”
这是我的代码的一部分:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// 0 = menu
if indexPath.row == 0 {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
let _ = navigationController?.popToViewController(viewController!, animated: true)
} else {
let vcName = identities[indexPath.row]
let viewController = storyboard?.instantiateViewController(withIdentifier: vcName)
self.navigationController?.pushViewController(viewController!, animated: true)
}
}
Run Code Online (Sandbox Code Playgroud)