Objective-C
引入 SDK
#import
@interface ViewController ()
@property (nonatomic, strong) MFAdBottomView *adbottom;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.adbottom = [[MFAdBottomView alloc] initWithBannerId:@"21963"];
self.adbottom.delegate = self;
[self.adbottom debugInfo:@"info"];
}
- (IBAction)requestAD:(id)sender {
[self.view addSubview:self.adbottom];
[self.adbottom requestAd];
}
// 成功請求廣告
- (void)requestAdSuccess {
[self.adbottom show];
NSLog(@"成功請求廣告");
}
// 請求廣告失敗
- (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 adbottom : MFAdBottomView?
override func viewDidLoad() {
super.viewDidLoad()
adbottom = MFAdBottomView(bannerId: "21963")
adbottom?.delegate = self
adbottom?.debugInfo("info")
}
@IBAction func requestAD(_ sender: Any) {
self.view.addSubview(adbottom!)
adbottom?.requestAd()
}
}
extension ViewController: MFAdBottomViewDelegate{
/// 成功請求廣告
func requestAdSuccess() {
adbottom?.show()
print("成功請求廣告")
}
/// 請求廣告失敗
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 bottomAdController = BottomAdController()
var body: some View {
VStack {
Button("請求廣告") {
bottomAdController.requestAd()
}
.padding()
.background(Color.blue.opacity(0.1))
.cornerRadius(8)
}
.onAppear {
DispatchQueue.main.async {
let bottomAd = MFAdBottomView(bannerId: "21963")
bottomAd.delegate = bottomAdController
bottomAd.debugInfo("info")
bottomAdController.adBottomView = bottomAd
}
}
}
}
class BottomAdController: NSObject, ObservableObject, MFAdBottomViewDelegate {
var adBottomView: MFAdBottomView?
// 請求廣告
func requestAd() {
guard let adBottomView = adBottomView else { return }
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let window = windowScene.windows.first {
window.addSubview(adBottomView)
adBottomView.requestAd()
}
}
// MARK: - MFAdBottomViewDelegate
func requestAdSuccess() {
print("成功請求廣告")
adBottomView?.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("關閉廣告")
}
}