objective-c
  • configuration-management
  • cocoapods
  • xcconfig
  • 2013-03-27 76 views 3 likes 
    3

    我開發了一個iOS項目,它是一個處理不同服務器的類庫。每個使用該庫的應用程序只需要一臺服務器。服務器類型可以在編譯時通過預處理器定義進行配置。如何在Cocoapod子規格中定義不同的xcconfig參數?

    在我的圖書館的podspec,我定義的各種subspecs像這樣每個服務器:

    s.name = "ServerLib" 
    [...] 
    s.subspec 'ServerA' do |a| 
        a.source_files = 'Classes/A/**/*.{h,m}' 
        a.xcconfig = { "GCC_PREPROCESSOR_DEFINITIONS" => "$(inherited) ServerA=1" } 
    end 
    
    s.subspec 'ServerB' do |b| 
        b.source_files = 'Classes/B/**/*.{h,m}' 
        b.xcconfig = { "GCC_PREPROCESSOR_DEFINITIONS" => "$(inherited) ServerB=1" } 
    end 
    

    我的應用程序是一個多用戶的應用程序,每個客戶的一個目標。每個客戶都使用庫項目中的特定服務器。所以,我Podfile看起來是這樣的:

    platform :ios, '5.0' 
    
    pod 'MyCore' 
    pod '3rdPartyLib' 
    
    target :'Customer1', :exclusive => true do 
        pod 'ServerLib/ServerA' 
    end 
    
    target :'Customer2', :exclusive => true do 
        pod 'ServerLib/ServerB' 
    end 
    

    什麼pod install腳本執行,被合併在subspecs定義成一個值在每莢customerN.xcconfig所有的標誌文件

    GCC_PREPROCESSOR_DEFINITIONS = $(inherited) 3RD_PARTY_FLAGS $(inherited) ServerA=1 $(inherited) ServerB=1 
    

    任何建議如何爲了規避Cocoapods的這種錯誤(?)行爲?據我瞭解的文檔,subspec屬性應該只從其父級規格而不是同級子規格繼承。

    +0

    你有沒有找到這個解決方案? – epologee 2013-08-27 17:04:46

    回答

    2

    找到一種解決方法,也許不是優雅的:

    由於pod install合併所有的編譯器標誌爲一個,我只好從圖書館的podspec文件中刪除GCC_PREPROCESSOR_DEFINITIONS。但是沒有這個定義,我的庫不會構建。

    在Xcode中,通過將定義添加到每個庫的目標,可以很容易地解決這個問題。但是,當我在應用程序中使用我的庫時,Pods項目將從不包含所需標誌的Podspec生成。

    解決方案是在應用程序的Podfile中使用post_install鉤子來操縱Pods項目生成的xcconfig。

    post_install do |installer| 
    
        file_names = ['./Pods/Pods-ServerA.xcconfig', 
            './Pods/Pods-ServerB.xcconfig'] 
    
        # rename existing pre-processor definitions 
        file_names.each do |file_name| 
         text = File.read(file_name) 
         File.open(file_name, 'w') { |f| f.puts text.gsub(/GCC_PREPROCESSOR_DEFINITIONS/, "GCC_PREPROCESSOR_DEFINITIONS_SHARED")} 
        end 
    
        # merge existing and required definition for ServerA 
        File.open('./Pods/Pods-ServerA.xcconfig', 'a') { |f| 
         f.puts "\nGCC_PREPROCESSOR_DEFINITIONS=$(GCC_PREPROCESSOR_DEFINITIONS_SHARED) ServerA=1" 
        } 
    
        # merge existing and required definition for ServerB 
        File.open('./Pods/Pods-ServerB.xcconfig', 'b') { |f| 
         f.puts "\nGCC_PREPROCESSOR_DEFINITIONS=$(GCC_PREPROCESSOR_DEFINITIONS_SHARED) ServerB=1" 
        } 
    
    end 
    

    該代碼有點冗長,因爲我不熟悉Ruby,但它的工作原理。只要變量和庫遵循命名方案,應該很容易自動執行這個重命名追加過程。

    相關問題