Google AdMob横幅广告视图现在会在相机中显示广告

时间:2016-02-13 22:19:06

标签: ios swift cocoa-touch admob

我有一个显示相机预览的UIView,因此用户可以扫描条形码。我正从iAd搬到Admob。我曾经在屏幕底部放置了一个广告,但是,如果我尝试在屏幕底部放置广告,则不会显示该广告。

我创建广告的代码是

self.bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
    self.bannerView.rootViewController = self
    bannerView.delegate = self
    let request = GADRequest()
    self.bannerView.loadRequest(request)

我把它放在视图中加载了。

整个视图控制器的代码是

import UIKit
import AVFoundation
import GoogleMobileAds


class ScanBarcodeViewController: UIViewController, AVCaptureMetadataOutputObjectsDelegate, GADBannerViewDelegate {

//MARK: - Properties
   var barcodeScanned = ""


@IBOutlet weak var cameraView: UIView!
@IBOutlet weak var bannerView: GADBannerView!
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!

var doneButton = UIButton(frame: CGRectMake(130,5,10,10))



//MARK: - didStuff
override func viewDidLoad() {
    super.viewDidLoad()
    //Camera stuff
    captureSession = AVCaptureSession()
    doneButton.setTitle("Done", forState: UIControlState.Normal)
    self.view.addSubview(doneButton)

    let videoCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    let videoInput: AVCaptureDeviceInput

    if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) == AVAuthorizationStatus.Denied {
        let alert = UIAlertController(title: "Camera permission", message: "To scan a barcode, Inventory Made Simple needs camera access. Please enable it to continue.", preferredStyle: .Alert)

        let settingsAction = UIAlertAction(title: "Settings", style: .Default, handler: { (alertAction) -> Void in
            if let appSettings = NSURL(string: UIApplicationOpenSettingsURLString) {
            UIApplication.sharedApplication().openURL(appSettings)
            }
        })

        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)


        alert.addAction(settingsAction)
        alert.addAction(cancelAction)

        presentViewController(alert, animated: true, completion: nil)

    }


    do {
        videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
    } catch {
        return
    }

    if (captureSession.canAddInput(videoInput)) {
        captureSession.addInput(videoInput)
    } else {
        failed()
        return
    }

    let metadataOutput = AVCaptureMetadataOutput()

    if (captureSession.canAddOutput(metadataOutput)) {
        captureSession.addOutput(metadataOutput)

        metadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
        metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypePDF417Code]
    } else {
        failed()
        return
    }
    //This puts the video in the UIView


    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    //this resizes the camera to be the same size as the UIView
    previewLayer.frame = cameraView.layer.bounds
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
    //previewLayer.videoGravity = AVLayerVideoGravityResizeAspect
    cameraView.layer.addSublayer(previewLayer)

    //Starts the camera session
    captureSession.startRunning()

    //bannerView.hidden = true
    self.bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
    self.bannerView.rootViewController = self
    bannerView.delegate = self
    let request = GADRequest()
    self.bannerView.loadRequest(request)

}

//MARK: - Functions

func appDelegate() -> AppDelegate {
    return UIApplication.sharedApplication().delegate as! AppDelegate
}


func failed() {
    let ac = UIAlertController(title: "Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.", preferredStyle: .Alert)
    ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
    presentViewController(ac, animated: true, completion: nil)
    captureSession = nil

}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)



    //Camera stuff
    if captureSession?.running == false {
        captureSession.startRunning()
    }
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)


    if (captureSession?.running == true) {
    captureSession.stopRunning()
    }
}

//When a barcode is found
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
    captureSession.stopRunning()

    if let metadataObject = metadataObjects.first {
        let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject
        //Makes the phone vibrate
        AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))

        foundCode(readableObject.stringValue)
    }


}

func foundCode(code: String) {

    barcodeScanned = code
    performSegueWithIdentifier("SendCode", sender: self)
}
//MARK: - Navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "SendCode" {
        let destinationVC = AddItemViewController()
        destinationVC.barcode = barcodeScanned
    }
}

//MARK: - Actions

@IBAction func cancel() {
    dismissViewControllerAnimated(true, completion: nil)
}


func adViewDidReceiveAd(bannerView: GADBannerView!) {
    print("got the ad")
    bannerView.hidden = false
}
}

在其他视图控制器中加载广告的工作正常。 我试图隐藏显示相机预览的UIView,只留下广告横幅,但它仍然不起作用。

1 个答案:

答案 0 :(得分:0)

您必须定位横幅视图。这样的事情:

    self.view.addSubview(adBanner!)

    // Bottom Constraint
    self.view.addConstraint(NSLayoutConstraint(item: adBanner!, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))

    // Left Right
    self.view.addConstraint(NSLayoutConstraint(item: adBanner!, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: adBanner!, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0))
    adBanner!.translatesAutoresizingMaskIntoConstraints = false

    // Set Height
    constraintAdHeight = NSLayoutConstraint(item: adBanner!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: CGSizeFromGADAdSize(kGADAdSizeSmartBannerLandscape).height)
    view.addConstraint(constraintAdHeight!)