私のゲームでは、広告を削除するためのアプリ内購入があります。私はAdmobを使用しています。私のコードは広告に適しています。私が持っている問題は、広告のコードがGameViewController.swiftにあることです。しかし、私のアプリ内購入は私のPurchaseScene.swiftにあります。広告を削除する - アプリ内購入
私はPurchaseScene.swiftで削除機能を実行する方法を見つけませんでした。だから私は私のGameViewController.swiftにあるコード:
class GameViewController: UIViewController, GADBannerViewDelegate, GADInterstitialDelegate {
@IBOutlet weak var banner: GADBannerView!
var interstital: GADInterstitial!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ShowAd(_:)), name: "ShowInterAdKey", object: nil)
if let scene = GameScene(fileNamed:"GameScene") {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = false
skView.showsNodeCount = false
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
scene.size = self.view.bounds.size
skView.presentScene(scene)
}
self.banner.adUnitID = "myUnitID"
self.banner.rootViewController = self
var request: GADRequest = GADRequest()
self.banner.loadRequest(request)
request.testDevices = [kGADSimulatorID]
interstital = GADInterstitial(adUnitID: "myUnitID")
let req = GADRequest()
interstital.loadRequest(req)
}
func adViewDidReceiveAd(bannerView: GADBannerView!) {
banner.hidden = false
}
func adView(bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
banner.hidden = true
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return .AllButUpsideDown
} else {
return .All
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
func ShowAd(sender: AnyObject) {
if (interstital.isReady) {
interstital.presentFromRootViewController(self)
interstital = CreateAd()
}
}
func CreateAd() -> GADInterstitial {
let interstital = GADInterstitial(adUnitID: "myUnitID")
interstital.loadRequest(GADRequest())
return interstital
}
}
そして今私はこのコードをUtilities.swiftに持っています。
let file = "IAP_Ads"
func removeADS() {
Utility.writeValueToFile("YES", fileName: file);
if let value = Utility.readValueFromFile(file) {
print("file exists... value = %@" , Utility.readValueFromFile(file))
let adsRemoved = (value == "YES" ? true : false)
if adsRemoved {
// Code to remove ads, but i don't know what code
print("ads removed")
}
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches{
let location = touch.locationInNode(self)
var node = self.nodeAtPoint(location)
if removeadsBTN.containsPoint(location){
for product in list {
let prodID = product.productIdentifier
if(prodID == "myProductID") {
p = product
buyProduct()
break;
}
}
}
}
}
ステップ3:
import Foundation
クラスユーティリティ{
// Gets a path to your app's local directory where it has permissions to write
static func getFilePathForFile(fileName:String) -> String {
let libraryPath = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)[0]
return libraryPath.stringByAppendingString("/").stringByAppendingString(fileName as String)
}
static func writeValueToFile(value:String, fileName:String) {
do {
try value.writeToFile(Utility.getFilePathForFile(fileName), atomically: true, encoding: NSUTF8StringEncoding)
} catch {
print("\(error)")
}
}
static func readValueFromFile(fileName:String) -> String? {
let file = Utility.getFilePathForFile(fileName);
if let value = try? String(contentsOfFile: file) {
return value;
}
return nil;
}
}
、通話機能は、このようなPurchaseScene.swiftであるbuyProduct()
func buyProduct() {
print("buy " + p.productIdentifier)
let pay = SKPayment(product: p)
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
Utility.writeValueToFile("YES", fileName: "IAP_Ads") //new code
}
購入画面はGameViewControllerとは別のものとしますか?もしそうなら、画面を変更しなければならない(例えば、ナビゲーションコントローラ経由でpush/popのように)、 'viewWillAppear'関数が呼び出されたとき、GameViewController内の" purchase "の状態を調べるだけです。オーバーレイ経由でGameViewControllerのライフサイクルメソッドが呼び出されない場合は、オブザーバー/通知モデルを使用する必要があります。 – thephatp
@thephatpはい別途購入画面があります。 GameViewControllerの「購入」の状態を確認するにはどうすればよいですか?コード例がありますか?ありがとうございます。 – Claudio13
追跡状態は悪くありませんが、これは購入であるため、ユーザーがデバイスを変更した場合、購入時点を復元できるようにしたいと考えています。後でそれを覚えておいてください。今のところ、「状態を見つける」に入るだけで、コードを手に入れましょう。コアデータを使用していますか? (これは、あまりにも多くの仕事を与えることなく、適切なコードを与えるのに役立ちます。) – thephatp