Tag: IOS

How to add Google Admob banner to the first row in UItableview, Swift IOS

step 1: create file AdCell.swift


import UIKit
import GoogleMobileAds

class AdCell: UITableViewCell {
    override func awakeFromNib() {
        super.awakeFromNib()
        self.backgroundColor = .clear
    }
    
    class func cellBannerView(rootVC: UIViewController, frame: CGRect) -> GADBannerView {
           let bannerView = GADBannerView()
           bannerView.frame = frame
           bannerView.rootViewController = rootVC
           bannerView.adUnitID = "ca-app-pub-3940256099942544/6300978111"
        bannerView.adSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(frame.size.width)
           return bannerView
       }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}

step 2: use the Adcell class:


 let bannerViewHeight = GADBannerView(adSize: kGADAdSizeBanner).frame.size.height



 override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
             return bannerViewHeight + 15
    }

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
             let cell = AdCell(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: bannerViewHeight))                     
             let bannerView = AdCell.cellBannerView(rootVC: self, frame:cell.bounds)
             bannerView.translatesAutoresizingMaskIntoConstraints = false
             cell.addSubview(bannerView)
                                
             bannerView.centerXAnchor.constraint(equalTo: cell.centerXAnchor).isActive = true
             bannerView.centerYAnchor.constraint(equalTo: cell.centerYAnchor, constant: -3).isActive = true

             let request = GADRequest()
             bannerView.load(request)

             return cell
        }

{ Add a Comment }

How to show an Alert message when there is no internet connection, swift code

Use this code to show an alert to the user when there is no internet connection. SWIFT IOS code

import SystemConfiguration

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if isInternetAvailable() {
            observeUsers()
        } else {
            let alert = UIAlertController(title: "No Connection", message: "No internet connection, connect to the internet and try again.", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "ok", style: .default, handler: nil))
            present(alert, animated: true, completion: nil)
            
            return
        }
        
    }



  func isInternetAvailable() -> Bool {
        var zeroAddress = sockaddr_in()
        zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
        zeroAddress.sin_family = sa_family_t(AF_INET)
        
        let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
            $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
                SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
            }
        }
        
        var flags = SCNetworkReachabilityFlags()
        if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
            return false
        }
        let isReachable = flags.contains(.reachable)
        let needsConnection = flags.contains(.connectionRequired)
        return (isReachable && !needsConnection)
    }


{ Add a Comment }