Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift iOS -Can't access my classes or notifications from within cocoapod's files

I downloaded 2 completely different pods, imported them into my project, used them inside one of my view controllers, and everything works fine if I choose to use either of them.

first pod

second pod

However if I try to access that same view controller from within one of the pod's files that same exact view controller is not recognized. I also created and tried to send a notification to the view controller but the notification gets no response (it works fine, I tried it from other classes I created). I then created a singleton underneath the pod file's class and then tried to access the singleton but nothing happened (print statements should run).

It's happened to 2 different pod files and they both work fine so I'm assuming there's another issue I'm overlooking that prevents outside files from working within the pods?

The pod works fine inside MyController

import ThePod

class MyController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(printSomethingInMyController(_:)), name: Notification.Name("printSomethingInMyController"), object: nil)

         // the pod works fine
        let podFile = FileWithinThePod()
    }

    @IBAction func buttonTapped(_ sender: UIButton) {

        // the pod does what it's supposed to do
        podFile.startSomeAction()
    }

    @objc fileprivate func printSomethingInMyController(_ notification: Notification) {
        print("notification- this should print in MyController")
    }

    static func printSomethingElse() {
        print("this a class print function")
    }
}

MyController is not accessible inside the pod file

open class FileWithinThePod {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }
    setUp() {
      // whatever this file needs
    }

    func startSomeAction() {

        // 0. the pod does something and it works fine

        // 1. ***THE PROBLEM IS HERE. I can't access MyController (photo below)
        MyController.printSomethingElse()

        // 2. ***THE PROBLEM IS ALSO HERE. This notification never fires because nothing ever prints
        NotificationCenter.default.post(name: Notification.Name("printSomethingInMyController"), object: nil, userInfo: nil)

        // 3. *** nothing happens with MySingleton because nothing ever prints
        MySingleton.startPrinting()

        // 4. *** same thing nothing prints
        let x = MySingleton.sharedInstance
        x.tryPrintingAgain()
    }
}

class MySingleton {

    static let sharedInstance = MySingleton()

    static func startPrinting() {

        print("print something from MySingleton")

        NotificationCenter.default.post(name: Notification.Name("printSomethingInMyController"), object: nil, userInfo: nil)
    }

    func tryPrintingAgain() {

        print("try again")

        NotificationCenter.default.post(name: Notification.Name("printSomethingInMyController"), object: nil, userInfo: nil)
    }
}

enter image description here

like image 939
Lance Samaria Avatar asked Dec 01 '25 05:12

Lance Samaria


1 Answers

It's a desired behaviour. The pod files (library) is not depended on your application target or your application classes. It doesn't know anything about your files or classes.

Your application depends on those library and those libraries not depended on your application. Editing a library like this way is not a good thing, because on next pod update these changes may go away.

Solution: Add the source files from the pod project to your application folder. Don't add them as a pod.

like image 149
Midhun MP Avatar answered Dec 03 '25 22:12

Midhun MP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!