How to put AdMob Banner in SpriteKit? - sprite-kit

I am creating an game in SpriteKit, and I don't know how to use AdMob banner in SpriteKit. I know how to use with Swift where you drag the view to your storyboard, and etc... But once again, Can anyone help me to use it in SpriteKit?
Getting this error when applying to place banner.
To get test ads on this device, call: request.testDevices = #[ *!********'" ];**

Everything is the exact same as with UIKit, except that you have to position it programmatically. Also make sure that this code goes in viewDidLoad in your GameViewController. Just add this line in:
BannerAd.frame = CGRect(x: 0, y: 0, width: 320, height: 50)
All in all you banner ad code should look something like this:
var BannerAd = GADBannerView(adSize: kGADAdSizeBanner)
BannerAd.frame = CGRect(x: 0, y: 0, width: 320, height: 50)
BannerAD.delegate = self
BannerAD.adUnitID = "ca-app-pub-##/##"
BannerAD.rootViewController = self
let request = GADRequest()
request.testDevices = [kGADSimulatorID, "*************************"]
BannerAD.loadRequest(request)
view.addSubview(BannerAd)

Related

how to resize a view in live playgrounds ?? Xcode

I am planning to create a live playground for coming wwdc scholarships but I have no experience in creating live playgrounds because I have worked with single view apps so far
I actually don't know what size should be the view in my playground and I am not able to resize it can any one tell me how to do that , I have tried the below code but it gives me error and doesn't work please help me and any advice related to wwdc project is appreciated thank you
let main = viewController()
main.view.frame.size = CGSize(width: 300, height: 600)
this code gives error:= expression failed to parse, unknown error
Please tell me how to resize the view and also how should I do it ??
You should change entire frame or bounds properties to resize the view:
import UIKit
import PlaygroundSupport
class ViewController : UIViewController {
...
}
let viewController = ViewController()
viewController.view.frame = CGRect(x: 0, y: 0, width: 300, height: 600)
PlaygroundPage.current.liveView = viewController
PlaygroundPage.current.needsIndefiniteExecution = true
When you use navbar it is important to size the NavigationController and not the ViewController.
Like this:
let vc = MyViewController()
let navBar = UINavigationController(rootViewController: vc)
navBar.view.frame = CGRect(x: 0, y: 0, width: 320, height: 568)
PlaygroundPage.current.liveView = navBar

viewController does not update

I have 2 UIViewControllers, one starting the GameScene and the other is supposed to show the highscore, as below. The problem is that my second UIViewController does not want to show the udpated highscore. It just keeps on showing 0 all the time.
Does anyone know what the problem is? I tried using viewDidLoad as well as viewWillAppear but none of them work.
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.center = CGPoint(x: 160, y: 285)
label.textAlignment = .center
label.text = String(GameScene().highScore)
self.view.addSubview(label)
}
in the GameScene, I have declared the highScore as so:
var highScore = Int()
and used in a gameOver function as so:
if score > highScore {highScore = score}
thanks
label.text = String(GameScene().highScore)
This piece of line suggests GameScene is a static class. Un less it is, you simply cannot access variables in such a manner.
You will need to pass data between view controllers to pass data between the scenes. You can pass data using one of three options:
Though performSegue()
performSegueWithIdentifier() and make a connection in IB
make a connection in IB and define an action

How to get action of button in Swift MacOS

I am trying to set up zoom for a game where you click on a button to zoom in, and another to zoom out (which will be part of the UI).
I am having problems getting the input of the buttons to the code. I can read that the button is not pressed, but I cannot figure out how to read if it is pressed.
Looking through the API reference gave me the idea to try using the "state" of the button, but it always returns 0, even when the button is pressed. This could be because the update function does not update as fast as the button's state changes.
Here is my code:
class GameScene: SKScene {
override func sceneDidLoad() {
cam = SKCameraNode()
cam.xScale = 1
cam.yScale = 1
self.camera = cam
self.addChild(cam)
cam.position = CGPoint(x: 0, y: 0)
setUpUI()
}
func setUpUI(){
let button1Rect = CGRect(x: 20, y: 80, width: 80, height: 40)
let zoomIn = NSButton(frame: button1Rect) //place a button in the Rect
zoomIn.setButtonType(NSMomentaryPushInButton)
zoomIn.title = "Zoom In"
zoomIn.alternateTitle = "Zoom In"
zoomIn.acceptsTouchEvents = true
view?.addSubview(zoomIn) //put button on screen
let button2Rect = CGRect(x: 20, y: 30, width: 80, height: 40)
let zoomOut = NSButton(frame: button2Rect) //place a button in the Rect
zoomOut.setButtonType(NSMomentaryPushInButton)
zoomOut.title = "Zoom Out"
zoomOut.alternateTitle = "Zoom Out"
zoomOut.acceptsTouchEvents = true
view?.addSubview(zoomOut) //put button on screen
if zoomIn.state == 1{ //this loop is never true no matter how many times I press the button
print("zoom")
}
}
}
I did cut out some of the code involving the mouse as it is not relevant.
Answer is from TheValyreanGroup.
Simply define the button outside of the setUpUI() function (outside of the GameScene or within it works, outside of the GameScene is easier).

UIImageView and UIWebView in UIScrollView

I have added UIImageView with view frame height and width and below that UIWebView has to be loaded. I am not sure how to implement this. I tried to add both in UIScrollView. Image is perfectly displaying but WebView not loaded. Adding UIWebView to UIScrollView is not good practice as its below screen, I need to make it in scroll. I am not using Storyboard, all through programming.
Please suggest what should be the best approach to implement this. Wether to use UIWebView in UIScrollView or if added then why it's not loaded. It comes blank.
func viewDidLoad(){
var image = UIImage(frame: view.bounds)
var webView = UIWebView(frame: CGRect(x: 0, y: self.view.frame.height, width: self.view.frame.width, height: self.view.frame.height))
var scrollView = UIScrollView()
scrollView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height*2)
webView.delegate = self
webView.loadRequest(URLRequest(url: URL(string: "http://www.google.com")))
scrollView.addSubview(image)
scrollView.addSubView(webView)
self.view.addSubview(scrollView)
}
func webViewDidFinishLoad(_ webView: UIWebView){
}
func webViewDidStartLoad(_ webView: UIWebView){
}
Thanks
Your code is fine.
Please check if you are getting this error:
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
If you are getting this error, then add this in info.plist file:

Is it possible to custom size the fabric twitter login button?

Supposedly, the twitter login button is a subclass of UIButton but none of the methods associated with setting a UIButton(such as setTitle(), setBackgroundColor() etc) object seem to work with the twitter button. Specifically, I'm trying to resize it but nothing seems to work. Here's what I've tried so far in my viewDidLoad()
let twitterLoginButton = TWTRLogInButton(frame: CGRectMake(0, 0, 200, 50))
self.view.addSubview(twitterLoginButton)
I also tried this
let twitterLoginButton = TWTRLogInButton.init(frame: CGRectMake(0, 0, 200, 50))
self.view.addSubview(twitterLoginButton)
Then I tried to create a container UIView for the login button with the above rect parameters and then place the button into it, instead of directly in the UIView controller like so
let twitterLoginButton = TWTRLogInButton()
let twitterLoginButtonParent = TWTRLogInButton(frame: CGRectMake(0, 0, 200, 50))
twitterLoginButtonParent.addSubview(twitterLoginButton)
self.view.addSubview(twitterLoginButtonParent)
let twitterLoginButtonSize = CGSize(width: 200, height: 50)
twitterLoginButton.sizeThatFits(twitterLoginButtonSize)
At least this time, the button doesn't get stuck in the top left hand corner but is forced to assume the x and y positions of its new parent. However it still cant be forced to assume the size of its parent as I intended.
Is there any way the button can be forced to resize? With the facebook button, it's much easier.
This solved it. Apparently I had to first initialize the button with loginCompletion
let twitterLoginButton = TWTRLogInButton(logInCompletion: { session, error in
if (session != nil) {
print("signed in as \(session!.userName)");
} else {
print("error: \(error!.localizedDescription)");
}
})
twitterLoginButton.frame = CGRectMake(0, 0, 200, 50)