Search code examples
swiftcocoapods

I can't import classes from a newly created cocoa pod


I have just created and published two new public cocoa pods from a single git repository (https://github.com/snakajima/bonjour-http).

Here is the Podspec of the second pod.

Pod::Spec.new do |s|
  s.name             = 'bonjour-http-server'
  s.version          = '0.4.0'
  s.summary          = 'HTTP over Bonjour in Swift.'
 
  s.description      = <<-DESC
  HTTP over Bonjour in Swift for iOS and macOS.
                       DESC
 
  s.homepage         = 'https://github.com/snakajima/bonjour-http'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'Satoshi Nakajima' => '[email protected]' }
  s.source           = { :git => 'https://github.com/snakajima/bonjour-http.git', :tag => s.version.to_s }
 
  s.ios.deployment_target = '13.0'
  s.osx.deployment_target = '10.14'
  s.source_files = 'core/BonjourService.swift', 'core/BonjourRequest.swift', 'core/BonjourResponse.swift', 'core/BonjourParser.swift'
  s.swift_versions = '5.0'
  s.dependency 'CocoaAsyncSocket'
end

Now, I am trying to use the second pod (bonjour-http-server) from another project. I added the following line to the Podfile and ran "pod install", which successfully added it to the project.

  pod 'bonjour-http-server', :git => 'https://github.com/snakajima/bonjour-http.git'

When I try to import this module from a swift file, Xcode will code-complete like this.

import bonjour_http_server

However, I am not be able to use any of classes in this module, such as BonjourService. Xcode won't code-complete it and the compiler fails.

I am new to Cocoa Pods and it is quite possible that I am making a mistake somewhere - either in the publishing phase or importing phase.

I'd really appreciate if somebody could help me to solve this problem. The entire source code is available at https://github.com/snakajima/bonjour-http.


Solution

  • Based on Access Control official documentation:

    All entities in your code (with a few specific exceptions, as described later in this chapter) have a default access level of internal if you don’t specify an explicit access level yourself. As a result, in many cases you don’t need to specify an explicit access level in your code.

    If you declare a class like this: (example from your repo)

    @objc class BonjourService: NSObject {
        ...
    }
    

    by default can be accessed only from within the same module.

    So, to expose a class, a function etc to the outside world, (outside of your module) you need to at least declare it as public:

    @objc public class BonjourService: NSObject {
        ...
    }
    

    You can find more details regarding Swift's Access Levels in the aforementioned documentation.