2017-05-09 66 views
0

我有一個共享按鈕(「共享」),按10次後,會掛斷。要解鎖它,您必須在應用程序中購買它。麻煩的是,我無法弄清楚如何在用戶購買按鈕時解鎖按鈕。該按鈕必須保持解鎖狀態。 10次​​按下後不應該被阻擋。如果按下「恢復」按鈕,則必須發生同樣的情況。如果用戶擦除應用程序並重新安裝,該按鈕必須解除鎖定。我請你幫忙。在這裏,我把運行中的應用程序購買和該按鈕的在應用程序內購買後啓用按鈕

感謝大家,併爲初學者的問題

爲了方便,我把2個文件在一個共享的Dropbox的鏈接道歉代碼,向所有人 Dropbox (file swift)

代碼按鈕 「共享」

var gif : GifModel? = nil 


var imgView : UIImageView? = nil 


override func viewDidLoad() { 
    super.viewDidLoad() 
    imgView = UIImageView(image: UIImage.gifImageWithURL(gif!.url!)) 
    imgView!.frame = CGRect(x: 0, y: 0, width: gif!.width, height: gif!.height) 
    self.view.addSubview(imgView!) 
    // Do any additional setup after loading the view. 

} 

override func viewDidLayoutSubviews() { 
    imgView!.center = CGPoint(x: self.view.frame.size.width/2, y: self.view.frame.size.height/2) 
} 
override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

    // Dispose of any resources that can be recreated. 
} 


func createAlert (title: String, message: String) { 

    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) 
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: { 
     (action) in alert.dismiss(animated: true, completion: nil)})) 
    self.present(alert, animated: true, completion: nil) 


} 

@IBAction func share(sender: UIBarButtonItem) { 


    var counter: Int = UserDefaults.standard.integer(forKey: "yourUniqueKey") 
    if counter < 10 { 
     self.shareGifs(url: URL(string: gif!.url!)!) 
     counter += 1 
     UserDefaults.standard.set(counter, forKey: "yourUniqueKey") 
    } else { 
     sender.isEnabled = false 

     // alert qui!!! l'utente ha esaurito i 10 tap! 
     createAlert(title: "OPS....", message: "In order to continue to share all the gifs you want and forever, you must purchase the 'Unlimited Gifs' pack") 
    } 
} 

CODE應用內購買

extension IAPHelper { 

public func requestProducts(completionHandler: @escaping ProductsRequestCompletionHandler) { 
    productsRequest?.cancel() 
    productsRequestCompletionHandler = completionHandler 

    productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers) 
    productsRequest!.delegate = self 
    productsRequest!.start() 
} 

public func buyProduct(_ product: SKProduct) { 
    print("Buying \(product.productIdentifier)...") 
    let payment = SKPayment(product: product) 
    SKPaymentQueue.default().add(payment) 
} 

public func isProductPurchased(_ productIdentifier: ProductIdentifier) -> Bool { 
    return purchasedProductIdentifiers.contains(productIdentifier) 
} 

public class func canMakePayments() -> Bool { 
    return SKPaymentQueue.canMakePayments() 
} 

public func restorePurchases() { 
    SKPaymentQueue.default().restoreCompletedTransactions() 
} 

}

extension IAPHelper: SKProductsRequestDelegate { 

public func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) { 
    let products = response.products 
    print("Loaded list of products...") 
    productsRequestCompletionHandler?(true, products) 
    clearRequestAndHandler() 

    for p in products { 
     print("Found product: \(p.productIdentifier) \(p.localizedTitle) \(p.price.floatValue)") 
    } 
} 

public func request(_ request: SKRequest, didFailWithError error: Error) { 
    print("Failed to load list of products.") 
    print("Error: \(error.localizedDescription)") 
    productsRequestCompletionHandler?(false, nil) 
    clearRequestAndHandler() 
} 

private func clearRequestAndHandler() { 
    productsRequest = nil 
    productsRequestCompletionHandler = nil 
} 

}

extension IAPHelper: SKPaymentTransactionObserver { 

public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { 
    for transaction in transactions { 
     switch (transaction.transactionState) { 
     case .purchased: 
      complete(transaction: transaction) 
      break 
     case .failed: 
      fail(transaction: transaction) 
      break 
     case .restored: 
      restore(transaction: transaction) 
      break 
     case .deferred: 
      break 
     case .purchasing: 
      break 
     } 
    } 
} 

private func complete(transaction: SKPaymentTransaction) { 
    print("complete...") 
    deliverPurchaseNotificationFor(identifier: transaction.payment.productIdentifier) 
    SKPaymentQueue.default().finishTransaction(transaction) 
} 

private func restore(transaction: SKPaymentTransaction) { 
    guard let productIdentifier = transaction.original?.payment.productIdentifier else { return } 

    print("restore... \(productIdentifier)") 
    deliverPurchaseNotificationFor(identifier: productIdentifier) 
    SKPaymentQueue.default().finishTransaction(transaction) 
} 

private func fail(transaction: SKPaymentTransaction) { 
    print("fail...") 
    if let transactionError = transaction.error as NSError? { 
     if transactionError.code != SKError.paymentCancelled.rawValue { 
      print("Transaction Error: \(String(describing: transaction.error?.localizedDescription))") 
     } 
    } 

    SKPaymentQueue.default().finishTransaction(transaction) 
} 

private func deliverPurchaseNotificationFor(identifier: String?) { 
    guard let identifier = identifier else { return } 

    purchasedProductIdentifiers.insert(identifier) 
    UserDefaults.standard.set(true, forKey: identifier) 
    UserDefaults.standard.synchronize() 
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: IAPHelper.IAPHelperPurchaseNotification), object: identifier) 
} 

}

+0

請把相關的代碼放在你的問題中 – Paulw11

+0

我沒有問題,但它很長。出於這個原因,我更願意簡化放置保管箱。無論如何,我現在把它做 –

+0

完成;)在swift中添加代碼 –

回答

0

最好的辦法是存儲一個值NSUserDefaults的進貨時發生,那麼你的看法是加載,做一個簡單的檢查價值,看看它是否已經購買,然後限制或不限制,可能發生的印刷次數。

將其設置你的完成代碼

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"IAPPurcashed" 
[[NSNotificationCenter defaultCenter] postNotificationName:@"IAP" object:self]; 

內。然後在您的視圖

([[NSUserDefaults standardUserDefaults] bookForKey:@"IAPPurcashed"] == YES 
{ 
    *unlock IAP for multiple presses* 
} 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ReloadIAPButton:) name:@"IAP" object:nil];`` 

還添加觀察員通知中心重新加載可能需要的任何按鈕。

+0

請... :( –