I'm making a game in swift and I've implemented iAd successfully using Swift but it's just a small banner at the bottom of the screen now which is fine, but I also want a full-screen advertisement to pop-up when the user is game over. How do I do this? The 'Game Over' view has it's own class by the way. This is what I've got so far:
func bannerViewAdLoad(banner: ADBannerView!) {
if !_bannerIsVisible {
if _adBanner?.superview == nil {
self.view.addSubview(_adBanner!)
}
}
UIView.beginAnimations("animateAdBannerOn", context: nil)
banner.alpha = 1.0
UIView.commitAnimations()
_bannerIsVisible = true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
println("Failed to retrieve Ad")
if _bannerIsVisible {
UIView.beginAnimations("animateAdBannerOff", context: nil)
banner.alpha = 0.0
UIView.commitAnimations()
_bannerIsVisible = false
}
}
Hi Please go through the this document i am not sure but it might help
https://developer.apple.com/library/prerelease/ios/documentation/iAd/Reference/ADInterstitialAd_Ref/index.html
Related
I'm running some regular edgePan code to initialise edgePan detection:
func setupGestures() {
let edgePan = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(screenEdgeSwiped))
edgePan.edges = .left
edgePan.cancelsTouchesInView = false
self.view.addGestureRecognizer(edgePan)
}
and the selector:
func screenEdgeSwiped(recognizer: UIScreenEdgePanGestureRecognizer) {
if recognizer.state == .recognized {
if slideOutMenuView != nil {
slideOutMenuView.show()
}
else {
print("THE SLIDE OUT MENU VIEW IS NIL")
}
}
}
This all works fine but when I give it a test run on the iPhone X. It seems to not even register the gesture.
Is there a different screenGesture that they have introduced or have they completely overwritten the functionality?
The App is in landscape mode.
This is a follow-up to this question.
I'm trying to create an app with a few windows that pop up on start,
and can be closed ,then opened again later.
I'm not having issues with reopening the windows anymore,
but issues with memory. It keeps going up, everytime I request those windows to show again.
Here is my code:
class AppDelegate: NSObject, NSApplicationDelegate {
var parentWC: NSWindowController?
var childWC: NSWindowController?
func showThem() {
if self.parentWC == nil {
let parentWindow = CustomWindow()
self.parentWC.window = parentWindow
}
if self.childWC == nil {
let childWindow = NSWindow()
self.childWC.window = childWindow
self.parentWC.window.addChildWindow(self.childWC.window)
}
}
func closeAllWindows() {
self.childWC.window.close()
self.parentWC.window.close()
}
// From app menu
#IBAction func showThemAgain(sender: AnyObject) {
self.parentWindow.window = nil
self.parentWC = nil
self.childWindow.window = nil
self.childWC = nil
self.showThem()
}
}
class CustomWindow: NSWindow {
func onCloseRequest() {
let appD = (NSApplication.sharedApplication().delegate) as! AppDelegate
appD.closeAllWindows()
}
}
If I'm closing the windows and settings the vars to nil, what is causing the memory usage to go up every time I show these windows? Are they not being closed?
*Note that there is a button in CustomWindow that is used for closing.
With Swift 2, GameCenter is not working for me. The authentication ViewController is not showing up... Here is my func authenticateLocalPlayer():
func authenticateLocalPlayer() {
var localPlayer = GKLocalPlayer()
localPlayer.authenticateHandler = {(viewController: UIViewController?, error: NSError?) -> Void in
if (viewController != nil) {
self.presentViewController(viewController!, animated: true, completion: nil)
print("Not Authenticated. ")
} else {
print("Authenticated. ")
}
}
}
It is returning "Not Authenticated" every time, but is not presenting the ViewController. Any solution?
This solution presents the viewController correctly using Swift 2 in Xcode 7.0.
Note that I have changed the code before the if statement begins. I believe the syntax may have changed in a recent software update as I had this problem too.
In my app I called authenticateLocalPlayer() in the viewDidLoad() method of the GameViewController class.
func authenticateLocalPlayer() {
let localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = {(viewController, error) -> Void in
if (viewController != nil) {
self.presentViewController(viewController!, animated: true, completion: nil)
}
else {
print((GKLocalPlayer.localPlayer().authenticated))
}
}
}
I am trying to implement window toggling (something I've done many times in Objective-C), but now in Swift. It seams that I am getting the use of NSWindowOcclusionState.Visible incorrectly, but I really cannot see my problem. Only the line w.makeKeyAndOrderFront(self) is called after the initial window creation.
Any suggestions?
var fileArchiveListWindow: NSWindow? = nil
#IBAction func tougleFileArchiveList(sender: NSMenuItem) {
if let w = fileArchiveListWindow {
if w.occlusionState == NSWindowOcclusionState.Visible {
w.orderOut(self)
}
else {
w.makeKeyAndOrderFront(self)
}
}
else {
let sb = NSStoryboard(name: "FileArchiveOverview",bundle: nil)
let controller: FileArchiveOverviewWindowController = sb?.instantiateControllerWithIdentifier("FileArchiveOverviewController") as FileArchiveOverviewWindowController
fileArchiveListWindow = controller.window
fileArchiveListWindow?.makeKeyAndOrderFront(self)
}
}
Old question, but I just run into the same problem. Checking the occlusionState is done a bit differently in Swift using the AND binary operator:
if (window.occlusionState & NSWindowOcclusionState.Visible != nil) {
// visible
}
else {
// not visible
}
In recent SDKs, the NSWindowOcclusionState bitmask is imported into Swift as an OptionSet. You can use window.occlusionState.contains(.visible) to check if a window is visible or not (fully occluded).
Example:
observerToken = NotificationCenter.default.addObserver(forName: NSWindow.didChangeOcclusionStateNotification, object: window, queue: nil) { note in
let window = note.object as! NSWindow
if window.occlusionState.contains(.visible) {
// window at least partially visible, resume power-hungry calculations
} else {
// window completely occluded, throttle down timers, CPU, etc.
}
}
I'm working on my first app for OSX 10.8 using Swift. I want to be able to have the state of the battery dictate the text in Today pulldown menu. That aspect of the code works, but I am very frustrated as my class never gets called. It is in a separate supporting script called 'DeviceMonitor.swift'. Thanks for the help!
Code:
import Foundation
import UIKit
class BatteryState {
var device: UIDevice
init() {
self.device = UIDevice.currentDevice()
println("Device Initialized")
}
func isPluggedIn(value: Bool) {
let sharedDefaults = NSUserDefaults(suiteName: "group.WidgetExtension")
let batteryState = self.device.batteryState
if (batteryState == UIDeviceBatteryState.Charging || batteryState == UIDeviceBatteryState.Full){
let isPluggedIn = true
println("Plugged In")
sharedDefaults?.setObject("Plugged In", forKey: "stringKey")
}
else {
let isPluggedIn = false
println("Not Plugged In")
sharedDefaults?.setObject("Not Plugged In", forKey: "stringKey")
}
sharedDefaults?.synchronize()
}
}
Unless you have done it somewhere else in your code, it looks like you haven't registered to receive UIDeviceBatteryLevelDidChangeNotification events. You should also ensure that the current UIDevice has batteryMonitoringEnabled as YES.