BandarHelal

Untitled

Mar 29th, 2021 (edited)
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 7.74 KB | None | 0 0
  1. //
  2. //  PdfC.swift
  3. //  MoSlaty
  4. //
  5. //  Created by osama altmeme on 19/03/2021.
  6. //
  7.  
  8. import UIKit
  9. import PDFKit
  10. import WebKit
  11. import GoogleMobileAds
  12. import Alamofire
  13. import NVActivityIndicatorView
  14. import MessageUI
  15.  
  16.  
  17. class PdfC: UIViewController,WKUIDelegate ,WKNavigationDelegate ,GADInterstitialDelegate , MFMailComposeViewControllerDelegate  {
  18.     @IBOutlet weak var BarBtn: UIBarButtonItem!
  19.    
  20.     let loading = NVActivityIndicatorView(frame: .zero, type: .lineSpinFadeLoader, color: #colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1), padding: 0)
  21.  
  22.     var fileNum = "\(ThePDFNum)"
  23.     var didView = WKWebView()
  24.     var MailLimit = true
  25.     var mInterstitial: GADInterstitial!
  26.  
  27.  
  28.     override func viewDidLoad() {
  29.         super.viewDidLoad()
  30.  
  31.         //osama
  32.         if (ThePDFNum == ""){
  33.             showAdmobInterstitial()
  34.             BarBtn.image = UIImage(systemName: "person.fill.questionmark")
  35.             title =  theName
  36.  
  37.             let myURL = URL(string:"\(ThepdfPage)")
  38.                let myRequest = URLRequest(url: myURL!)
  39.             didView.load(myRequest)
  40.             didView.frame = CGRect(x: view.frame.origin.x, y: view.frame.origin.y, width: view.frame.size.width, height:view.frame.size.height)
  41.             didView.navigationDelegate = self
  42.             didView.backgroundColor = UIColor .clear
  43.             didView.isOpaque = false
  44.             view.addSubview(didView)
  45.             startAnimation(start: "run")
  46.            
  47.         }else {
  48.            
  49.  
  50.            
  51.  
  52.             let pdfView = PDFView(frame: view.frame)
  53.              
  54.              title = "\(theName)"
  55.              
  56.              if let url = Bundle.main.url(forResource: fileNum, withExtension: "pdf"),
  57.                  let pdfDocument = PDFDocument(url: url) {
  58.                  pdfView.displayMode = .singlePageContinuous
  59.                  pdfView.autoScales = true
  60.                  pdfView.displayDirection = .vertical
  61.                  pdfView.document = pdfDocument
  62.                  
  63.                  view.addSubview(pdfView)
  64.              }
  65.  
  66.  
  67.            
  68.         }
  69.  
  70.        
  71.        
  72.     }
  73.    
  74.  
  75.     @IBAction func ShareBTN(_ sender: Any) {
  76.        
  77.        
  78.         if (ThePDFNum != "") {
  79.             // This Code for Download PDF and Share
  80.             startAnimation(start: "run")
  81.  
  82.             let pdfurl = "\(ThePDF)"
  83.             // before downloading lets check if the file is already downloaded
  84.             let url = URL(string: pdfurl)!
  85.             var docPath = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
  86.             docPath.appendPathComponent(url.lastPathComponent)
  87.             if FileManager.default.fileExists(atPath: docPath.path) {
  88.                 let ActivityViewController = UIActivityViewController(activityItems: [URL(fileURLWithPath: docPath.path)], applicationActivities: nil)
  89.                 self.present(ActivityViewController, animated: true, completion: nil)
  90.                 if let popOver = ActivityViewController.popoverPresentationController {
  91.                     popOver.barButtonItem = self.BarBtn
  92.                 }
  93.                startAnimation(start: "stop")
  94.             } else {
  95.                 // downloading file
  96.                 let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
  97.                 AF.download(pdfurl, to: destination).response { (response) in
  98.                     if response.error != nil {
  99.                         self.startAnimation(start: "stop")
  100.                         print(response.error!)
  101.                         let alert = UIAlertController(title: nil, message: response.error!.localizedDescription, preferredStyle: .alert)
  102.                         alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
  103.                         self.present(alert, animated: true, completion: nil)
  104.                     } else {
  105.                         // file is ready
  106.                        self.startAnimation(start: "stop")
  107.                         let ActivityViewController = UIActivityViewController(activityItems: [URL(fileURLWithPath: response.fileURL!.path)], applicationActivities: nil)
  108.                         self.present(ActivityViewController, animated: true, completion: nil)
  109.                         if let popOver = ActivityViewController.popoverPresentationController {
  110.                             popOver.barButtonItem = self.BarBtn
  111.                         }
  112.                     }
  113.                 }
  114.             }
  115.            
  116.        
  117.     }else {
  118.  
  119.         if (MailLimit == true) {
  120.        
  121.             MailLimit = false
  122.         if MFMailComposeViewController.canSendMail() {
  123.             let mail = MFMailComposeViewController()
  124.             mail.mailComposeDelegate = self
  125.             mail.setToRecipients(["arabfono143@hotmail.com"])
  126.             mail.setSubject("\(theName)\(theNameHead) ابلغ عن خطأ")
  127.             mail.setMessageBody("<p>اكتب هنا مشكلتك - اي رساله تكون فاضيه راح يتم تجاهلها</p>", isHTML: true)
  128.  
  129.             present(mail, animated: true)
  130.         } else {
  131.             // show failure alert
  132.         }
  133.         } else if (MailLimit == false){
  134.        
  135.             let alert = UIAlertController(title: "", message: "تم ابلاغ عن المشكله وسوف نقوم بالرد عليك قريبا على بريدك الالكتروني", preferredStyle: UIAlertController.Style.alert)
  136.             self.present(alert, animated: true, completion: nil)
  137.             alert.addAction(UIAlertAction(title: "حسنا", style: .default, handler: { action in
  138.                 switch action.style{
  139.                 case .default: break
  140.                    
  141.  
  142.                 @unknown default: break
  143.                    
  144.                 }
  145.             }))
  146.        
  147.         }
  148.  
  149.  
  150.  
  151.     }
  152.    
  153.    
  154.    
  155.    
  156.    
  157.    
  158.     }
  159.    
  160.    
  161.    
  162.     //NVActivityIndicatorView add subbview and size
  163.  
  164.     fileprivate func startAnimation(start: String){
  165.         loading.translatesAutoresizingMaskIntoConstraints = false
  166.         view.addSubview(loading)
  167.         NSLayoutConstraint.activate([
  168.             loading.widthAnchor.constraint(equalToConstant: 150),
  169.             loading.heightAnchor.constraint(equalToConstant: 150),
  170.             loading.centerYAnchor.constraint(equalTo: view.centerYAnchor),
  171.             loading.centerXAnchor.constraint(equalTo: view.centerXAnchor)
  172.        
  173.         ])
  174.        
  175.         if (start == "run"){
  176.            loading.startAnimating()
  177.            
  178.         }
  179.        
  180.         if (start == "stop"){
  181.             loading.stopAnimating()
  182.         }
  183.  
  184.     }
  185.    
  186.    
  187.     func webView(_ webView: WKWebView,
  188.        didFinish navigation: WKNavigation!) {
  189.        startAnimation(start: "stop")
  190.        
  191.      }
  192.     func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
  193.         controller.dismiss(animated: true)
  194.     }
  195.    
  196.     // ADS Setp 2
  197.  
  198.     func  showAdmobInterstitial()
  199.     {
  200.         let kGoogleFullScreenAppUnitID = "ca-app-pub-2534034482177387/8255100599";
  201.         self.mInterstitial = GADInterstitial.init(adUnitID:kGoogleFullScreenAppUnitID )
  202.        
  203.         mInterstitial.delegate = self
  204.         let Request  = GADRequest()
  205.         // Request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
  206.         mInterstitial.load(Request)
  207.        
  208.     }
  209.    
  210.    
  211.    
  212.     // ADS Setp 3
  213.  
  214.     func interstitialDidReceiveAd(_ ad: GADInterstitial!)
  215.     {
  216.         if mInterstitial != nil {
  217.             if mInterstitial!.isReady {
  218.                 mInterstitial?.present(fromRootViewController: self)
  219.                 print("Ad presented")
  220.             } else {
  221.                 print("Ad was not ready for presentation")
  222.             }
  223.         }
  224.  
  225. }
  226. }
  227.  
Add Comment
Please, Sign In to add comment