Skip to content

Commit b6fe65e

Browse files
[Amazon] StopConnect for UDC in iOS tv-casting-app
Updated iOS tv-casting-app to send stopConnect command when user exits UDC before confirming passcode Changes 1. MCConnectionExampleViewModel track if user confirmed passcode or not 2. MCConnectionExampleView trigger stopConnect when Connecting view dissapear Test 1. Verified stopConnect works for normal UDC and commissioner generated passcode UDC 2. Verified alternating attempt of normal UDC and commissioner generated passcode UDC 3. Verified stopConnect is not called in other states other than pending passcode confirmation
1 parent 992e6fe commit b6fe65e

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

examples/tv-casting-app/darwin/TvCasting/TvCasting/MCConnectionExampleView.swift

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ struct MCConnectionExampleView: View {
8888
viewModel.connect(selectedCastingPlayer: self.selectedCastingPlayer, useCommissionerGeneratedPasscode: self.useCommissionerGeneratedPasscode)
8989
}
9090
})
91+
.onDisappear(perform: {
92+
viewModel.cancelConnectionAttempt(selectedCastingPlayer: self.selectedCastingPlayer)
93+
})
9194
}
9295
}
9396

examples/tv-casting-app/darwin/TvCasting/TvCasting/MCConnectionExampleViewModel.swift

+28-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class MCConnectionExampleViewModel: ObservableObject {
2525

2626
var passcodeAlertController: UIAlertController?
2727

28+
var pendingPasscodeConfirmation: Bool = true;
29+
2830
// VendorId of the MCEndpoint on the MCCastingPlayer that the MCCastingApp desires to interact with after connection
2931
let kDesiredEndpointVendorId: UInt16 = 65521;
3032

@@ -42,13 +44,33 @@ class MCConnectionExampleViewModel: ObservableObject {
4244

4345
@Published var errorCodeDescription: String?
4446

47+
func cancelConnectionAttempt(selectedCastingPlayer: MCCastingPlayer?) {
48+
DispatchQueue.main.async {
49+
// Only stop connection if we are pending passcode confirmation
50+
// We cannot stop connection once continueConnecting() is called
51+
if self.pendingPasscodeConfirmation {
52+
self.Log.info("MCConnectionExampleViewModel cancelConnect(). User navigating back from ConnectionView")
53+
let err = selectedCastingPlayer?.stopConnecting()
54+
if err == nil {
55+
self.pendingPasscodeConfirmation = false;
56+
self.connectionStatus = "User cancelled the connection attempt with CastingPlayer.stopConnecting()."
57+
self.Log.info("MCConnectionExampleViewModel cancelConnect() MCCastingPlayer.stopConnecting() succeeded.")
58+
} else {
59+
self.connectionStatus = "Cancel connection failed due to: \(String(describing: err))."
60+
self.Log.error("MCConnectionExampleViewModel cancelConnect() MCCastingPlayer.stopConnecting() failed due to: \(err)")
61+
}
62+
}
63+
}
64+
}
65+
4566
func connect(selectedCastingPlayer: MCCastingPlayer?, useCommissionerGeneratedPasscode: Bool) {
4667
self.Log.info("MCConnectionExampleViewModel.connect() useCommissionerGeneratedPasscode: \(String(describing: useCommissionerGeneratedPasscode))")
4768

4869
let connectionCompleteCallback: (Swift.Error?) -> Void = { err in
4970
self.Log.error("MCConnectionExampleViewModel connect() completed with: \(err)")
5071
DispatchQueue.main.async {
5172
if err == nil {
73+
self.pendingPasscodeConfirmation = false;
5274
self.connectionSuccess = true
5375
if useCommissionerGeneratedPasscode {
5476
self.connectionStatus = "Successfully connected to Casting Player using the Casting Player/Commissioner-Generated passcode!"
@@ -125,6 +147,7 @@ class MCConnectionExampleViewModel: ObservableObject {
125147
self.Log.info("MCConnectionExampleViewModel connect() commissionerDeclarationCallback, calling MCCastingPlayer.continueConnecting()")
126148
let errContinue = selectedCastingPlayer?.continueConnecting()
127149
if errContinue == nil {
150+
self.pendingPasscodeConfirmation = false;
128151
self.connectionStatus = "Continuing to connect with user entered passcode: \(userEnteredPasscode)"
129152
} else {
130153
self.connectionStatus = "Continue Connecting to Casting Player failed with: \(String(describing: errContinue)) \n\nRoute back and try again."
@@ -142,6 +165,7 @@ class MCConnectionExampleViewModel: ObservableObject {
142165
self.Log.info("MCConnectionExampleViewModel connect() commissionerDeclarationCallback, Connection attempt cancelled by the user, calling MCCastingPlayer.stopConnecting()")
143166
let err = selectedCastingPlayer?.stopConnecting()
144167
self.connectionSuccess = false
168+
self.pendingPasscodeConfirmation = false;
145169
if err == nil {
146170
self.connectionStatus = "User cancelled the connection attempt with CastingPlayer. \n\nRoute back to exit."
147171
self.Log.info("MCConnectionExampleViewModel connect() commissionerDeclarationCallback, User cancelled the connection attempt with MCCastingPlayer, MCCastingPlayer.stopConnecting() succeeded.")
@@ -180,7 +204,10 @@ class MCConnectionExampleViewModel: ObservableObject {
180204

181205
self.Log.info("MCConnectionExampleViewModel.connect() calling MCCastingPlayer.verifyOrEstablishConnection()")
182206
let err = selectedCastingPlayer?.verifyOrEstablishConnection(with: connectionCallbacks, identificationDeclarationOptions: identificationDeclarationOptions)
183-
if err != nil {
207+
if err == nil {
208+
self.pendingPasscodeConfirmation = true;
209+
} else {
210+
self.pendingPasscodeConfirmation = false;
184211
self.Log.error("MCConnectionExampleViewModel connect(), MCCastingPlayer.verifyOrEstablishConnection() failed due to: \(err)")
185212
}
186213
}

0 commit comments

Comments
 (0)