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
How to Add a Watermark to an Image Using This Code
How to Convert Delegate to Observable Rxswift
How to Make a Https Request to a Server in Swift
Converting .M4A File to .Aiff Using Audioconverter Swift
How to Convert a Swift String to Cfstring
Swift Codable Decode Manually Optional Variable
Can a Swift Property Wrapper Reference the Owner of the Property Its Wrapping
Binary Operator '+' Cannot Be Applied to Two 'T' Operands
Rxswift/Rxcocoa: Prevent Uitextfield from Having More Than ... Characters
How to Use Image Literal in Xcode 13
Adding Uibarbutton Item in Swift
What Are Convenience Required Initializers in Swift
Do You Need to Release Cgcontextref in Swift
Wkwebview Notification When View Is Actually Shown
What Is the Meaning of "" in Swift
How to Fix 'Line Length Violation: Line Should Be 120 Characters or Less' - Swiftlint
How to Provide Default Implementations for Uipageviewcontrollerdatasource