2016-05-10 38 views
16

Uygulamamın aşamalı olarak dönüştürülmesini React Native olarak gerçekleştiriyoruz. Ve iOS'ta Yerel İçerikte Bağımlılık Enjeksiyonu ile ilgili sorunları yaşamaya devam ediyorum.React Native modüllerindeki Bağımlılık Enjeksiyonu

Uygulamamda bazı hizmetlerim var, yerel bir modülde kullanmak istiyorum. Şu anda, Typhoon aracılığıyla enjekte edilir ve her şey iyi çalışır. Ancak, yerel olarak kendisini yeniden başlatır ve yerel modülü tekil olarak korur. Bu, Typhoon'un onları başlatmasına izin vermemi engelliyor, ve ben de onlara bağımlılıkları enjekte edemem.

Ne yapılabilir? RCTBridge'i kendim oluşturmak bir seçenektir, ancak çok düşük seviyededir ve yine de ilk etapta UIView'e nasıl enjekte edileceğini bulmaya ihtiyaç duyar.

cevap

10

Sorunun neden daha fazla oy almadığından tam olarak emin değilim; Ben de aynı soruyu cevaplamak için uğraşıyordum ve ilk StackOverflow sorusuna cevap vermem gerektiğini düşündüm.

Cevabı RCTBridge class çevirin. Ne yapmanız gereken RCTBridgeProtocol (ve önemlisi yöntemi 'extraModulesForBridge' uygulayan bir sınıf örneğine ile elle RCTBridge initialize olduğu; eğer istedim hatta View Controller içinde bu protokolü uygulamak

// Initialise a class that implements RCTBridgeDelegate 
// Be warned, RCTBridge won't store a strong reference to your delegate. 
// You should there store this delegate as a property of your initialising class, or implement the protocol in the View Controller itself. 
id<RCTBridgeDelegate> moduleInitialiser = [[classThatImplementsRCTBridgeDelegate alloc] init]; 

// Create a RCTBridge instance 
// (you may store this in a static context if you wish to use with other RCTViews you might initialise. 
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:moduleInitialiser launchOptions:nil]; 

// Initialise an RCTRootView 
RCTRootView *rootView = [[RCTRootView alloc] 
        initWithBridge:bridge 
         moduleName:kModuleName 
        initialProperties:nil]; 

// Note that your class that implements RCTBridgeDelegate SHOULD implement the following methods and it might look something like this. 

// This will return the location of our Bundle 
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge 
{ 
    return [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios"]; 
} 

// Importantly, this is the method we need to implement to answer this question 
// We must return an array of initialised Modules 
// Be warned, I am writing this off of the top of my head, so excuse any syntax errors there might be! 
- (NSArray *)extraModulesForBridge:(RCTBridge *)bridge 
{ 
    return @[[OurNativeModule alloc] initWithCustomInitialiser:customDependency]]; 
} 
.

Düzenleme: tepki-yerli belgelere bu katma https://facebook.github.io/react-native/docs/native-modules-ios.html#dependency-injection

+0

//Make sure to hold the strong reference to the moduleInitaliser self.moduleInitialiser = RNModuleInitialiser() self.bridge = RCTBridge(delegate: self.moduleInitialiser, launchOptions: nil) 
. Bununla birlikte, bunu tekrar gözden geçirebilirim ve sadece tesisat için C nesnesini kullanabilir ve gerçek yerel modülü hızlıca yazabilirim. Çalıştırdın mı? – Rubys

+0

DatePickerIOS gibi standart yerel modülleri kendi uygulamamla değiştirerek, seçici renkleri değiştirmek için özel API gibi bir şey kullanan herhangi bir yetenek var mı? – pinguinjkeke

2

yukarıdaki kod sadece çalışıyor İşte kod Swift 4 versiyonu köprü başlatırken

@objc(RNModuleInitialiser) 
final class RNModuleInitialiser: NSObject { 

    //Inject your dependencies here 
    init() { 

    } 

} 

extension RNModuleInitialiser: RCTBridgeDelegate { 

    func sourceURL(for bridge: RCTBridge!) -> URL! { 
     return URL(string: "http://localhost:8081/index.ios.bundle?platform=ios")! 
    } 

    func extraModules(for bridge: RCTBridge!) -> [RCTBridgeModule]! { 

     var extraModules = [RCTBridgeModule]() 

     //Initialise the modules here using the dependencies injected above 

     return extraModules 
    } 

} 

, m geçmektedir... oduleInitialiser:

Aslında kendi başıma keşfettim, ama Swift kullanırken bizim tüm uygulama dayansın Swift olduğundan, RCTModules oluşturmak için iyi bir yol (Ben bir süre önce oldu, RCTModule olduğunu düşünüyorum) bulamadık