2014-11-08 1 views
4

탭이있는 컨트롤러의 공간을 확보하기 위해 배너보기 iAd에서 삽입 광고로 전환하는 방법을 알아 내려고합니다. 어떤 이유로 나는 신속하게 이러한 광고를 시작할 수있는 자원을 찾을 수 없습니다.Swift (Xcode 6.1)에서 삽입 광고를 구현하는 방법

누구나 Swift와 함께 삽입 광고 iAds에 대한 정보를 제공하고 프로젝트에서 어떻게 구현할 수 있습니까? 여기

+1

체크 아웃 [스위프트의 AdMob 튜토리얼] (http://www.ios-blog.co.uk/tutorials/swift/display-ads-in-your-application-with-google-admob/) - 이 사실을 알기 전까지 말 그대로 문자 그대로이 시련을 겪었습니다. (iAds가 닫힙니다.) – JamesG

답변

3

내가 내 프로젝트에서 사용하는 방법입니다

// 말게 프레임 워크를 추가

import iAd 

// 말게 위임을 준수

class ViewController: UIViewController,ADInterstitialAdDelegate 


//create instance variable 
var interstitial:ADInterstitialAd! 

// 기본 말게 삽입 광고 닫기 버튼을 제공하지 않습니다 그래서 수동으로 만들어야합니다.

var placeHolderView:UIView! 
var closeButton:UIButton! 



override func viewDidLoad() { 
    super.viewDidLoad() 

    //iAD interstitial 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: ("runAd:"), name:UIApplicationWillEnterForegroundNotification, object: nil) 

} 




//iAD interstitial 
func runAd(notification:NSNotification){ 
var timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: Selector("dislayiAdInterstitial"), userInfo: nil, repeats: false) 
cycleInterstitial() 
} 

func cycleInterstitial(){ 

    // Clean up the old interstitial... 
    // interstitial.delegate = nil; 
    // and create a new interstitial. We set the delegate so that we can be notified of when 
    interstitial = ADInterstitialAd() 
    interstitial.delegate = self; 
} 

func presentInterlude(){ 
    // If the interstitial managed to load, then we'll present it now. 
    if (interstitial.loaded) { 

     placeHolderView = UIView(frame: self.view.frame) 
     self.view.addSubview(placeHolderView) 

     closeButton = UIButton(frame: CGRect(x: 270, y: 25, width: 25, height: 25)) 
     closeButton.setBackgroundImage(UIImage(named: "error"), forState: UIControlState.Normal) 
     closeButton.addTarget(self, action: Selector("close"), forControlEvents: UIControlEvents.TouchDown) 
     self.view.addSubview(closeButton) 


     interstitial.presentInView(placeHolderView) 
    } 
} 

// iAd Delegate Mehtods 

// When this method is invoked, the application should remove the view from the screen and tear it down. 
// The content will be unloaded shortly after this method is called and no new content will be loaded in that view. 
// This may occur either when the user dismisses the interstitial view via the dismiss button or 
// if the content in the view has expired. 

func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!){ 
    placeHolderView.removeFromSuperview() 
    closeButton.removeFromSuperview() 
    interstitial = nil 

    cycleInterstitial() 
} 


func interstitialAdActionDidFinish(_interstitialAd: ADInterstitialAd!){ 
    placeHolderView.removeFromSuperview() 
    closeButton.removeFromSuperview() 
    interstitial = nil 

    println("called just before dismissing - action finished") 

} 

// This method will be invoked when an error has occurred attempting to get advertisement content. 
// The ADError enum lists the possible error codes. 
func interstitialAd(interstitialAd: ADInterstitialAd!, 
    didFailWithError error: NSError!){ 
     cycleInterstitial() 
} 


//Load iAd interstitial 
func dislayiAdInterstitial() { 
    //iAd interstitial 
    presentInterlude() 
} 


func close() { 
    placeHolderView.removeFromSuperview() 
    closeButton.removeFromSuperview() 
    interstitial = nil 

} 
여기
+0

훌륭한 코드! 나는이 일을하고 있지만, 3.0 줄이있는 이유는 궁금하다. NSTimer.scheduledTimerWithTimeInterval (3.0, target : self, selector : Selector ("dislayiAdInterstitial"), userInfo : nil, repeats : false) –

+0

기본적으로 광고 사용 데이터. 따라서 서비스가 좋지 않아도 광고를 사용할 수 있도록 다운로드하려면 3 초 정도 기다려야합니다. –

17

이 방법은

import UIKit 
import iAd 

class ViewController: UIViewController, ADInterstitialAdDelegate { 
var interstitialAd:ADInterstitialAd! 
var interstitialAdView: UIView = UIView() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    loadInterstitialAd() 
} 

func loadInterstitialAd() { 
    interstitialAd = ADInterstitialAd() 
    interstitialAd.delegate = self 
} 

func interstitialAdWillLoad(interstitialAd: ADInterstitialAd!) { 

} 

func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) { 
    interstitialAdView = UIView() 
    interstitialAdView.frame = self.view.bounds 
    view.addSubview(interstitialAdView) 

    interstitialAd.presentInView(interstitialAdView) 
    UIViewController.prepareInterstitialAds() 
} 

func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) { 
    interstitialAdView.removeFromSuperview() 
} 

func interstitialAdActionShouldBegin(interstitialAd: ADInterstitialAd!, willLeaveApplication willLeave: Bool) -> Bool { 
    return true 
} 

func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) { 

} 

func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) { 
    interstitialAdView.removeFromSuperview() 
} 

의 사용을 필요로하지 않기 때문에 삽입 광고를 구현하는 방법을 따라 상대적으로 깨끗하고 쉽게 그리고 당신은 단지 추적하기 위해 각 기능에 println("Function Name")를 넣어 경우 도움이 될 수 있습니다 삽입 광고 프로세스의 이 코드 블록을 개선하기위한 질문이나 방법이 있으면 의견을 남겨주십시오. 하지만 난 당신이 앱 위임이를 추가하면 3 초 지연은 위에 제시된 것을 피할 수 있습니다

override func prepareForSegue(segue: UIStoryboardSegue, 
       sender: AnyObject?) { 

let destination = segue.destinationViewController 
      as UIViewController 
destination.interstitialPresentationPolicy = 
    ADInterstitialPresentationPolicy.Automatic 
} 
1

나는 이것이 오래 알고 감사드립니다.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
     UIViewController.prepareInterstitialAds() 
     return true 
    } 
관련 문제