【发布时间】:2016-05-31 17:05:32
【问题描述】:
在我的游戏中,我希望通过应用内购买来移除广告。我使用 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 中有这段代码。
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 中是这样的:
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 步:购买产品()
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 是分开的?如果是这样,并且您必须更改屏幕(例如,通过导航控制器推送/弹出),那么您需要做的就是在调用
viewWillAppear函数时检查 GameViewController 中“购买”的状态。如果它是通过覆盖,并且您的 GameViewController 的生命周期方法不会被调用,那么您需要使用观察者/通知模型。 -
@thephatp 是的,我有一个单独的购买屏幕。如何检查 GameViewController 中“购买”的状态?你有代码示例吗?非常感谢
-
跟踪状态还不错,但由于这是一次购买,我假设您希望能够在用户更换设备时恢复购买。请记住这一点以备后用。现在,只是进入“寻找状态”,让我为您获取一些代码。您是否使用核心数据? (这将帮助我在不给你太多工作的情况下为你提供正确的代码。)
-
@thephatp 好吧,那太好了。不,这是一个精灵套件游戏。我认为在这我不能使用核心数据。谢谢! :)
-
所以你添加了 Utility 文件,并且你添加了提供给你的 PurchaseScene.swift 的方法。 步骤 1 完成。 第 2 步,在 PurchaseScene 中,添加以下行:
if let value = Utility.readValueFromFile(file),将print("ads removed")替换为代码以删除任何可能可见的广告。 步骤 3 在buyProduct()方法中,调用Utility.writeValueToFile("YES", "IAP_Ads")。 第 4 步,更新GameViewController并调用Utility.readValueFromFile("IAP_Ads"),如果应删除广告,则不要显示展示广告的视图。
标签: ios swift sprite-kit in-app-purchase