一个用于进行捐赠的视图控制器。
将 https://github.com/GroupeMINASTE/DonateViewController.git
添加到你的 Swift Package 配置中 (或者使用 Xcode 菜单:File
> Swift Packages
> Add Package Dependency
)
⚠️ 你需要在 App Store Connect 中注册你的应用内购买项目才能使其工作。购买项目的名称和价格将被使用。
首先,在你想要打开 DonateViewController
的视图控制器中导入该包。
// Import the package
import DonateViewController
创建一个方法来打开它
// Import the package
import DonateViewController
class MyViewController: UIViewController {
// Some code...
func openDonateViewController() {
// Create the view controller
let controller = DonateViewController()
// Customize view title, header and footer (optional)
controller.title = "Donate"
controller.header = "Select an amount to donate:"
controller.footer = "The donations help us to improve our projects (...)"
// Add a delegate to get notified when a donation ends
controller.delegate = self
// Add donations
controller.add(identifier: "my.app.identifier.mydonation1")
controller.add(identifier: "my.app.identifier.mydonation2")
// ... (add as many donations as you want)
// And open your view controller: (two ways)
// 1. In your navigation controller
navigationController?.pushViewController(controller, animated: true)
// 2. In a modal
present(UINavigationController(rootViewController: controller), animated: true, completion: nil)
}
// Some code...
}
然后你需要实现 DonateViewControllerDelegate
协议,以便在捐赠结束时收到通知
// Import the package
import DonateViewController
class MyViewController: UIViewController, DonateViewControllerDelegate { // Add the protocol to the view controller
// Some code...
// The method we just created (no changes)
func openDonateViewController() {
// ...
}
// Implement delegate methods
func donateViewController(_ controller: DonateViewController, didDonationSucceed donation: Donation) {
// Handle when the donation succeed
}
func donateViewController(_ controller: DonateViewController, didDonationFailed donation: Donation) {
// Handle when the donation failed
}
// Some code...
}
你可以显示一个 UIAlertController
,并包含一条简短的消息示例。
当你想要打开 DonateViewController
时,调用 openDonateViewController()
方法
self.openDonateViewController()
这可以在用户点击 UIButton
时(在 selector 方法中)或在用户选择表视图单元格时调用。
就这样,你就可以接收捐赠了!
在 iOS 13 之前,控制器中没有实现暗黑主题。如果你想实现自定义的暗黑主题,请继承 DonateViewController
和 DonateCell
来更改视图的颜色。(背景,...)
随意捐赠以帮助开发者创作更多优质内容!立即捐赠