Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // PdfC.swift
- // MoSlaty
- //
- // Created by osama altmeme on 19/03/2021.
- //
- import UIKit
- import PDFKit
- import WebKit
- import GoogleMobileAds
- import Alamofire
- import NVActivityIndicatorView
- import MessageUI
- class PdfC: UIViewController,WKUIDelegate ,WKNavigationDelegate ,GADInterstitialDelegate , MFMailComposeViewControllerDelegate {
- @IBOutlet weak var BarBtn: UIBarButtonItem!
- let loading = NVActivityIndicatorView(frame: .zero, type: .lineSpinFadeLoader, color: #colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1), padding: 0)
- var fileNum = "\(ThePDFNum)"
- var didView = WKWebView()
- var MailLimit = true
- var mInterstitial: GADInterstitial!
- override func viewDidLoad() {
- super.viewDidLoad()
- //osama
- if (ThePDFNum == ""){
- showAdmobInterstitial()
- BarBtn.image = UIImage(systemName: "person.fill.questionmark")
- title = theName
- let myURL = URL(string:"\(ThepdfPage)")
- let myRequest = URLRequest(url: myURL!)
- didView.load(myRequest)
- didView.frame = CGRect(x: view.frame.origin.x, y: view.frame.origin.y, width: view.frame.size.width, height:view.frame.size.height)
- didView.navigationDelegate = self
- didView.backgroundColor = UIColor .clear
- didView.isOpaque = false
- view.addSubview(didView)
- startAnimation(start: "run")
- }else {
- let pdfView = PDFView(frame: view.frame)
- title = "\(theName)"
- if let url = Bundle.main.url(forResource: fileNum, withExtension: "pdf"),
- let pdfDocument = PDFDocument(url: url) {
- pdfView.displayMode = .singlePageContinuous
- pdfView.autoScales = true
- pdfView.displayDirection = .vertical
- pdfView.document = pdfDocument
- view.addSubview(pdfView)
- }
- }
- }
- @IBAction func ShareBTN(_ sender: Any) {
- if (ThePDFNum != "") {
- // This Code for Download PDF and Share
- startAnimation(start: "run")
- let pdfurl = "\(ThePDF)"
- // before downloading lets check if the file is already downloaded
- let url = URL(string: pdfurl)!
- var docPath = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
- docPath.appendPathComponent(url.lastPathComponent)
- if FileManager.default.fileExists(atPath: docPath.path) {
- let ActivityViewController = UIActivityViewController(activityItems: [URL(fileURLWithPath: docPath.path)], applicationActivities: nil)
- self.present(ActivityViewController, animated: true, completion: nil)
- if let popOver = ActivityViewController.popoverPresentationController {
- popOver.barButtonItem = self.BarBtn
- }
- startAnimation(start: "stop")
- } else {
- // downloading file
- let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
- AF.download(pdfurl, to: destination).response { (response) in
- if response.error != nil {
- self.startAnimation(start: "stop")
- print(response.error!)
- let alert = UIAlertController(title: nil, message: response.error!.localizedDescription, preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
- self.present(alert, animated: true, completion: nil)
- } else {
- // file is ready
- self.startAnimation(start: "stop")
- let ActivityViewController = UIActivityViewController(activityItems: [URL(fileURLWithPath: response.fileURL!.path)], applicationActivities: nil)
- self.present(ActivityViewController, animated: true, completion: nil)
- if let popOver = ActivityViewController.popoverPresentationController {
- popOver.barButtonItem = self.BarBtn
- }
- }
- }
- }
- }else {
- if (MailLimit == true) {
- MailLimit = false
- if MFMailComposeViewController.canSendMail() {
- let mail = MFMailComposeViewController()
- mail.mailComposeDelegate = self
- mail.setToRecipients(["arabfono143@hotmail.com"])
- mail.setSubject("\(theName)\(theNameHead) ابلغ عن خطأ")
- mail.setMessageBody("<p>اكتب هنا مشكلتك - اي رساله تكون فاضيه راح يتم تجاهلها</p>", isHTML: true)
- present(mail, animated: true)
- } else {
- // show failure alert
- }
- } else if (MailLimit == false){
- let alert = UIAlertController(title: "", message: "تم ابلاغ عن المشكله وسوف نقوم بالرد عليك قريبا على بريدك الالكتروني", preferredStyle: UIAlertController.Style.alert)
- self.present(alert, animated: true, completion: nil)
- alert.addAction(UIAlertAction(title: "حسنا", style: .default, handler: { action in
- switch action.style{
- case .default: break
- @unknown default: break
- }
- }))
- }
- }
- }
- //NVActivityIndicatorView add subbview and size
- fileprivate func startAnimation(start: String){
- loading.translatesAutoresizingMaskIntoConstraints = false
- view.addSubview(loading)
- NSLayoutConstraint.activate([
- loading.widthAnchor.constraint(equalToConstant: 150),
- loading.heightAnchor.constraint(equalToConstant: 150),
- loading.centerYAnchor.constraint(equalTo: view.centerYAnchor),
- loading.centerXAnchor.constraint(equalTo: view.centerXAnchor)
- ])
- if (start == "run"){
- loading.startAnimating()
- }
- if (start == "stop"){
- loading.stopAnimating()
- }
- }
- func webView(_ webView: WKWebView,
- didFinish navigation: WKNavigation!) {
- startAnimation(start: "stop")
- }
- func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
- controller.dismiss(animated: true)
- }
- // ADS Setp 2
- func showAdmobInterstitial()
- {
- let kGoogleFullScreenAppUnitID = "ca-app-pub-2534034482177387/8255100599";
- self.mInterstitial = GADInterstitial.init(adUnitID:kGoogleFullScreenAppUnitID )
- mInterstitial.delegate = self
- let Request = GADRequest()
- // Request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
- mInterstitial.load(Request)
- }
- // ADS Setp 3
- func interstitialDidReceiveAd(_ ad: GADInterstitial!)
- {
- if mInterstitial != nil {
- if mInterstitial!.isReady {
- mInterstitial?.present(fromRootViewController: self)
- print("Ad presented")
- } else {
- print("Ad was not ready for presentation")
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment