Objective-C
引入 SDK
#import
@interface ViewController ()
@property (nonatomic, strong) MFInterstitialView *fullbanner;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.fullbanner = [[MFInterstitialView alloc] initWithBannerId:@"21951"];
self.fullbanner.delegate = self;
[self.fullbanner debugInfo:@"info"];
}
- (IBAction)reqAdAction:(UIButton *)sender {
[self.fullbanner requestAd];
}
// 成功請求廣告
- (void)requestAdSuccess {
NSLog(@"請求廣告成功");
[self.fullbanner show];
}
// 請求廣告失敗
- (void)requestAdFail {
NSLog(@"請求廣告失敗");
}
// 蓋版廣告點擊後
- (void)onClickAd:(NSString *)urlStr {
NSLog(@"點擊廣告 URL: %@", urlStr);
NSURL *url = [NSURL URLWithString:urlStr];
if (url) {
NSString *path = [url path];
NSLog(@"%@", path);
}
}
// 關閉蓋板廣告後
- (void)onCloseAd {
NSLog(@"關閉廣告!");
}
@end
Swift
引入 SDK
import iMFAD
class ViewController: UIViewController {
var fullbanner: MFInterstitialView?
override func viewDidLoad() {
super.viewDidLoad()
fullbanner = MFInterstitialView(bannerId: "21951")
fullbanner?.delegate = self
fullbanner?.debugInfo("info")
}
@IBAction func reqAdAction(_ sender: UIButton) {
fullbanner?.requestAd()
}
}
extension ViewController: MFInterstitialViewDelegate {
// 成功請求廣告
func requestAdSuccess() {
print("請求廣告成功")
fullbanner?.show()
}
// 請求廣告失敗
func requestAdFail() {
print("請求廣告失敗")
}
// 蓋版廣告點擊後
func onClickAd(_ urlStr: String) {
print("點擊廣告 URL: \(urlStr)")
if let url = URL(string: urlStr) {
let path = url.path
print(path)
}
}
// 關閉蓋板廣告後
func onCloseAd() {
print("關閉廣告")
}
}
SwiftUI
引入 SDK
import iMFAD
struct ContentView: View {
@ObservedObject var interstitialController = InterstitialAdController()
var body: some View {
VStack {
Button("請求廣告") {
interstitialController.requestAd()
}
.padding()
.background(Color.blue.opacity(0.1))
.cornerRadius(8)
}
.onAppear {
DispatchQueue.main.async {
let interstitial = MFInterstitialView(bannerId: "21951")
interstitial.delegate = interstitialController
interstitial.debugInfo("info")
interstitialController.interstitialView = interstitial
}
}
}
}
class InterstitialAdController: NSObject, ObservableObject, MFInterstitialViewDelegate {
var interstitialView: MFInterstitialView?
// 請求廣告
func requestAd() {
interstitialView?.requestAd()
}
// MARK: - MFInterstitialViewDelegate
func requestAdSuccess() {
print("請求廣告成功")
interstitialView?.show()
}
func requestAdFail() {
print("請求廣告失敗")
}
func onClickAd(_ urlStr: String) {
print("點擊廣告 URL: \(urlStr)")
if let url = URL(string: urlStr) {
let path = url.path
print(path)
}
}
func onCloseAd() {
print("關閉廣告")
}
}