Giter VIP home page Giter VIP logo

Comments (5)

marmelroy avatar marmelroy commented on May 16, 2024

Thanks @lvcloudgit, that's one strange issue. Zip doesn't have anything to do with StoreKit.

Can you share some code so I can investigate your issue further?

from zip.

lvcloudgit avatar lvcloudgit commented on May 16, 2024

@marmelroy Thanks for your reply!
My code is a bit chaos and I don't know which part should be posted... But I found another possibility, still it's not work every time, but maybe it shows some tips?
2016-10-08 12 01 03
The Build Settings of TARGETS has an Option "Always Embed Swift Standard Libraries", the value is "No" by default. I change it to "Yes", the error just disappeared!
But still... It's really bothered me, next morning, the error happened again! And I just change this option back to "No", the error disappeared!
So right now, seems the solution is just switch this option... would you take a guess where the problem could be?

This is the IAPHelper.swift code:

import Foundation
import StoreKit

public typealias bookId = String
public typealias BooksRequestCompleteHandler = (_ success: Bool, _ books:[SKProduct]?) -> ()

open class IAPHelper: NSObject {

    static let IAPHelperPurchaseNotification = "IAPHelperPurchaseNotification"

    var books: Set<bookId>
    fileprivate var purchasedBookIds:Set<bookId> = Set()
    var aRequest:SKProductsRequest?
    var booksRequestFinishedHandler: BooksRequestCompleteHandler?

    init(bookIds: Set<bookId>) {
        books = bookIds
        for id in bookIds {
            let purchased = UserDefaults.standard.bool(forKey: id)
            if purchased {
                purchasedBookIds.insert(id)
            } else {
            }
        }
        super.init()
        SKPaymentQueue.default().add(self)
    }
}

// MARK: StoreKit API
extension IAPHelper {

    public func requestProducts(_ completionHandler: @escaping BooksRequestCompleteHandler) {
        aRequest?.cancel()
        booksRequestFinishedHandler = completionHandler
        aRequest = SKProductsRequest(productIdentifiers: books)
        aRequest!.delegate = self
        aRequest!.start()
    }

    public func buyProduct(_ product: SKProduct) {
        let payment = SKPayment(product: product)
        SKPaymentQueue.default().add(payment)
    }

    public func isProductPurchased(_ productIdentifier: bookId) -> Bool {
        return purchasedBookIds.contains(productIdentifier)
    }

    public class func canMakePayments() -> Bool {
        return SKPaymentQueue.canMakePayments()
    }

    public func restorePurchases() {
        SKPaymentQueue.default().restoreCompletedTransactions()
    }
}

// MARK: SKProductsRequestDelegate
extension IAPHelper: SKProductsRequestDelegate {
    public func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
        let booksGet = response.products
        booksRequestFinishedHandler?(true, booksGet)
        clearRequestANdHandler()

    public func request(_ request: SKRequest, didFailWithError error: Error) {
        booksRequestFinishedHandler?(false, nil)
        clearRequestANdHandler()
    }

    public func clearRequestANdHandler() {
        aRequest = nil
        booksRequestFinishedHandler = nil
    }
}

// MARK: SKPaymentTransactionObserver
extension IAPHelper: SKPaymentTransactionObserver {

    public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
            switch (transaction.transactionState) {
            case .purchased:
                completeTransaction(transaction)
                break
            case .failed:
                failedTransaction(transaction)
                break
            case .restored:
                restoreTransaction(transaction)
                break
            case .deferred:
                break
            case .purchasing:
                break
            }
        }
    }

    fileprivate func completeTransaction(_ transaction: SKPaymentTransaction) {
        UserDefaults.standard.set(true, forKey: transaction.payment.productIdentifier)
        deliverPurchaseNotificatioForIdentifier(transaction.payment.productIdentifier)
        SKPaymentQueue.default().finishTransaction(transaction)
    }

    fileprivate func restoreTransaction(_ transaction: SKPaymentTransaction) {
        guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }
        deliverPurchaseNotificatioForIdentifier(productIdentifier)
        SKPaymentQueue.default().finishTransaction(transaction)
    }

    fileprivate func failedTransaction(_ transaction: SKPaymentTransaction) {
        if transaction.error!._code != SKError.paymentCancelled.rawValue {
            print("Transaction Error: \(transaction.error?.localizedDescription)")
        }

        SKPaymentQueue.default().finishTransaction(transaction)
    }

    fileprivate func deliverPurchaseNotificatioForIdentifier(_ identifier: String?) {
        guard let identifier = identifier else { return }
        purchasedBookIds.insert(identifier)
        UserDefaults.standard.set(true, forKey: identifier)
        UserDefaults.standard.synchronize()
        NotificationCenter.default.post(name: Notification.Name(rawValue: IAPHelper.IAPHelperPurchaseNotification), object: identifier)
    }
}

/// get productIdentifiers
class PurchaseBooks {
    var bookListArray:[String] {
        didSet {
            self.bookIds = Set(bookListArray)
            self.store = IAPHelper(bookIds: self.bookIds)
        }
    }

    var bookIds:Set<bookId> = []
    var store:IAPHelper?
    init() {
        bookListArray = bookListData?.allKeys as! [String]
        bookListArray = []
        bookIds = Set(bookListArray)
        store = IAPHelper(bookIds: self.bookIds)
    }
}
var bookPurchase = PurchaseBooks()

** This is the

from zip.

lvcloudgit avatar lvcloudgit commented on May 16, 2024

The part of Unzip:

func resetDefaultBookOffline() {
        let defaultZip = Bundle.main.path(forResource: "defaultResource", ofType: "zip")
        let booklistFile = "\(documentPath)/BookList.plist"
        let fileMgr = FileManager.default
        if fileMgr.fileExists(atPath: booklistFile) == false {
            do {
                try _ = Zip.unzipFile(URL(string: defaultZip!)!, destination: URL(string:documentPath)!, overwrite: false, password: nil, progress: {
                    (progress: Double) -> () in
                    // print("Unzip progress: \(progress)\n")
                })
                try? fileMgr.removeItem(atPath: documentPath + "/__MACOSX")
                try? fileMgr.removeItem(atPath: documentPath + "/default.zip")

                print("Unzip complete, path:\(documentPath)")

            } catch let error {
                print("Unzip error:\(error.localizedDescription)")
            }
        }
    }

from zip.

lvcloudgit avatar lvcloudgit commented on May 16, 2024

I'm sorry, it still can't work well. The error is:

2016-10-10 1 35 18

It's not always happened, but sometimes. What's this mean? Why is that?
Sometimes it just happened in running Profile mode.

from zip.

lvcloudgit avatar lvcloudgit commented on May 16, 2024

I just use unzip once all over the project, as the code I just posted, pretty simple.
I think it's not caused by any specific code, maybe it caused by some build settings options?

from zip.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.