How to Continue Ble Activities Onto Next View Controller

How to continue BLE activities onto next view controller

Put your BLE related code into a centralized place, e.g. BLEMaganer (better) or AppDelegate (so so). So that controllerA and controllerB can share the same centrolManager instance.

For example, currently you have a property centralManager in controllerA, and implement its delegate in controllerA. You access centralManager by controllerA.centralManager.

Move the centralManager property to AppDelegate, as well other related code. Then you can access centrolManager instance by

(UIApplication.sharedApplication().delegate as! AppDelegate).centralManager.

BLE Peripheral disconnects when navigating to different ViewController

Create a Singleton class and add bleManager and peripheral properties there:

class Shared { 
private init(){ }
static let instance = Shared()
var bleManager: BLEManager!
var peripheral: CBPeripheral!
}

And you can access the same instance through different controllers:

Shared.instance.bleManager = BLEManager() 

How to access bluetooth data across view controllers? in xcode

You could possibly create a global class that can become the central manager property?

See this answer how to do that: https://stackoverflow.com/a/6067515/1331332

You could then use NSNotificationCenter to send the data throughout your app, just set up the observers in each ViewController

Passing connected ble peripheral using segue in swift 4?

When you need this object throughout the app (i.e. as a data service) you might want to consider creating a service class that holds this peripheral. Using the singleton pattern it is possible just have to keep in mind that singletons stay in memory until your app exits. If this object is meant to be cleared at some point then singleton might not be your best bet, but if you're planning on keeping it anyway, then go like this:

class BTService{
static let instance = BTService()
//set up your peripheral here you can have a setup function that you call early in your app}

The setup funciton you can call in the first viewDidLoad or even in applicationFInishedLaunching in appDelegate. Then on any viewcontroller you refer to this peripheral as BTService.instance.peripheral or whatever you call it, also the setup function you can access through the instance.setupBT.

Swift IOS keep view controller running in background after pop

A popped view controller does not "stop running". It is returned to you, and if you don't retain it, it is completely destroyed.

If you don't want that to happen, retain it when it is returned. You are currently ignoring the returned view controller:

 self.navigationController?.popViewControllerAnimated(true)

Instead, keep a reference to it:

self.mySecondViewController = 
self.navigationController?.popViewControllerAnimated(true)

Be warned, however, that this is a very unusual architecture. You will not be able to use the storyboard segue to push again, because it will push a different copy. It would be better to abandon your navigation controller architecture entirely, as it is completely unsuited to the idea of a view controller persisting after it is popped. If you want an architecture where two view controllers persist simultaneously, you would be better off using a UITabBarController — or, even better, reorganize your app completely. The notion that you need the view controller to persist after being popped is a "bad smell": it means that you have put the functionality in the wrong place. Put the functionality in a place that does persist, rather than forcing the view controller to persist in some artificial way.



Related Topics



Leave a reply



Submit