How to Create a Cocoapod with .Swift

Custom cocoapod not finding swift file

Thanks to @balazs630, I was able to solve the problem. I had to make sure my access control was set to public or open in order to access them. Looking back at my code, ApplicationUtils was a public class and BaseClass had no designation, so it was by default internal, which according to the docs:

Internal access enables entities to be used within any source file
from their defining module, but not in any source file outside of that
module.

For anyone with a similar problem, check to make sure your access control is set appropriately. Here are some sources to familiarize yourself with access control in Swift:

  • https://medium.com/@abhimuralidharan/swift-3-0-1-access-control-9e71d641a56c
  • https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html

How to embedded Images and use those Images when creating custom cocoapod in iOS, swift 4

To get images in a CocoaPod you definitely need to call it specifying the bundle:

let bundle = Bundle(for: [A_CLASS_IN_YOUR_COCOAPOD].self)
let bundleURL = bundle.resourceURL?.URLByAppendingPathComponent("[YOUR_BUNDLE_NAME].bundle")
let resourceBundle = NSBundle(URL: bundleURL!)
UIImage(named: [IMAGE_NAME_STRING], in: resourceBundle, compatibleWith: nil)

[YOUR_BUNDLE_NAME] would be bottlenecLibrary in this case.

Source: How to load resource in cocoapods resource_bundle

How to make pod with Swift

I finally found the problem, my sample project have the same name with my pod TestSwift. After change the project name everything working fine.

Swift CocoaPod Library in Objective-C Project Migration from Swift 3 to 4/5

For anyone else that runs across this. The online resources I found were helpful but didn't detail this specific case.

  1. First off. Leave the CocoaPod code as it is. This will allow you to update it without having to worry about modifying it each time.

  2. Next create a Swift file within your Objective C Project. xCode will ask if you want a header generated for it, say yes. This file will be called -Swift.h.

  3. In this Swift file subclass the Swift file from the CocoaPod you are interested in accessing.

For example :

import Foundation
import HGCircularSlider

class CircularSliderObjc: RangeCircularSlider {
}

  1. Next add in the properties you wish to access with getter and setters.
    @objc override open var startPointValue: CGFloat {

get {
return super.startPointValue;
}

set {
super.startPointValue = newValue;
}
}

  1. Then finally change the import in your Objective-C file to your project's generated header file that I mentioned above (#import "-Swift.h"
    ). If you have a property pointing to the class in the CocoaPod change it to your new Swift Class. For example :
@property (weak, nonatomic) IBOutlet CircularSliderObjc *rangeSlider;

In this example I have it setup as an Outlet for InterfaceBuilder.

After that, you're done. It seems like a lot of work but it's quite easy and simple. Not quite as fast as just importing the header but since Swift 4 and 5 you can no longer just access open vars in Swift as properties in objc. They need the @objc declaration to make that work. Added security to the language I'm guessing.

Hope this helps someone.

adding a pod to a universal framework library using cocoapods

Here is my whole procedure

  1. crate a new swift app project.
  2. create a new cocoa touch framework target.
  3. in the project directory , run pod init
  4. Tyr This Podfile

    # Uncomment the next line to define a global platform for your project
    platform :ios, '9.0'

    target 'SOQ' do
    use_frameworks!
    end

    target 'SOQFW' do
    use_frameworks!
    end

    pod 'EVReflection'

after pod install, in both my two targets SOQ (swift app) and SOQFW (cocoa touch framework) can import EVReflection in *.swift and user class EVObject without a problem.

sample code is

import EVReflection

class User: EVObject {
var id: Int = 0
var name: String = ""
var friends: [User]? = []
}

you can give it a try.

My development environment is os x 10.12 , xode 8.2.1 , cocoapods 1.1.1

Skeleton Cocoapod Swift Project

The guide now appears to be up to date.
Guide

The trick is to create a swift framework (swift must be used in a framework cocoa pod).

Follow the pod init guide and set the path to your classes as required (.swift instead of .h|.m

Using cocoapods without use_frameworks! in Swift

Past

Up to CocoaPods 1.4.x (included), it was NOT possible to use CocoaPods with Swift code without use_frameworks!.

Present: 1.x.x and above

Nowadays, with CocoaPods 1.x.x (I verified it with 1.4.0), it's common to use use_frameworks! for both Swift and ObjC projects: it allows for a mix of the two languages in any way you want without issues:

  • You'll be able to use a Swift dependency in an Objective-C project.
  • You'll be able to use an Objective-C dependency in a Swift project.

Present: 1.5.x and above

Nowadays, CocoaPods 1.5.0 supports integrating swift pods as static libraries. Try it (sudo gem install cocoapods) and enjoy removing use_frameworks! from your Podfile.


Note that for iOS:

  • Apple requires Xcode 10.1 minimum, which is only well supported starting CocoaPods 1.6.0, so don't bother using older versions of CocoaPods.
  • Apple will require Xcode 11 minimum in April 2020, for which I would only use CocoaPods 1.7.5 or newer, together with xcodeproj 1.13.0 or newer.


Related Topics



Leave a reply



Submit