1

I have written a small app in Swift using Xcode 12.5 by following the information and code samples provided here ... https://github.com/square/SquarePointOfSaleSDK-iOS

The app polls a server to see if there is a charge to be made. The output from the server is in JSON format. When a charge comes in, the JSON results are providing a customer id, amount to be charged, and a note to the Square Point of Sale SDK.

Using the SCCAPIRequest example from the GitHub page ...

// Replace with your app's URL scheme.
let callbackURL = URL(string: "<#T##Your URL Scheme##String#>://")!

// Your client ID is the same as your Square Application ID.
// Note: You only need to set your client ID once, before creating your first request.
SCCAPIRequest.setApplicationID(<#T##Application ID##String#>)

do {
    // Specify the amount of money to charge.
    let money = try SCCMoney(amountCents: 100, currencyCode: "USD")

    // Create the request.
    let apiRequest =
        try SCCAPIRequest(
                callbackURL: callbackURL,
                amount: money,
                userInfoString: nil,
                locationID: nil,
                notes: "Coffee",
                customerID: nil,
                supportedTenderTypes: .all,
                clearsDefaultFees: false,
                returnsAutomaticallyAfterPayment: false,
                disablesKeyedInCardEntry: false,
                skipsReceipt: false
        )

    // Open Point of Sale to complete the payment.
    try SCCAPIConnection.perform(apiRequest)

} catch let error as NSError {
    print(error.localizedDescription)
}

The app successfully switches to Square POS, displays the amount due, and knows which customer I am wanting to charge (via customer id). I can process the payment and Square POS switches back to my app just fine.

This is where I am running in to trouble. I am also using the UIApplication delegate method example on that same page. Under the comment "Handle a successful request" ...

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
guard SCCAPIResponse.isSquareResponse(url) else {
    return
}

do {
    let response = try SCCAPIResponse(responseURL: url)

    if let error = response.error {
        // Handle a failed request.
        print(error.localizedDescription)
    } else {
        // Handle a successful request.
    }

} catch let error as NSError {
    // Handle unexpected errors.
    print(error.localizedDescription)
}

return true

}

I have added the following ...

print("Transaction successful: \(response)")

From what I understand, the response should include the transaction id, and anything that was passed along in the userInfoString. It appears that this code example isn't even firing when Square POS returns to my app. I cannot see anything in the Xcode console.

I have assigned a callback URL within Xcode using the documentation on the link above, and it's also added in the Square Developer Portal under the Point of Sale API.

What am I missing? Where should the UIApplication delegate method be placed, in AppDelegate.swift or should it reside in ViewController.swift, or somewhere else? Any insight would be greatly appreciated.

Martin M
  • 231
  • 2
  • 5
  • So what you are saying is "If you want to know what I'm talking about, just refer to the GitHub sample code"? – El Tomato Jul 15 '21 at 23:43
  • @ElTomato Thank you for the comment. I do not often come to Stack Overflow and have been under the impression that it was shunned upon to provide large blocks of code when it could be referenced via a link. I have added the code block from that link. – Martin M Jul 15 '21 at 23:59
  • " It appears that this code example isn't even firing when Square POS returns to my app." That's because `application(_:open:options:)` is used for iOS 12 or lower unless you remove the scene delegate and take care of `UIWindow`. – El Tomato Jul 16 '21 at 00:05
  • @ElTomato Very good to know. Since I'm new to all of this, the reading that I just did seems to indicate that I should be using `onOpenURL(perform:)`. Is this where I should spend my time learning? – Martin M Jul 16 '21 at 00:38
  • Find out how to use `application(_:open:options:)` under iOS 13 and higher, ignoring or deleting SceneDelegate. – El Tomato Jul 16 '21 at 00:50

1 Answers1

0

@ElTomato provided me with the hint that I needed to solve the problem I was having. I needed to delete SceneDelegate.swift, remove Application Scene Manifest from Info.plist, and remove some code from AppDelegate.swift

I found detailed instructions on THIS site ...

iOS 13: Swift - 'Set application root view controller programmatically' does not work

Thank you kindly for the fantastic help @ElTomato

Martin M
  • 231
  • 2
  • 5