Hi
I want to add to my app the ability to purchase something. My app is similar to Uber. What I mean is that I have a collection view, and I want to be able to associate a price for the type of ride ex. basic or luxury, and the duration of the ride. My app is set up where you choose in collection view what type of ride you want. Then there are 3 sets of buttons where you choose the length of the ride. What I wanted to know is how would I make it so that my app take the type of ride, and the duration and adds the two of them together on another screen, as well as displays what choices you made on the previous screen, and what would the code look like for that. The only thing that I have for the follow-up screen where I want my app to show the price and ask the user to pay is I made a view controller, and I connected it to the previous screen with a custom segue which has no code in it yet.
So your question is not about IAP but about transferring data from one view to another.
What I wanted to know is how would I make it so that my app take the type of ride, and the duration and adds the two of them together on another screen
Let's call the view wwhere you define the ride the rideView and the view where you tell the price the price View
Proceed like this:
you have probably defined enum
enum TypeOfRide {
case basic
case luxury
func description() -> String {
switch self {
case .basic : return "basic"
case .luxury : return "luxury"
}
}
enum LengthOfRide {
case short
case medium
case long
func description() -> String {
switch self {
case .short : return "short"
case .medium : return "medium"
case .long : return "long"
}
}In RideView:
- you have a var to store the type of ride
var rideType : TypeOfRideThis var is set when you select the type either to .basic or .luxury in the collectionView
- you have a var to store the length of ride
var rideLength : LengthOfRideThis is set when you click a button, in the IBAction
In PriceView,
you define a var to store the price, the lenght and the type of ride
var price : Float?
var category : TypeOfRide?
var length: LengthOfRide?and an IBOutlet where you will display the price
@IBOutlet weak var priceLabel: UILabel!
@IBOutlet weak var typeLabel: UILabel!
@IBOutlet weak var lengthLabel: UILabel!In RideView:
You should have a butto like Order
Control-drag from the button to the PriceViewController to define a segue
Give it an identifier as SegueToPrice
In prerare for segue
- you compute the price, according to your computation formula
- you pass the price to the destination VC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var ridePrice : Float = 0.0
switch rideType {
case .basic :
switch rideLength {
case .short : ridePrice = 5.0
case .medium : ridePrice = 10.0
case .long : ridePrice = 15.0
}
case .luxury :
switch rideLength {
case .short : ridePrice = 12.5
case .medium : ridePrice = 25.0
case .long : ridePrice = 40.0
}
}
if segue.identifier == "SegueToPrice" { // The identifier you have given to the segue
if let destVC = segue.destination as? PriceView {
destVC.price = ridePrice
destVC.category = rideType
destVC.length = rideLength
}
}
}Finally, in PriceView
in viewDidLoad
priceLabel.text = price
typeLabel.text = category.description
lengthLabel.text = length.description