2015-10-19 44 views
13

我正在構建一個React本地用戶界面組件,它將利用Google Maps iOS SDK在React應用中呈現地圖。這是作爲一個靜態的Cocoa Touch框架構建的,以便我可以在不同的項目中使用它。在React Native自定義用戶界面組件中使用CocoaPods模塊

到目前爲止,這個框架並沒有太多的工作,我只是試圖在我嘗試和做任何有用的事情之前去編譯它。我有它加載在谷歌地圖SDK一個Podfile,我已經運行pod install命令:

# Uncomment this line to define a global platform for your project 
platform :ios, '8.1' 
# Uncomment this line if you're using Swift 
# use_frameworks! 

target 'GoogleMapView' do 
    source 'https://github.com/CocoaPods/Specs.git' 
    pod 'GoogleMaps' 
end 

我有GoogleMapView.hGoogleMapView.m文件,這將做到這一點模塊中繁重。現在他們並不真正做太多:

@import GoogleMaps; 

@interface GoogleMapView: GMSMapView 

@end 

-

#import "GoogleMapView.h" 

@implementation GoogleMapView { 
    GMSMapView *mapView_; 
} 

- (void)viewDidLoad { 
    // Create a GMSCameraPosition that tells the map to display the 
    // coordinate -33.86,151.20 at zoom level 6. 
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86 
                  longitude:151.20 
                   zoom:6]; 
    mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; 
    mapView_.myLocationEnabled = YES; 
    self.view = mapView_; 

    // Creates a marker in the center of the map. 
    GMSMarker *marker = [[GMSMarker alloc] init]; 
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20); 
    marker.title = @"Sydney"; 
    marker.snippet = @"Australia"; 
    marker.map = mapView_; 
} 

@end 

然後,我有GoogleMapViewManager.hGoogleMapViewManager.m文件,這提供了橋樑反應原住民。同樣,這些不會做太多,現在!:

#import "RCTViewManager.h" 

@interface GoogleMapViewManager : RCTViewManager 

@end 

-

#import "GoogleMapView.h" 
#import "GoogleMapViewManager.h" 

@implementation GoogleMapViewManager 

RCT_EXPORT_MODULE() 

- (UIView *)view 
{ 
    GoogleMapView *map = [[GoogleMapView alloc] init]; 

    return map; 
} 

@end 

我已經添加了這個庫,以我的母語做出反應的XCode項目,像這樣 - 有列出了一些紅色的文件(我也不太清楚他們的意思):

Xcode File Tree

我還添加了產品從我的靜態庫的Link Binary With Libraries列表中主要陣營項目部分:

Build Phases

然而,當我嘗試編譯項目時,我得到了一些錯誤,這是導致構建失敗,像這樣:

Build Failure

我確定我在使用CocoaPods導入Google Maps SDK時做了一些錯誤。當我將靜態庫導入React應用程序時,我無法關注文檔並使用.xcworkspace文件,這可能是什麼問題,但我無法弄清楚如何讓它運行!

更新:

我可以得到代碼編譯,如果我包括使用.xcworkspace文件地圖項目,但當時我不能夠訪問的二進制文件,或將它們任意構建階段,其ISN 「T有用:

Use .xcworspace file instead

有誰知道我怎麼能利用谷歌地圖iOS版SDK的這樣一個陣營原生應用程序?

+0

不知道如何在庫項目中使用CocoaPods,但可以直接在您的原始React項目中包含那些與Google地圖相關的類(如果您不打算共享該庫) –

+0

這就是我不得不恢復到這樣做 - 如果您需要更新React版本,則會導致嚴重的問題,因爲#react-native upgrade命令會使XCode項目變得混亂。 – edcs

+0

我認爲最簡單的方法是通過CocoaPods安裝react-native並將你的依賴添加爲另一個Pod。 – Adamski

回答

2

在你有一個陣營原生應用項目的情況下,我們稱之爲MyApp.xcodeproj,並且要包括庫項目,GoogleMapView.xcodeproj,你將無法使用GoogleMapView的CocoaPods只需將它拖動到MyApp.xcodeproj你在這個例子中已經完成了。

如果您想使用CocoaPods來拉動GoogleMaps Pod,您必須使用CocoaPods吸引您的GoogleMapView項目。

像這樣:

# MyApp/Podfile 

pod 'GoogleMapView', :path => 'Libraries/GoogleMapView' 

但你這樣做之前,你需要確保該項目GoogleMapView /私有CocoaPod配置是否正確。

首先,Podfile:

# MyApp/Libraries/GoogleMapView/Podfile 

pod 'GoogleMaps' 

然後,Podspec。這個文件將項目標記爲CocoaPods庫。

您可以使用pod spec create GoogleMapView生成此項。

這裏就是你需要看起來像:

# MyApp/Libraries/GoogleMapView/GoogleMapView.podspec 

    Pod::Spec.new do |s| 

     s.name   = "GoogleMapView" 
     s.version  = "0.0.1" 
     s.summary  = "A short description of GoogleMapView." 

     s.description = <<-DESC 
         DESC 

     s.homepage  = "http://EXAMPLE/GoogleMapView" 
     s.license  = "MIT (example)" 


     s.author    = { "David Young-Chan Kay" => "[email protected]" } 

     s.source  = { :git => "http://EXAMPLE/GoogleMapView.git", :tag => "0.0.1" } 

     s.source_files = "Classes", "Classes/**/*.{h,m}" 
     s.exclude_files = "Classes/Exclude" 

     # This is the key line. You must add it by hand. 
     s.dependency 'GoogleMaps' 

    end 

一旦這三個文件是在地方,你將能夠重複使用您的GoogleMapView使用的CocoaPods!

你甚至可以將它上傳到中央CocoaPods存儲庫以便與他人共享。

希望這會有所幫助。讓我知道你是否需要澄清。

+0

這看起來不錯 - 謝謝你的答案。當我有一兩分鐘的空餘時間時,我會盡力實施它:) – edcs

+0

祝你好運!讓我知道事情的後續。 :) –

+0

可能是anwser是不正確的。因爲在GoogleMapView中我們將使用react-native。但是在pod開發中,沒有反應原生。 – shawnXiao

相關問題