Skip to content

Instantly share code, notes, and snippets.

@eunjiChung
Created March 11, 2021 09:49
Show Gist options
  • Save eunjiChung/aa369f5fa51bf3ba8e254b1a89145321 to your computer and use it in GitHub Desktop.
Save eunjiChung/aa369f5fa51bf3ba8e254b1a89145321 to your computer and use it in GitHub Desktop.
extension ViewController {
private func initIAP() {
NotificationCenter.default.addObserver(self, selector: #selector(handleIAPPurchase(_:)), name: .IAPHelperPurchaseNotification, object: nil)
// IAP 불러오기
InAppProducts.store.requestProducts { [weak self] (success, products) in
guard let self = self, success else { return }
// ...
}
}
// 인앱 결제 버튼 눌렀을 때
private func touchIAP() {
if let product = products.first {
InAppProducts.store.buyProduct(product) // 구매하기
}
}
// 결제 후 Notification을 받아 처리
@objc func handleIAPPurchase(_ notification: Notification) {
guard let success = notification.object as? Bool else { return }
if success {
DispatchQueue.main.async {
let vc = UIAlertController(title: "알림", message: "구매 성공.", preferredStyle: .alert)
let ok = UIAlertAction(title: "확인", style: .default) { _ in
if let mainURL = URL(string: homeurl) {
let request = URLRequest(url: mainURL)
self.mainWebview.load(request)
}
}
vc.addAction(ok)
self.present(vc, animated: true, completion: nil)
}
} else {
DispatchQueue.main.async {
let vc = UIAlertController(title: "알림", message: "구매에 실패했습니다.", preferredStyle: .alert)
let ok = UIAlertAction(title: "확인", style: .default, handler: nil)
vc.addAction(ok)
self.present(vc, animated: true, completion: nil)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment