Search code examples
cocoapodswatchkitapple-watchwatchos-2

Working with Cocoapods + WatchOS 2 target


I have an iOS project with a lot of pods, around twenty. I want to integrate a watchOS 2 app in, but CocoaPods requires that the podspec contain support for watchOS (as seen here: http://blog.cocoapods.org/CocoaPods-0.38/)

At first, I thought I could fork all of the pods that aren't updated, point my podfile to those forked repos, and bob's your uncle. The problem is that some of the pods that I'm using are closed/not-public. Is there a way for me to not build the main application's pods for the watchOS target? Like using target isolation like so?:

target "Watch" do end

I can't seem to get that ^ potential solution to build, as it still tries to build the pods. I've also tried this repo, no luck: https://github.com/orta/cocoapods-expert-difficulty


Solution

  • There are two way to integrate pods using podfile with WathOS.

    1) Add Required pods directly to watch extension as below.

    target '<your watch Extension Name>' do
    
    platform :watchos, '2.0'
    pod 'RealmSwift'
    pod 'Alamofire'
    pod 'MMWormhole', '~> 2.0.0'
    
    end 
    

    2) Create Shared pods and add to both watch extension and iOS target both.

    def sharedPods
        pod 'RealmSwift'
        pod 'Alamofire'
    end
    
    target '<your watch Extension Name>' do
    platform :watchos, '2.0'
       sharedPods
    end
    
    
    target '<your iOSApp Name>' do
    platform :ios, '8.0'
       sharedPods
    end
    

    Add only watchOS and iOS supported pods in sharedPods, Do not add pods in sharedPods which does not support watchOS. e.g.

    def sharedPods
            pod 'RealmSwift'
            pod 'Alamofire'
            pod 'otherWatchOS&iOS supported Pod1'
            pod 'otherWatchOS&iOS supported Pod2'
        end
    

    Add only iOS supported pods in target '<your iOSApp Name>' e.g.

    target '<your iOSApp Name>' do
    platform :ios, '8.0'
       sharedPods
       pod 'otherOnlyiOS supported Pod1'
       pod 'otherOnlyiOS supported Pod2'
    end
    

    So, this way you can add required pods for required targets.