2016-05-26 31 views
1

我剛將Chartboost應用到了我的所有快速應用程序中。目標C Swift中的代表

我已獎勵插頁式廣告。所以我需要使用Chartboost給我的目標C函數來設置一個委託。我無法弄清楚如何去做。這就是我通過Swift做Admob的方式。

NSNotificationCenter.defaultCenter().addObserver(self, selector: "presentInterstitial:", name: "AdMobCall", object: nil) 

    func presentInterstitial(sender:UIButton!) { 

    if let isReady = interstitial?.isReady { // _ isReady 
     interstitial?.presentFromRootViewController(self) 

    } 
} 

NSNotificationCenter.defaultCenter().postNotificationName("AdMobCall", object: nil) 

但目標C Chartboost功能我給定爲:

  • (無效)didCompleteRewardedVideo:(CBLocation)位置 withReward:(INT)獎勵;

我不明白如何使用此功能爲Admob創建相同的代理。這似乎不可能。我有Chartboost獎勵intersitials工作。但我無法設置代表,看他們是否觀看了視頻完成與否。

回答

0

如果你想要實現objective-c委託來swift。您需要執行以下步驟。

  1. 需要創建橋文件導入您的Objective-C類

    // 
    // Use this file to import your target's public headers that you would like to expose to Swift. 
    // 
    
    #import "UVIViewController.h" 
    
  2. 您需要在您的SWIFT視圖 - 控制擴展協議名稱

    import UIKit 
    
    class ViewController: UVIViewController, UVIDataRefreshProtocol { // 2. Extend Protocal 
    
        var secondController: DelegateViewController? 
    
        @IBOutlet var delegateRefresh: UILabel! 
    
        override func viewDidLoad() { 
         super.viewDidLoad() 
         // Do any additional setup after loading the view, typically from a nib. 
    
         self.registerForBasicDataRefreshNotification();    // 3. Register the protocol notification 
        } 
    
        override func didReceiveMemoryWarning() { 
         super.didReceiveMemoryWarning() 
         // Dispose of any resources that can be recreated. 
        } 
    
        // Register the protocol notification with same name. Example : DataRefreshNotification 
        override func registerForBasicDataRefreshNotification() { 
    
         self.validateBasicDataRefreshNotification(); 
         NSNotificationCenter.defaultCenter().addObserver(  // 4. Register the protocol notification with same name. 
          self, 
          selector: "basicDataDidRefresh", 
          name: DataRefreshNotification, 
          object: nil) 
    
        } 
    
        override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
         delegateRefresh.text = ""; 
         if segue.identifier == "view_to_delegate" { 
          secondController = segue.destinationViewController as? DelegateViewController 
         } 
        } 
    
        // Basic Data Did Refresh Notification 
        func basicDataDidRefresh() {       // 4. Event will be triggered while calling the protocol post notification. called with same name 
          NSLog("\n Basic data is refreshed") 
          delegateRefresh.text = "Basic data is refreshed"; 
        } 
    
    } 
    

5. NSNotificationCenter.defaultCenter().postNotificationName(DataRefreshNotification, object: nil) // Call the notification. 

示例項目:https://github.com/vigneshuvi/Objective-C-Delegate-Swift