I have checked all the tutorial and also did a lot of R & D on gateway integration.
But didn't find a way to integrate paytm payment gateway.
func paymentConfiguration()
{
var orderDict = [AnyHashable: Any]()
orderDict["MID"] = "WorldP64425807474247"
orderDict["CHANNEL_ID"] = "WAP"
orderDict["INDUSTRY_TYPE_ID"] = "Retail"
orderDict["WEBSITE"] = "worldpressplg"
orderDict["TXN_AMOUNT"] = "1"
orderDict["ORDER_ID"] = ViewController.generateOrderID(withPrefix: "")
orderDict["CALLBACK_URL"] = "https://securegw.paytm.in/theia/paytmCallback?ORDER_ID=<ORDER_ID>"
orderDict["CHECKSUMHASH"] = "w2QDRMgp1/BNdEnJEAPCIOmNgQvsi+BhpqijfM9KvFfRiPmGSt3Ddzw+oTaGCLneJwxFFq5mqTMwJXdQE2EzK4px2xruDqKZjHupz9yXev4="
orderDict["REQUEST_TYPE"] = "DEFAULT"
orderDict["CUST_ID"] = "1234567890"
var order = PGOrder(params: orderDict)
}
func openPaytmController()
{
PGServerEnvironment.selectServerDialog(view, completionHandler: {(_ type: ServerType) -> Void in
var txnController = PGTransactionViewController.initTransaction(forOrder: order)
if type != eServerTypeNone {
txnController.serverType = type
txnController.merchant = mc
txnController.delegate = self
self.show(txnController)
}
})
}
Any help would be greatly appreciated.
Thanks in advance
**
PayTM Integaration with swift with detail
**
##Download paytm sdk##
https://github.com/Paytm-Payments/Paytm_iOS_App_Kit
Make sure the dynamic lib and systemConfiguration.framwork is added in “Linked binaries and frameworks”
Add bridging header file into the project
#import "PaymentsSDK.h"
You have to generate CheckSumHash key - PayTm unique key for transaction purpose
Pass the below parameters to generate checkSumHash
let params:[String: Any] = [
"CUST_ID”:<Your Customer ID>, // you have to generate unique customer ID (Generate random string - less than 50 length count )
"TXN_AMOUNT":"10.00", // sample amount
“MID": <Your merchant ID>,
"ORDER_ID”:<Your Order ID>, // you have to generate unique order ID (Generate random string - less than 50 length count )
"INDUSTRY_TYPE_ID":"Retail", //Staging Environment
"CHANNEL_ID":"WAP", //Staging Environment
"WEBSITE":"APPSTAGING", //Staging Environment - Mobile
"CALLBACK_URL":"https://securegw-stage.paytm.in/theia/paytmCallback?ORDER_ID=\(<Your Order ID>)” // This should be important one and make sure the correct order ID.
]
In your server , You have to setUp the the file that should generate CheckSumHash key based on the given parameters and merchant key (Which is stored in the file; Don’t use inside your app ). That should be your CHECKSUM URL along with the above mentioned parameter. And finally, we get the CheckSumHash in the response
You have to given the above mentioned parameters along with checkSumHash (which you got the response - refer :step 3) to imitate PGTransactionViewCOntroller
let params:[String: Any] = [
"CUST_ID”:<Your Customer ID>, // you have to generate unique customer ID (Generate random string - less than 50 length count )
"TXN_AMOUNT":"10.00", // sample amount
“MID": <Your merchant ID>,
"ORDER_ID”:<Your Order ID>, // you have to generate unique order ID (Generate random string - less than 50 length count )
"INDUSTRY_TYPE_ID":"Retail", //Staging Environment
"CHANNEL_ID":"WAP", //Staging Environment
"WEBSITE":"APPSTAGING", //Staging Environment - Mobile
"CALLBACK_URL":"https://securegw-stage.paytm.in/theia/paytmCallback?ORDER_ID=\(<Your Order ID>)” // This should be important one and make sure the correct order ID. ,“CHECKSUMHASH”:<your geenrated CheckSumHash key> // which you got the response of generate CheckSumHash
]
let order = PGOrder(params: params)
let txnController = PGTransactionViewController(transactionFor: order)
txnController?.serverType = eServerTypeStaging
txnController?.merchant = PGMerchantConfiguration.default()
txnController?.merchant.checksumGenerationURL = CheckSumGenerationURL
txnController?.merchant.merchantID = "FlotaS90100524961231"
txnController?.merchant.checksumValidationURL = CheckSumVerifyURL + orderID
txnController?.loggingEnabled = true
txnController?.merchant.website = "APPSTAGING"
txnController?.merchant.industryID = "Retail"
txnController?.serverType = eServerTypeStaging
txnController?.delegate = self
self.navigationController?.pushViewController(txnController!, animated: true)
PayTM delegates to handle response
func didSucceedTransaction(controller: PGTransactionViewController, response: [NSObject : AnyObject]) {
print(response)
}
func didFinishedResponse(_ controller: PGTransactionViewController!, response responseString: String!) {
print(responseString) // Response will be in string
let data = responseString.data(using: .utf8)!
let obj = JSON(data: data)
if obj["STATUS"].stringValue != "TXN_SUCCESS" {
//handle what you want
}
}
}
func didFailTransaction(_ controller: PGTransactionViewController!, error: Error!, response: [AnyHashable : Any]!) {
print(error)
}
func didCancelTrasaction(_ controller: PGTransactionViewController!) {
print("User camcelled the trasaction")
controller.navigationController?.popViewController(animated: true)
}
func errorMisssingParameter(_ controller: PGTransactionViewController!, error: Error!) {
print(error.localizedDescription)
controller.navigationController?.popViewController(animated: true)
}
Try this code:
func showController(controller: PGTransactionViewController) {
if self.navigationController != nil {
self.navigationController?.pushViewController(controller, animated: true)
} else {
self.present(controller, animated: true, completion: nil)
}
}
func removeController(controller: PGTransactionViewController) {
if self.navigationController != nil {
self.navigationController?.popViewController(animated: true)
} else {
controller.dismiss(animated: true, completion: nil)
}
}
//Creat Payment----------------
func creatPayment(CheckSum: String) {
let mc = PGMerchantConfiguration.default()!
var orderDict = [String : Any]()
orderDict["MID"] = "WorldP64425807474247";
orderDict["ORDER_ID"] = ViewController.generateOrderID(withPrefix: "");
orderDict["CUST_ID"] = "1234567890";
orderDict["INDUSTRY_TYPE_ID"] = "Retail";
orderDict["CHANNEL_ID"] = "WAP";
orderDict["TXN_AMOUNT"] = self.FINAL_AMOUNT;
orderDict["WEBSITE"] = "APP_STAGING";
orderDict["CALLBACK_URL"] = "https://pguat.paytm.com/paytmchecksum/paytmCallback.jsp";
orderDict["CHECKSUMHASH"] = CheckSum;
let pgOrder = PGOrder(params: orderDict )
let transaction = PGTransactionViewController.init(transactionFor: pgOrder)
transaction!.serverType = eServerTypeStaging
transaction!.merchant = mc
transaction!.loggingEnabled = true
transaction!.delegate = self
self.showController(controller: transaction!)
}
func didFinishedResponse(_ controller: PGTransactionViewController!, response responseString: String!) {
print(responseString)
}
func didCancelTrasaction(_ controller: PGTransactionViewController!) {
print("CANCELLED")
}
func errorMisssingParameter(_ controller: PGTransactionViewController!, error: Error!) {
self.removeController(controller: controller)
print(error)
}
You have given the checksum as hardcore so, when the paytm decode your checksum and comapare with your parameters, the comparison will get fail. so you need to provide a new checksum when ever you make a payment.
The main thing you need to be care is that, the given parameters for checksum generation and payment setup should be same.
Checksum can be generated in the backend with the help of checksum generation kit provided by paytm
you should call the func creatPayment(CheckSum: String) in success of Generate checksumApi and give the checksum to the argument in the function
In Swift 5.0 Follow the following steps:-
1:- Follow these steps to download and import the library in your project:-
Download SDK from:-
https://github.com/Paytm-Payments/Paytm_iOS_App_Kit
Open your project in XCode and from the File menu, select Add files to
"your project"
Select Paytm.framework in the directory you just unzipped
Make sure 'Copy items if needed' is checked and click 'Add' Under
"Link Binary With Libraries" in the "Build Phases" tab of your
project settings, add SystemConfiguration.framework
Check if PaytmSDK.framework is added in both “Link Binary With
Libraries” and “Embedded Binaries”. If not, add by clicking on the plus icon.
Import PaytmSDK to ViewController
import PaymentSDK
Generate ChecksumHash
Security parameters to avoid tampering. Generated using server-side checksum utility provided by Paytm. Merchant has to ensure that this always gets generated on the server. Utilities to generate checksum hash is available
Add code on your payment ViewController to execute the payment process
var txnController = PGTransactionViewController()
var serv = PGServerEnvironment()
var params = [String:String]()
var order_ID:String?
var cust_ID:String?
func beginPayment() {
serv = serv.createStagingEnvironment()
let type :ServerType = .eServerTypeStaging
let order = PGOrder(orderID: "", customerID: "", amount: "", eMail: "", mobile: "")
order.params = ["MID": "rxazcv89315285244163",
"ORDER_ID": "order1",
"CUST_ID": "cust123",
"MOBILE_NO": "7777777777",
"EMAIL": "username#emailprovider.com",
"CHANNEL_ID": "WAP",
"WEBSITE": "WEBSTAGING",
"TXN_AMOUNT": "100.12",
"INDUSTRY_TYPE_ID": "Retail",
"CHECKSUMHASH": "oCDBVF+hvVb68JvzbKI40TOtcxlNjMdixi9FnRSh80Ub7XfjvgNr9NrfrOCPLmt65UhStCkrDnlYkclz1qE0uBMOrmuKLGlybuErulbLYSQ=",
"CALLBACK_URL": "https://securegw-stage.paytm.in/theia/paytmCallback?ORDER_ID=order1"]
self.txnController = self.txnController.initTransaction(for: order) as! PGTransactionViewController
self.txnController.title = "Paytm Payments"
self.txnController.setLoggingEnabled(true)
if(type != ServerType.eServerTypeNone) {
self.txnController.serverType = type;
} else {
return
}
self.txnController.merchant = PGMerchantConfiguration.defaultConfiguration()
self.txnController.delegate = self
self.navigationController?.pushViewController(self.txnController, animated: true)
}
Change "CHECKSUMHASH": value in params that comes from API
order.params["CHECKSUMHASH"] = checkSum // API checkSum value
confirm the protocol-delegate of ‘PGTransactionDelegate’ to Handle error and success responses
To handle success/errors on completion of payment, implement "didFinishedResponse", "didCancelTrasaction", "errorMisssingParameter" methods of the "PGTransactionDelegate". Code snippet provided below:-
extension PaymentViewController : PGTransactionDelegate {
//this function triggers when transaction gets finished
func didFinishedResponse(_ controller: PGTransactionViewController, response responseString: String) {
let msg : String = responseString
var titlemsg : String = ""
if let data = responseString.data(using: String.Encoding.utf8) {
do {
if let jsonresponse = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:Any] , jsonresponse.count > 0{
titlemsg = jsonresponse["STATUS"] as? String ?? ""
}
} catch {
debugLog("Something went wrong")
}
}
let actionSheetController: UIAlertController = UIAlertController(title: titlemsg , message: msg, preferredStyle: .alert)
let cancelAction : UIAlertAction = UIAlertAction(title: "OK", style: .cancel) {
action -> Void in
controller.navigationController?.popViewController(animated: true)
}
actionSheetController.addAction(cancelAction)
self.present(actionSheetController, animated: true, completion: nil)
}
//this function triggers when transaction gets cancelled
func didCancelTrasaction(_ controller : PGTransactionViewController) {
controller.navigationController?.popViewController(animated: true)
}
//Called when a required parameter is missing.
func errorMisssingParameter(_ controller : PGTransactionViewController, error : NSError?) {
controller.navigationController?.popViewController(animated: true)
}
}
Related
My app totally freeze after successful login to DocuSign
Here my code:
#IBAction private func signDocument(_ sender: UIButton) {
guard let hostURL = URL(string: Environment.current.docuSignHost) else { return }
isLoading = true
DSMManager.login(withEmail: Environment.current.docuSignUserID,
password: Environment.current.docuSignPass,
integratorKey: Environment.current.docuSignIntegratorKey,
host: hostURL) { [weak self] info, error in
self?.isLoading = false
if let error = error {
self?.error = error
} else {
self?.showDocuSign(info: info)
}
}
}
// MARK: - Helpers
private func showDocuSign(info: DSMAccountInfo?) {
guard let info = info else { return }
envelopManager.perform(with: info, presentingController: self)
}
final class EnvelopeManager {
private let envelopesManager = DSMEnvelopesManager()
private let templateManager = DSMTemplatesManager()
// MARK: - Lifecycle
func sync() {
envelopesManager.syncEnvelopes()
}
func perform(with config: DSMAccountInfo, presentingController: UIViewController) {
guard let path = Bundle.main.path(forResource: R.file.tgkCapitalPortfolioBAgreementPdf.name, ofType: "pdf") else { return }
let envelopDefinition = DSMEnvelopeDefinition()
envelopDefinition.envelopeName = "Some name"
envelopDefinition.emailSubject = "Please Sign Envelope on Document"
envelopDefinition.emailBlurb = "Hello, Please sign my Envelope"
let builder = DSMDocumentBuilder()
builder.addDocumentId("1")
builder.addName(R.file.tgkCapitalPortfolioBAgreementPdf.name)
builder.addFilePath(Bundle.main.path(forResource: R.file.tgkCapitalPortfolioBAgreementPdf.name,
ofType: "pdf")!)
let document = builder.build()
envelopDefinition.documents = [document]
let signHere = DSMSignHere()
signHere.documentId = document.documentId
signHere.pageNumber = 1
signHere.recipientId = "1"
signHere.frame = .init(originX: 100,
originY: 100,
width: 100,
height: 100,
originYOffsetApplied: 50)
signHere.tabId = "1"
let tabs = DSMTabs()
tabs.signHereTabs = [signHere]
let signer = DSMSigner()
signer.email = config.email
signer.name = config.userName
signer.userId = config.userId
signer.clientUserId = config.userId
signer.routingOrder = 1
signer.recipientId = "1"
signer.tabs = tabs
let signers: [DSMSigner] = [signer]
let recipients = DSMRecipients()
recipients.signers = signers
envelopDefinition.recipients = recipients
envelopDefinition.status = "created"
envelopesManager.composeEnvelope(with: envelopDefinition, signingMode: .offline) { [weak self] envelopID, error in
if let envelopID = envelopID {
print(envelopID)
self?.presentSigning(presenter: presentingController,
envelopeID: envelopID)
} else {
print(error.localizedDescription)
}
}
}
private func presentSigning(presenter: UIViewController, envelopeID: String) {
envelopesManager.resumeSigningEnvelope(withPresenting: presenter,
envelopeId: envelopeID) { (viewController, error) in
if let viewController = viewController {
print(viewController)
}
if let error = error {
print(error.localizedDescription)
}
}
}
}
All closures have a success status but the app freezes and any DocuSign view controllers are not showing.
But if in the second attempt I add the calling the logout before login - all works as expected
#IBAction private func signDocument(_ sender: UIButton) {
guard let hostURL = URL(string: Environment.current.docuSignHost) else { return }
isLoading = true
DSMManager.logout()
DSMManager.login(withEmail: Environment.current.docuSignUserID,
password: Environment.current.docuSignPass,
integratorKey: Environment.current.docuSignIntegratorKey,
host: hostURL) { [weak self] info, error in
self?.isLoading = false
if let error = error {
self?.error = error
} else {
self?.showDocuSign(info: info)
}
}
}
How to resolve this issue? How to make that my app will not freeze after any successful login?
Edit1
I have figured out that the total app freeze is occurring by the first app install. When I open an app that was already installed and I many times opened it earlier and go to DocuSign flow, call logout and finally call login- all works as expected - I don't have any app freeze. But if I don't call the logout method in this chain then my app freezes.
The problem was consistent in that DSMManager.setup() called earlier than applicationDidLaunch. After I moved it to applicationDidLaunch the problem was resolved
Thanks for posting the solution #igdev.
Yes, correct sequence to perform SDK setup is during applicationDidLaunch or afterwards, say during viewDidLoad() of any of the viewControllers (lazy sdk setup & initialization). As long as setup() is done prior to any of the sdk-calls such as login(withAccessToken:...) or cacheEnvelope(...). DocuSign SDK setup() establishes the core data and other essential components required for its functionality.
Typical sequence of calls made with DocuSign SDK.
Application Launch
Initialize DocuSign SDK with DocuSignSDK setup()
Login with DocuSignSDK login(with...). Login could be skipped if a prior SDK session is already established, for e.g. when getting signatures in offline-envelopes using downloaded templates.
Accessing TemplatesManager or EnvelopesManager functionality [Download templates, Sign Envelopes, Sync offline envelopes, etc] and other tasks such as handling DocuSign-SDK notifications to track events (e.g. Signing Completed Envelope Cached, etc).
Logout, it's not required, if needed can perform DSMManager.logout() when client user is logged out.
Error Domain=MCOErrorDomain Code=5 "Unable to authenticate with the current session's credentials." UserInfo={NSLocalizedDescription=Unable to authenticate with the current session's credentials.}
I put this code in my project.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
prepareImapSession()
}
var imapsession:MCOIMAPSession = MCOIMAPSession()
var error : Error? = nil
func prepareImapSession()
{
// CONFIGURE THAT DEPENDING OF YOUR NEEDS
imapsession.hostname = "imap.gmail.com" // String
imapsession.username = my email // String
imapsession.password = password // String
imapsession.port = 993 // UInt32 number
imapsession.authType = MCOAuthType.saslLogin
imapsession.connectionType = MCOConnectionType.TLS
DispatchQueue.main.asyncAfter(deadline: .now() + 8) { [self] in
self.useImapWithUIDS()
}
imapsession.connectOperation()
}
func useImapWithUIDS() {
// There is more than one option here, explore depending of your needs
// let kind = MCOIMAPMessagesRequestKind()
// let headers = kind.union(MCOIMAPMessagesRequestKind.headers)
// let request = headers.union(MCOIMAPMessagesRequestKind.flags)
let requestKind: MCOIMAPMessagesRequestKind = [.headers, .flags]
let folder : String = "INBOX"
// HERE ALSO EXPLORE DEPENDING OF YOUR NEEDS, RANGE IT IS THE RANGE OF THE UIDS THAT YOU WANT TO FETCH, I SUGGEST TO YOU TO CHANGE THE // NUMBER ONE IF YOU HAVE A LOWER BOUND TO FETCH EMAIL
let uids : MCOIndexSet = MCOIndexSet(range: MCORangeMake(1, UINT64_MAX))
let fetchOperation = imapsession.fetchMessagesOperation(withFolder: folder, requestKind: requestKind, uids: uids)
fetchOperation?.start
{ [self] (err, msg, vanished) -> Void in
if (err != nil)
{
self.error = err
NSLog((err?.localizedDescription)!)
}
else
{
guard let msgs = msg as? [MCOIMAPMessage]
else
{
print("ERROR GETTING THE MAILS")
return
}
for i in 0..<msgs.count
{
// THE SUBJECT
let subject = msgs[i].header.subject
// THE uid for this email. The uid is unique for one email
let uid = msgs[i].uid
self.useImapFetchContent(uidToFetch: uid)
// The sequenceNumber like the nomber say it is the sequence for the emails in the INBOX from the first one // (sequenceNumber = 1) to the last one , it not represent always the same email. Because if you delete one email then //next one will get the sequence number of that email that was deleted
let sequenceNumber = msgs[i].sequenceNumber
}
}
}
}
// MARK: - EXTRACT THE CONTENT OF ONE EMAIL, IN THIS FUNCTION YOU NEED THE uid, THE UNIQUE NUMBER FOR ONE EMAIL
func useImapFetchContent(uidToFetch uid: UInt32) {
let operation: MCOIMAPFetchContentOperation = imapsession.fetchMessageOperation(withFolder: "INBOX", uid: uid)
operation.start { (Error, data) in
if (Error != nil)
{
NSLog("ERROR")
return
}
let messageParser: MCOMessageParser = MCOMessageParser(data: data)
// IF YOU HAVE ATTACHMENTS USE THIS
let attachments = messageParser.attachments() as? [MCOAttachment]
// THEN YOU NEED THIS PROPERTIE, IN THIS EXAMPLE I TAKE THI FIRST, USE WHAT EVER YOU WANT
let attachData = attachments?.first?.data
// FOR THE MESSAGEPARSER YOU CAN EPLORE MORE THAN ONE OPTION TO OBTAIN THE TEXT
let msgPlainBody = messageParser.plainTextBodyRendering()
}
}
}
I using the mailcore2 framework. I got error description Unable to authenticate with the current session's credentials.
It can be related with 2nd factor authentication on your account. It was described on Apple forum.
I've been struggling with this for 2 hours now. I'm building an app in Swift, using Firebase Database and storage.
The goal is to update User profile. The user has 2 images - Profile and header. Now, I have to first check if they've selected an image from the photo library, if not - just get the old URL from the database and submit it back to the database with the rest of the updated information. If it's a new selected image, upload the image to the Storage, get back the URL using downloadURL assign it to the var storageHeaderDownloadedURL and/or var storageProfileDownloadedURL and submit the string values with the rest of the user data to Firebase Database.
The problem is that it obviously assigns the values of an empty String (I've declared them as such) BEFORE I get back the downloaded URL. If the user doesn't update the images but the rest of the UITextFields it all works, the old URL is submitted to the Firebase Database.
My question is how do I execute the downloaded URL methods for from the storage and then assign it to var storageHeaderDownloadedURL and var storageProfileDownloadedURL first hand?
func updateUserProfile ()
{
if let userID = FIRAuth.auth()?.currentUser?.uid
{
// Note: Storage references to profile images & profile headers folder
let storageUserProfileID = Storage.storage.profile_images.child(userID)
let storageUserHeaderID = Storage.storage.profile_headers.child(userID)
guard let imageProfile = profileImage.image else { return }
guard let headerImage = headerImage.image else { return }
if let newProfileImage = UIImagePNGRepresentation(imageProfile), let newHeaderImage = UIImagePNGRepresentation(headerImage)
{
storageUserProfileID.put(newProfileImage, metadata: nil, completion: { (metadata, error) in
if error != nil
{
showAlert(title: "Oops!", msg: (error?.localizedDescription)!, actionButton: "OK", viewController: self)
return
}
// Get the URL from the storage
storageUserProfileID.downloadURL(completion: { (url, error) in
if error != nil
{
showAlert(title: "Oops!!!", msg: (error?.localizedDescription)!, actionButton: "OK", viewController: nil)
return
}
else
{
if let profileImgDownloadedURL = url?.absoluteString
{
self.storageProfileDownloadedURL = profileImgDownloadedURL
print(self.storageProfileDownloadedURL)
self.selectedProfileImage = .True
}
}
})
})
storageUserHeaderID.put(newHeaderImage, metadata: nil, completion: { (metadata, error) in
if error != nil
{
showAlert(title: "Oops!", msg: (error?.localizedDescription)!, actionButton: "OK", viewController: self)
return
}
// Get the URL from the storage
storageUserHeaderID.downloadURL(completion: { (url, error) in
if error != nil
{
showAlert(title: "Oops!!!", msg: (error?.localizedDescription)!, actionButton: "OK", viewController: self)
return
}
else
{
if let headerImgDownloadedURL = url?.absoluteString
{
self.storageHeaderDownloadedURL = headerImgDownloadedURL
print(self.storageHeaderDownloadedURL)
self.selectedHeaderImage = .True
}
}
})
})
//Note: Update the info for that user in Database
print(self.storageHeaderDownloadedURL)
print(self.storageProfileDownloadedURL)
var finalHeaderImageURL = String()
switch self.selectedHeaderImage {
case .True:
finalHeaderImageURL = self.storageHeaderDownloadedURL
break
case .False:
finalHeaderImageURL = self.oldHeaderImageInDB
break
}
print(finalHeaderImageURL)
var finalProfileImageURL = String()
switch self.selectedProfileImage {
case .True:
finalProfileImageURL = self.storageProfileDownloadedURL
break
case .False:
finalProfileImageURL = self.oldProfilePhotoImageInDB
break
}
print(finalProfileImageURL)
guard let newDisplayName = self.displayNameTextField.text else { return }
guard let newLocation = self.locationTextField.text else { return }
guard let newDescription = self.bioTextField.text else { return }
guard let newWebsite = self.websiteTextField.text else { return }
guard let newBirthday = self.birthdayTextField.text else { return }
let newUpdatedUserDictionary = ["imageProfile": finalProfileImageURL,
"imageHeader" : finalHeaderImageURL,
"description" : newDescription,
"location": newLocation,
"displayName": newDisplayName,
"website": newWebsite,
"birthday": newBirthday,
]
Database.dataService.updateUserProfile(uid: userID, user: newUpdatedUserDictionary)
showAlert(title: "Hey", msg: "Your profile was updated", actionButton: "OK", viewController: self)
} // Get new uploaded profile and header image URLs
}
}
The enums I use for the switch statements to determine if it's an old URL or a new one:
enum SelectedHeaderImage
{
case True
case False
}
enum SelectedProfileImage
{
case True
case False
}
Class outlets:
var storageProfileDownloadedURL = String()
var storageHeaderDownloadedURL = String()
var oldProfilePhotoImageInDB = String()
var oldHeaderImageInDB = String()
var selectedHeaderImage = SelectedHeaderImage.False
var selectedProfileImage = SelectedProfileImage.False`
From what I understood, your problem is with queuing. You want the code below to execute after the download is complete but it executes in its normal flow. If this is what your problem is then I would suggest you to create another enum, with three download states/count. And move that code below you want to be executed later in a function. Increment the state of new enum when download is complete. It would look something like this:
enum DownloadCount
{
case Zero
case One
case Two
}
var downloadCount = DownloadCount.Zero
and in each of the success block of your download complete change it to, I will just write one here to give you the idea of what needs to be done.
if let profileImgDownloadedURL = url?.absoluteString
{
self.storageProfileDownloadedURL = profileImgDownloadedURL
print(self.storageProfileDownloadedURL)
self.selectedProfileImage = .True
if(downloadCount == .Zero)
{
downloadCount = DownloadCount.One
}
else
{
downloadCount = DownloadCount.Two
}
self.newAssigningFunction()
}
func newAssigningFunction()
{
if(downloadCount == .Two)
{
//Do your storage/saving work here
}
}
Also if you need to execute this function again, it would be best to set downloadCount back to Zero at start of your updateUserProfile function. Let me know if somethis is unclear or you need further help. Or if this was not your case.
I am wanting to update the child values after editing inside the textfields.
At the moment I have this action:
#IBAction func updateAction(_ sender: Any) {
guard let itemNameText = itemName.text, let itemDateText = itemDate.text else { return }
guard itemNameText.characters.count > 0, itemDateText.characters.count > 0 else {
print("Complete all fields")
return
}
let uid = FIRAuth.auth()?.currentUser?.uid
let key = item.ref!.key
let itemList = Item(itemName: itemNameText, itemDate: itemDateText, uid: uid!)
let editItemRef = databaseRef.child("/usersList/\(key)")
editItemRef.updateChildValues(itemList.toAnyObject())
print("edited")
}
I was following this tutorial but he seems to use the username, and as I only have the email or uid (userID) as authentication I thought I'd use the uid.
This is my toAnyObject function inside my class:
func toAnyObject() -> [String: AnyObject] {
return ["itemName": itemName as AnyObject, "itemDate": itemDate as AnyObject, "userID": userID as AnyObject]
}
When I run the breakpoint it does show the edited value of the item however the update doesn't appear to be performing.
Just to be extra safe, try dropping the leading slash from your path:
databaseRef.child("usersList/\(key)")
…and try printing the Error returned by Firebase, if any:
editItemRef.updateChildValues(itemList.toAnyObject()) {
(error, _) in
if let error = error {
print("ERROR: \(error)")
} else {
print("SUCCESS")
}
Edit. We found out he was using the wrong database path. The right one is:
databaseRef.child("users").child(uid!).child("usersList/\(key)")
Has anyone gone through this API and has figured it out?
This is my third time trying to get this to work by following this guide
I am using the swift version of this guide.
Google Apps Script Guide
And it always give me the same errors.
import UIKit
class ViewController: UIViewController {
private let kKeychainItemName = "Google Apps Script Execution API"
private let kClientID = "493692471278-3mf6bo212flgjopl06hrjfeepphe70h4.apps.googleusercontent.com"
private let kScriptId = "Mj0RNm2ZtohFurieBLPwnxYAb4Jnnku4P"
// If modifying these scopes, delete your previously saved credentials by
// resetting the iOS simulator or uninstall the app.
private let scopes = ["https://www.googleapis.com/auth/drive"]
private let service = GTLService() // error Use of unresolved identifier 'GTLService'
let output = UITextView()
// When the view loads, create necessary subviews
// and initialize the Google Apps Script Execution API service
override func viewDidLoad() {
super.viewDidLoad()
output.frame = view.bounds
output.editable = false
output.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0)
output.autoresizingMask = UIViewAutoresizing.FlexibleHeight |
UIViewAutoresizing.FlexibleWidth
// error*** Binary operator '|' cannot be applied to two 'UIViewAutoresizing' operands
view.addSubview(output);
// Error**Use of unresolved identifier 'GTMOAuth2ViewControllerTouch'
if let auth = GTMOAuth2ViewControllerTouch.authForGoogleFromKeychainForName(
kKeychainItemName,
clientID: kClientID,
clientSecret: nil) {
service.authorizer = auth
}
}
// When the view appears, ensure that the Google Apps Script Execution API service is authorized
// and perform API calls
override func viewDidAppear(animated: Bool) {
if let authorizer = service.authorizer,
canAuth = authorizer.canAuthorize where canAuth {
callAppsScript()
} else {
presentViewController(
createAuthController(),
animated: true,
completion: nil
)
}
}
// Calls an Apps Script function to list the folders in the user's
// root Drive folder.
func callAppsScript() {
output.text = "Getting folders..."
let baseUrl = "https://script.googleapis.com/v1/scripts/\(kScriptId):run"
let url = GTLUtilities.URLWithString(baseUrl, queryParameters: nil)
// error ** Use of unresolved identifier 'GTLUtilities'
// Create an execution request object.
var request = GTLObject()
// Error** Use of unresolved identifier 'GTLObject'
request.setJSONValue("getFoldersUnderRoot", forKey: "function")
// Make the API request.
service.fetchObjectByInsertingObject(request,
forURL: url,
delegate: self,
didFinishSelector: "displayResultWithTicket:finishedWithObject:error:")
}
// Displays the retrieved folders returned by the Apps Script function.
func displayResultWithTicket(ticket: GTLServiceTicket,
finishedWithObject object : GTLObject,
error : NSError?) {
if let error = error {
// The API encountered a problem before the script
// started executing.
showAlert("The API returned the error: ",
message: error.localizedDescription)
return
}
if let apiError = object.JSON["error"] as? [String: AnyObject] {
// The API executed, but the script returned an error.
// Extract the first (and only) set of error details and cast as
// a Dictionary. The values of this Dictionary are the script's
// 'errorMessage' and 'errorType', and an array of stack trace
// elements (which also need to be cast as Dictionaries).
let details = apiError["details"] as! [[String: AnyObject]]
var errMessage = String(
format:"Script error message: %#\n",
details[0]["errorMessage"] as! String)
if let stacktrace =
details[0]["scriptStackTraceElements"] as? [[String: AnyObject]] {
// There may not be a stacktrace if the script didn't start
// executing.
for trace in stacktrace {
let f = trace["function"] as? String ?? "Unknown"
let num = trace["lineNumber"] as? Int ?? -1
errMessage += "\t\(f): \(num)\n"
}
}
// Set the output as the compiled error message.
output.text = errMessage
} else {
// The result provided by the API needs to be cast into the
// correct type, based upon what types the Apps Script function
// returns. Here, the function returns an Apps Script Object with
// String keys and values, so must be cast into a Dictionary
// (folderSet).
let response = object.JSON["response"] as! [String: AnyObject]
let folderSet = response["result"] as! [String: AnyObject]
if folderSet.count == 0 {
output.text = "No folders returned!\n"
} else {
var folderString = "Folders under your root folder:\n"
for (id, folder) in folderSet {
folderString += "\t\(folder) (\(id))\n"
}
output.text = folderString
}
}
}
// Creates the auth controller for authorizing access to Google Apps Script Execution API
private func createAuthController() -> GTMOAuth2ViewControllerTouch {
// Error** Use of undeclared type 'GTLServiceTicket' let scopeString = " ".join(scopes) // Error* 'join' is unavailable: call the 'joinWithSeparator()' method on the sequence of elements
return GTMOAuth2ViewControllerTouch(
scope: scopeString,
clientID: kClientID,
clientSecret: nil,
keychainItemName: kKeychainItemName,
delegate: self,
finishedSelector: "viewController:finishedWithAuth:error:"
)
}
// Handle completion of the authorization process, and update the Google Apps Script Execution API
// with the new credentials.
func viewController(vc : UIViewController,
finishedWithAuth authResult : GTMOAuth2Authentication, error : NSError?)
// Error** Use of undeclared type 'GTMOAuth2Authentication' {
if let error = error {
service.authorizer = nil
showAlert("Authentication Error", message: error.localizedDescription)
return
}
service.authorizer = authResult
dismissViewControllerAnimated(true, completion: nil)
}
// Helper for showing an alert
func showAlert(title : String, message: String) {
let alert = UIAlertView(
title: title,
message: message,
delegate: nil,
cancelButtonTitle: "OK"
)
alert.show()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I find it hard to believe that google would make a guide and have it not work for the current version of xcode. It even says at the bottom of their guide that it was last updated February 2016.
Wanted to see if anyone has had any luck with following this guide in the past.
is there another swift guide for this google API?
Thank you in advance.
1)Looks like you aren't getting the header files. You need a Bridging-Header.h. That step is omitted from the Quickstart; see the Google Drive Quickstart for the step you need to take.
2)You can just comment out the following line, it doesn't seem to be necessary.
output.autoresizingMask = UIViewAutoresizing.FlexibleHeight |
UIViewAutoresizing.FlexibleWidth
3)Those changes will fix the compile problems. I found that after fixing the compile problems, it would not link and run on a phone.
The Quickstart guide has you link the GTL.framework with your app using Xcode's 'Link Binary with Libraries'. I instead installed it using Cocoapods (https://cocoapods.org/pods/Google-API-Client) and was then able to run my app.