如何使用cocoapod为ios app和watch extension

时间:2015-12-07 21:06:42

标签: ios cocoapods watchkit realm watch-os-2

我尝试为我的ios app和ios手表套件使用一个框架(Realm.framework)。 我尝试了很多方法,但都没有。谁能给我一个如何编写pod文件以在ios app和watch app之间共享框架的例子?

在pod文件中没有任何手表扩展目标时,我收到错误消息:

Target 'Realm' of project 'Pods' was rejected as an implicit dependency for 'Realm.framework' because it doesn't contain platform 'watchsimulator' in its SUPPORTED_PLATFORMS 'iphonesimulator, iphoneos'
Target 'RealmSwift' of project 'Pods' was rejected as an implicit dependency for 'RealmSwift.framework' because it doesn't contain platform 'watchsimulator' in its SUPPORTED_PLATFORMS 'iphonesimulator, iphoneos'
Target 'Pods' of project 'Pods' was rejected as an implicit dependency for 'Pods.framework' because it doesn't contain platform 'watchsimulator' in its SUPPORTED_PLATFORMS 'iphonesimulator, iphoneos'

然后我将监视扩展的目标添加到我的pod文件中。这是我的pod文件:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
link_with 'myApp', 'myApp Watch Extension'

def shared_pods
  pod 'RealmSwift'
end

target 'myApp' do
    podspec :path => 'myapp.podspec'
    pod 'SnapKit'
    pod 'ChameleonFramework/Swift'
    pod 'Google-Mobile-Ads-SDK'
    shared_pods
end

target 'myApp Watch Extension' do
    podspec :path => 'myapp.podspec'
    platform :watchos, '2.0'
    shared_pods
end

我使用" pod install"发出了警告,但我的工作区无法运行。

2015-12-07 15:45:46.402 ruby[17042:4339468] warning:  The file reference for "Realm.framework" is a member of multiple groups ("Products" and "Products"); this indicates a malformed project.  Only the membership in one of the groups will be preserved (but membership in targets will be unaffected).  If you want a reference to the same file in more than one group, please add another reference to the same path.
2015-12-07 15:45:46.402 ruby[17042:4339468] warning:  The file reference for "RealmSwift.framework" is a member of multiple groups ("Products" and "Products"); this indicates a malformed project.  Only the membership in one of the groups will be preserved (but membership in targets will be unaffected).  If you want a reference to the same file in more than one group, please add another reference to the same path.

我的Pods-myApp Watch Extension-Realm文件中有很多错误。

我也尝试过pod文件:

source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
link_with 'myApp', 'myApp Watch Extension'

def shared_pods
  pod 'RealmSwift'
end

target 'myApp' do
    podspec :path => 'myapp.podspec'
    platform :ios, '8.0'
    pod 'SnapKit'
    pod 'ChameleonFramework/Swift'
    pod 'Google-Mobile-Ads-SDK'
    shared_pods
end

target 'myApp Watch Extension' do
    podspec :path => 'myapp.podspec'
    platform :watchos, '2.0'
    shared_pods
end

然后我得到了#![!]目标与不同的平台"错误。

在我的podspec中,我已添加了一行:

  s.platform     = :ios
  s.platform     = :ios, "8.0"
  s.platform     = :watchos
  s.platform     = :watchos, "2.0"

有人能告诉我应该怎么做吗?

1 个答案:

答案 0 :(得分:1)

在运行pod install之后,您看到的警告不应该出现,并且确实是CocoaPods / Xcodeproj中的错误。这似乎与UUID生成和您可能已经看到的警告有关:

[!] [Xcodeproj] Generated duplicate UUIDs:
…

Podfile中有两个目标特定的依赖关系定义组。 您不能将Podfile中的隐式根目标链接到您的应用及其扩展,因为它们位于不同的平台上。这意味着您必须删除第3 /第4行:

source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
#link_with 'myApp', 'myApp Watch Extension' # <= REMOVE this line.
…

我无法重现您在Xcode中看到的关于拒绝作为隐式依赖项的目标的错误。由于我没有你的podspec,我无法完全重现它,但从我看来,只要你的podspec没有声明任何依赖,它就不重要。

除此之外,只需在podspec中的那两行声明平台可用性就足够了:

s.platform     = :ios, "8.0"
s.platform     = :watchos, "2.0"