Skip to content

Instantly share code, notes, and snippets.

@shakked
Created May 18, 2018 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shakked/83eb08687b0c579572682a51204044e6 to your computer and use it in GitHub Desktop.
Save shakked/83eb08687b0c579572682a51204044e6 to your computer and use it in GitHub Desktop.
Old AnalyticsProService.swift
//
// AnalyticsProService.swift
// Command
//
// Created by Zachary Shakked on 1/12/17.
// Copyright © 2017 Shakd, LLC. All rights reserved.
//
import UIKit
import SwiftyJSON
class AnalyticsProService {
static let shared = AnalyticsProService()
fileprivate var isSubscribed: Bool? = nil
var hasValidSubscription: Bool {
// return true
return isSubscribed ?? false
}
fileprivate var sharedSecret: String {
let path = Bundle.main.path(forResource: "Keys", ofType: "plist")!
let keys = NSDictionary(contentsOfFile: path)!
let sharedSecret = keys["iTunesSharedSecret"] as! String
return sharedSecret
}
func checkSubscriptionStatus(completion: @escaping (Bool, Bool?) -> ()) {
guard let receiptURL = Bundle.main.appStoreReceiptURL else {
self.isSubscribed = false
completion(true, false)
return
}
guard let receiptData = try? Data(contentsOf: receiptURL) else {
//No Local Receipt
self.isSubscribed = false
completion(true, false)
return
}
let requestContents = [
"receipt-data": receiptData.base64EncodedString(),
"password": sharedSecret,
]
guard let requestData = try? JSONSerialization.data(withJSONObject: requestContents, options: .prettyPrinted) else {
completion(false, false)
return
}
#if DEBUG
let storeURL = URL(string: "https://sandbox.itunes.apple.com/verifyReceipt")!
#else
let storeURL = URL(string: "https://buy.itunes.apple.com/verifyReceipt")!
#endif
var storeRequest = try! URLRequest(url: storeURL, method: .post)
storeRequest.httpBody = requestData
let urlSession = URLSession(configuration: .default)
let task = urlSession.dataTask(with: storeRequest) { (data: Data?, response: URLResponse?, error: Error?) in
guard let data = data, let dataObject = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) else {
completion(false, nil)
return
}
let repsonseDictionary = dataObject as! NSDictionary
let json = JSON(repsonseDictionary)
guard let receipts = json["latest_receipt_info"].array else {
completion(false, false)
return
}
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .short
var mostRecentExpirationDate: Date? = nil
for receipt in receipts {
let expires_date_ms = receipt["expires_date_ms"].doubleValue
let expirationDate = Date(timeIntervalSince1970: expires_date_ms)
// print("now: \(formatter.string(from: Date())) < expDate: \(formatter.string(from: expirationDate)) ? \(Date() < expirationDate)")
if Date() < expirationDate {
mostRecentExpirationDate = expirationDate
self.isSubscribed = true
}
}
if let mostRecentExpirationDate = mostRecentExpirationDate {
print("Subscription Expiration Date: \(formatter.string(from: mostRecentExpirationDate)) -- currentDate \(formatter.string(from: Date())) -- isSubscribed: \(self.hasValidSubscription)")
} else {
print("Subscription Expiration Date: nil -- currentDate \(formatter.string(from: Date())) -- isSubscribed: \(self.hasValidSubscription)")
}
}
task.resume()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment