2017-06-20 32 views
1

新來的iOS發展,使我在實現銷售SDK實施申報和UIApplication的代表

使用

斯威夫特點的這部分有點困惑

的進口宣言:進口SquarePointOfSaleSDK

// Replace with your app's URL scheme. 
let yourCallbackURL = URL(string: "your-url-scheme://")! 

// 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.setClientID("YOUR_CLIENT_ID") 

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

    // Create the request. 
    let apiRequest = 
     try SCCAPIRequest(
      callbackURL: yourCallbackURL, 
      amount: money, 
      userInfoString: nil, 
      merchantID: nil, 
      notes: "Coffee", 
      customerID: nil, 
      supportedTenderTypes: .all, 
      clearsDefaultFees: false, 
      returnAutomaticallyAfterPayment: false 
     ) 

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

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

最後,執行UIApplication委託方法如下:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { 
    guard let sourceApplication = options[.sourceApplication] as? String, 
     sourceApplication.hasPrefix("com.squareup.square") else { 
     return false 
    } 

    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 
} 

我有點困惑,我把這些相應的代碼段。我最好的猜測是UIApplication委託進入AppDelegate.swift?

回答

0

您可以在appDidFinishLaunching方法中設置client IDAppDelegate,因爲它只需要設置一次。

創建並執行API請求的代碼可以放在您希望用來啓動付款的任何視圖控制器中。

你是對的,最後一種方法應該加到你的AppDelegate