Quantcast
Channel: Active questions tagged react-native+ios - Stack Overflow
Viewing all articles
Browse latest Browse all 16750

Communication between React Native app and iOS widget

$
0
0

I'm trying to figure out how to communicate between my React Native app and an iOS widget. I need some insights on how to do so, this is completely new for me so.

I'm using React Native 0.61, XCode 11.1 and Swift 5.

I already have a bridge between my React Native app and a file named Bridge.swift. This file contains a variable string that needs to be displayed in my widget. But I really have no idea how to access that updated variable.

The Bridge.swift file is located in my main app while the TodayViewController is located in the widget target.

There is nothing wrong with the setup of my bridge, I checked that already.

I also tried doing Bridge.getStringToShow() in my TodayViewController but that is not possible.

Bridge.swift

import Foundation
import UIKit

@objc(Bridge)
class Bridge : NSObject {

  var stringToShow : String = String()

  @objc
  static func requiresMainQueueSetup() -> Bool {
    return true
  }

  @objc(strToShow:)
  func setStringToShow(strToShow: String) -> Void {
    stringToShow = strToShow
    print(stringToShow)
  }

  @objc
  func getStringToShow() -> String {
    return stringToShow
  }
}

TodayViewController.swift

import UIKit
import NotificationCenter

class TodayViewController: UIViewController, NCWidgetProviding {

  var bridge : Bridge = Bridge()

  override func viewDidLoad() {
    // Do any additional setup after loading the view.
    super.viewDidLoad()

    renderBackground(file: "background")

    loadTest()
  }

  @objc
  func loadTest() {
    let lblstr = createLabel(text: bridge.getStringToShow(), fontSize: 12)
    view.addSubview(lblstr)

    NSLayoutConstraint.activate([
      lblstr.centerXAnchor.constraint(equalTo: view.centerXAnchor),
      lblstr.centerYAnchor.constraint(equalTo: view.centerYAnchor)
    ])
  }
}

When I press a button in my React Native project, it sets a variable in that file to "test". This works and I can print it in my console. But I want to show that on my widget, which does not work. It does not show anything because it is empty.

I think it is because in the TodayViewController I initialise the variable when the string that needs to be displayed is empty, and it does not update that existing variable because it is another instance.

But how can I pass something from my React Native app to my widget then??


Viewing all articles
Browse latest Browse all 16750

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>