Swift Stripe Access DidCreateToken - swift

I'm trying to integrate Stripe into my ios (Swift) app by using Firebase-Cloud-Functions. Now I'm want to get the token to a created card to save it into my Firestore-Database.
I've followed this tutorial on how to implement it. It's working when I'm displaying my addCardViewController on my own since I have the method didCreateToken. But now that I'm just showing it programmatically like shown in their provided example (row 158 is where they're showing the view controller) I don't know how to implement this method and get the token of the card if a user creates/adds a new one.
Thats what Id normally do:
func addCardViewController(_ addCardViewController: STPAddCardViewController, didCreateToken token: STPToken, completion: #escaping STPErrorBlock)
{
STRIPE_CUSTOMERS_REF.document(userId).collection("tokens").addDocument(data: ["token": tokenId]) // Calls Firebase-Cloud-Function and adds payment method to Stripe
navigationController?.popViewController(animated: true)
}
But like I said Im not able to implement this method.
I want to get the token when a user adds a new card.
Id really appreciates any kind of help. If you need any additional information let me know.
-Marie

It looks like you using STPPaymentContext (Standard Integration) to present the STPPaymentMethodsViewController (line 158 in the example above).
STPPaymentContext actually implements its own instances of STPPaymentMethodsViewController and STPAddCardViewController. Therefore, STPPaymentContext handles the delegate methods for those 2 view controllers and those aren't exposed to the user when using the Standard Integration. Which explains why that delegate method is not triggering for you.
Instead, your view controller should become the delegate for STPPaymentContext and implement the all the required delegate methods [0], including paymentContextDidChange method.
paymentContextDidChange method is triggered whenever a user adds a new card or selects a new payment method [1].
When a user enters new card details, you should be able to get the token ID with the following:
func paymentContextDidChange(_ paymentContext: STPPaymentContext)
{
if let card = paymentContext.selectedPaymentMethod as? STPCard {
let token = card.stripeID
// store token as required
}
}
Hope that helps!
[0] https://stripe.github.io/stripe-ios/docs/Protocols/STPPaymentContextDelegate.html
[1] https://stripe.github.io/stripe-ios/docs/Protocols/STPPaymentMethod.html

Related

How can I exract information to convert to vcard

I am creating an app using swift in xcode 10 that would allow user to create digital portfolios viewed in UIview, this UI view will contain their names , portfolio links and other stuff related to their profession. I have not done anything on it yet, I just want to know how can I extract data from the view to convert to vcard so I can share it
So if get it correctly you will have an application that lets a user fill in his information and create a digital portfolio, after that you want to the user to able to share the information outside of the app. I am not exactly sure what you mean by "extract" but on iOS using swift you can share information like this.
Inside your viewController:
override func viewDidLoad() {
super.viewDidLoad()
let items: [Any] = ["Put whatever you want to share in here", URL(string: "https://www.apple.com")!]
let ac = UIActivityViewController(activityItems: items, applicationActivities: nil)
self.present(ac, animated: true)
}
This will bring up the activity controller.

How to get back to the current window from AppDelegate

In my macOS application, I am following a OAuth-Login procedure.
I am authenticating successfully by receiving a code within a url through my custom url, with which I already can get hold of an access_token.
I start the login procedure with the simple click of a button in a ViewController.
The system I log in to then answers with my registered custom url scheme, providing me with a code I have to then use to get an access_token with POST request. This access_token than can be used to make api calls.
My Problem now is that lacking knowledge of how to implement other techniques, I am currently doing all the latter procedure within the AppDelegate function application(_ application: NSApplication, open urls: [URL]).
From my limited understanding this is the way to handle this, but now
how can I get back from there to get hold of the current view? I am really struggling with the view controller life cycle part of things here I guess...
In AppDelegate, you can get a reference to the current window's ViewController as follows. Replace "MainViewController" with the name of the one you use.
iOS Swift:
if let vc = window?.rootViewController as? MainViewController {
// use `vc` here by calling public functions
// and accessing public properties
}
macOS Swift:
if let vc = NSApplication.shared.mainWindow?.contentViewController as? MainViewController {
// use `vc` here by calling public functions
// and accessing public properties
}
OK, found it: since there is no rootViewController in macOS-land as there is with iOS, it works a little different:
in macOS you can get hold of the window currently "under keyboard" like so:
in application(_:open:), where the redirect_uri gets called:
if let viewController = NSApplication.shared.keyWindow?.contentViewController as? ViewController {
// make changes to the view
}

How do I offer a "sent action" from a custom class?

I'm developing a custom class in Swift based on NSObject. It's a statusMenu icon/menu helper. When I receive an event for the icon being clicked in my custom class, I want to pass this on in the same way an NSButton allows to create an IBAction to respond to the user clicking the button.
How do I do this?
Code:
I'm registering a selector in my class to listen to clicks:
statusItem.action = #selector(statusBarIconClicked)
The selector receiving this:
#objc func statusBarIconClicked(sender: AnyObject) {
print("clicked clicked!!")
// pass sent action on through a new sent action... how?
}
I want this to be linkable to the user in the same way a button can lead to this:
#IBAction func myClassSaysMenuWasClicked(_ sender: Any) {
// Reacting to that
}
Googled for a good while and found: nothing.
I take it that you're asking about this sort of thing, displayed in the Connections inspector (this is iOS, not macOS, but it's the same idea):
The question would then be: when the user selects an instance of my class in the nib editor in Xcode, I'd like those Sent Events to appear in the Connections inspector so that the user can hook up one of them and use the target-action architecture.
You can do this only if your class is a Control subclass. Thus, for example, in iOS, a custom UIControl subclass displays those sent events in Interface Builder.
If you can't do that, then the programmer won't be able to configure your target-action in Interface Builder. You can certainly implement a target–action architecture, but the programmer will have to set the target and action in code. (You could do half a job of it by making the target an outlet, of course.)
I worked around the comment above and googled further. I found the solution being to change from NSObject to NSController in this line:
class StatusMenuController: NSControl, NSMenuDelegate {
And run this command when I want to trigger the sent action:
if let theAction = self.action { NSApp.sendAction(theAction, to: self.target, from: self) }
The if-command of course checking so that an action is actually set before trying to use it.
I found no ways during my research to add any further sent actions. The way to go here seems to be delegates.

How do I use Firebase Authentication using multiple view controllers?

I am in the process of adding the Create new user Method for Firebase. I have already completed the SignUp Auth process. However when users are creating a new account. How do I go about this if I have multiple ViewControllers?
For example,
The First SignUpViewController - User is asked to enter their email address.
When the user clicks next. This is then followed by another ViewController.
SecondViewControlle - User is asked to enter their name and create a password.
After this, The user is greeted with a Welcome page with their name they submitted. (Welcome to “App Name” Name). Which will then proceed to the home UI after next is clicked.
How do I implement The Auth.auth().CreateUserwith func when I have more than one ViewController? Is there a way to save the email and password using two different ViewControllers?
Update**
I have added the following code that was suggested below, however the App crashes every time I click next on the enter your " Email" View Controller. I am getting this warning. Does anyone know what I did wrong? The rest of the code seems to be working fine. Here is a screenshot.
You can save all information in a Dictionary and use it at the time of SignUp. In your AppDelegate class:
/// Instance of AppDelegate
static let shared: AppDelegate = UIApplication.shared.delegate as! AppDelegate
/// Dictionary for user information
var userInformation: Dictionary<String, Any> = [:]
On Email Screen - Next Button Click
/// #1. Validate the email address
/// #2. Save it in Dictionary
AppDelegate.shared.userInformation["email"] = "xyz#gmail.com" or tFEmail.text.lowercased()
Then On name and password screen
AppDelegate.shared.userInformation["name"] = "xyz" or tFName.text
AppDelegate.shared.userInformation["password"] = "xxxxxxx" or tFPassword.text
Now on last screen - Sign Up Button clicked you can use all this information
let email = AppDelegate.shared.userInformation["email"]
let name = AppDelegate.shared.userInformation["name"]
let password = AppDelegate.shared.userInformation["password"]
Implement the Firebase SignUp method and after successfully logged in show the welcome screen.

xcode / swift receiving user data through a form

I've been designing an app where the user needs to be able to send me the value of certain variables within the app. I originally planned to use a simple mail setup where they fill out a form and the game data is automatically filled out in the email.
Is there any way a user can click a button, and send me the values of variables within the app without the user specifically seeing or being able to modify the variables?
The user consents and realizes what they are doing to. I've been looking at firebase and seeing if there's anything there which can help me. Any ideas would be greatly appreciated.
Is there any way a user can click a button, and send me the values of
variables within the app without the user specifically seeing or being
able to modify the variables?
Yes. Add a UIButton to your storyboard and create a IBAction for it. Whenever the button gets tapped the IBAction gets called. In this case the IBAction is named didTapbutton. See the code below:
import UIKit
class ViewController: UIViewController {
var secretName = "Bond"
var secretNumber = 007
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func didTapButton(_ sender: Any) {
mail(to: secretName, secretNumber: secretNumber)
}
func mail(to secretName: String, secretNumber: Int) {
print("hello \(secretName + secretNumber)")
}
}
didTapButton uses the local variables secretName and secretNumber "within the app" without the user seeing the values.