Implement unity banner ads in swift - swift

I have a game that I want to implement Unity banner ads to. I have implemented Unity rewarded and interstitial ads but there is no documentation or videos about banner ads. I had Admob Ads but Google blocked my account for 30 days so I need to transfer to Unity in this month.

there is my implementation:
Create method like this:
func prepareUnityBannerAdd() {
if UnityAds.isReady() {
UnityAdsBanner.initialize()
UnityAdsBanner.setDelegate(self)
UnityAdsBanner.setBannerPosition(UnityAdsBannerPosition.topCenter)
UnityAdsBanner.load(UNITY.banner) // This is my placment id string
}
}
in UnityAdsDelegate method AdsReady call like this:
func unityAdsReady(_ placementId: String) {
prepareUnityBannerAdd()
}
and finally in UnityAdsBannerDelegate method BannerDidLoad do something like this:
func unityAdsBannerDidLoad(_ placementId: String, view: UIView) {
bannerView.addSubview(view) // bannerView is just UIView to hold banner...
}
I hope this will help you

Related

How to add listener for all running applications

I want to display a list of all running application names.
Issue: It doesn't add an app that is running after the function is called. Therefore, it doesn't add the app name to the list
simultaneously.
Goal: I want to add a listener, so if a new app is running it will add it to the array simultaneously without restarting the app or recalling the function again.
func allRunningApplications() {
for runningApplication in NSWorkspace.shared.runningApplications {
let appName = runningApplication.localizedName
// Add App Name to Array
array.append(appName)
}
}
I mentioned the "did launch", et. al., notifications because you didn't explain why you wanted to monitor the set of running applications.
If you are only interested in whether a specific app has launched (or quit), it would probably be easier to use the NSWorkspace notifications:
(untested code)
let center = NSWorkspace.shared.notificationCenter
center.addObserver(forName: NSWorkspace.didLaunchApplicationNotification,
object: nil, // always NSWorkspace
queue: OperationQueue.main) { (notification: Notification) in
if let app = notification.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication {
if app.bundleIdentifier == "com.apple.Terminal" {
// User just launched the Terminal app; should we be worried?
}
}
}
Note that workspace notifications are posted to NSWorkspace's private notification center, not the default notification center, so remember to add your observers there.
You could poll the runningApplications property (check it every x seconds) to test, if there is a new application. But it's not recommended: https://developer.apple.com/documentation/appkit/nsworkspace/1534059-runningapplications
Similar to the NSRunningApplication class’s properties, this property
will only change when the main run loop is run in a common mode.
Instead of polling, use key-value observing to be notified of changes
to this array property.
So use key-value observing on NSWorkspace.shared.runningApplications
A good example can be found here: https://www.ralfebert.de/ios-examples/swift/property-key-value-observer/
For your code it should be something like this:
var observers = [NSKeyValueObservation]()
override func viewDidLoad() {
super.viewDidLoad()
observeModel()
}
func observeModel() {
self.observers = [
NSWorkspace.shared.observe(\.NSWorkspace.runningApplications, options: [.initial]) {(model, change) in
// runningApplications changed, so update your UI or something else
}
]
}
(untested code)
You can try using the notification centre of NSWorkspace.
self.workspace = [NSWorkspace new];
NSArray *myObserver;
myObserver = (NSArray*) [[[NSWorkspace sharedWorkspace] notificationCenter] addObserverForName: NSWorkspaceWillLaunchApplicationNotification object:nil queue:nil usingBlock:^(NSNotification *note)
{
if(note)
{
//do your action
}
}
];
NSWorkspaceWillLaunchApplicationNotification will notify you if any application is about to be launched.

Motion shake action not working anymore in swift

Hi I have hit a bit of a brick wall, I have been redesigning my menu navigation for my app. which I have managed to do. But now one of the features of my app has decided to stop functioning.
The idea is you shake your phone and it chooses a picture at random, the code separate from the app works fine, just as it has done on all previous versions of it, I even ran it quickly in it's present form on it's own just in case and it worked perfectly.
hopefully somebody can point me in the direction of where I have gone wrong.
my code is as follows;
import Foundation
import UIKit
class cocktailChoice: UIViewController {
#IBOutlet weak var drinkImage: UIImageView!
var drinkNamesArray:[String] = ["cocktailList0","cocktailList1","cocktailList2","cocktailList3","cocktailList4","cocktailList5","cocktailList6","cocktailList7","cocktailList8","cocktailList9","cocktailList10","cocktailList11","cocktailList12","cocktailList13","cocktailList14","cocktailList15","cocktailList16","cocktailList17","cocktailList18","cocktailList19","cocktailList20","cocktailList21","cocktailList22","cocktailList23","cocktailList24","cocktailList25","cocktailList26","cocktailList27","cocktailList28","cocktailList29","cocktailList30","cocktailList31"]
override func viewDidLoad() {
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
if motion == .motionShake{
let firstRandomNumber = Int(arc4random_uniform(32))
let DrinkString:String = self.drinkNamesArray[firstRandomNumber]
self.drinkImage.image = UIImage(named: DrinkString)
}
}
}
it compiles with no errors, the IBOutlet is connect, it has no errors that i can see but the shake action doesn't want to play. getting very frustrating now.
Instead of this part if motion == .motionShake{ , can you try to use this:
if(event.subtype == .motionShake) {
print("Shake event!")
}

Programmatically disabling screenshot in App

I want to prevent taking screenshot of a page in app.
how to do it programmatically so that screenshots cannot be taken.
Found code to detect screenshot. Can it be deleted as soon as a screenshot is taken?
let mainQueue = NSOperationQueue.mainQueue()
NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationUserDidTakeScreenshotNotification,
object: nil,
queue: mainQueue) { notification in
// executes after screenshot
}
There is no way to prevent ScreenShots but you can prevent Screen Recording
through this code.
func detectScreenRecording(action: #escaping () -> ()) {
let mainQueue = OperationQueue.main
NotificationCenter.default.addObserver(forName: UIScreen.capturedDidChangeNotification, object: nil, queue: mainQueue) { notification in
// executes after screenshot
action()
}
}
//Call in vewWillApper
detectScreenRecording {
print(UIScreen.main.isCaptured)
if UIScreen.main.isCaptured {
//your vier hide code
print("self.toHide()")
} else {
// self.sceneDeleg(ate?.window?.isHidden = false
//your view show code
print("self.toShow()")
}
}
There is absolutely no way to completely prevent user from taking screenshot during the app process, and that's because you do not have access to delete photos in the photo gallery of the user. It would totally be a security issue if you could access your user's photos.
However, there are ways to partially prevent screenshots, as described here: Prevent screen capture in an iOS app
Technically that is possible, via the Photos framework, the docs for which can be found here.
Example code can be found here.
However, this will ask the user's permission first, and then again to confirm deletion; so possibly not the ideal solution. Unfortunately this is as good as it gets as Apple has the Camera Roll fairly locked down.
You cannot prevent user from taking screenshot, however, you can hide the content while a screenshot is taken, Use this code to do so..
extension UIView {
func hideContentOnScreenCapture() {
DispatchQueue.main.async {
let field = UITextField()
field.isSecureTextEntry = true
self.addSubview(field)
field.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
field.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
self.layer.superlayer?.addSublayer(field.layer)
field.layer.sublayers?.first?.addSublayer(self.layer)
}
}
}
Usage:
yourView.hideContentOnScreenCapture()

swift function to show the score in the Game Center

I am developing a game with five scenes (SKSecne) in swift. I am using the following function to show the score in the Game Center at the end of each scene. Currently I have to copy the function to all the scene files.
How can I modify the function so I can call it from all the scene files without duplicating it?
func showLeader() {
let viewControler = self.view?.window?.rootViewController
let gameCenter = GKGameCenterViewController()
gameCenter.gameCenterDelegate = self
viewControler?.presentViewController(gameCenter, animated: true, completion: nil) }
One solution is just create a subclass of SKScene and use it like parent for others five scenes.
class BasicScene: SKScene {
func showLeader() {}
}
class Scene1: BasicScene {
// call showLeader() when needed
}

When my reward video finishes it doesn't give the user the coins.......Why?

Im using Chartboost reward videos and when the video is done playing I want to give the user 200 coins. I added the delegate function didCompleteRewardedVideo in my GameViewController but it doesn't give the user 200 coins after the video is finished being watched. What am I doing wrong?
func didCompleteRewardedVideo(location: String!, withReward reward: Int32) {
println("REWARDS!!!!")
gameScene.coins += 200
gameScene.coinLabel.text = String(gameScene.coins)
}
You are calling your delegate function GameViewController instead of doing that call your function into your GameScene where your coins are. and it will add and save it.