Darius Darius - 24 days ago 5
C# Question

WCSession Send Message gives error "payload could not be delivered"

I am using Xamarin to develop an Apple Watch app. I am trying to send a message from my watch to the iPhone with my

SendMessage
function. When I do this, I get in the
out
error the message
payload could not be delivered
. I can only read part of the message ("payload could n...") because I am writing it on a label (since my debugger doesn't work in Xamarin I can't take a look at the message), but after doing some googling I'm assuming that's what it is written. Any idea why? Here is my code:

public sealed class WCSessionManager : NSObject, IWCSessionDelegate
{
// Setup is converted from https://www.natashatherobot.com/watchconnectivity-say-hello-to-wcsession/
// with some extra bits
private static readonly WCSessionManager sharedManager = new WCSessionManager();
private static WCSession session = WCSession.IsSupported ? WCSession.DefaultSession : null;

#if __IOS__
public static string Device = "Phone";
#else
public static string Device = "Watch";
#endif

public event ApplicationContextUpdatedHandler ApplicationContextUpdated;

public delegate void ApplicationContextUpdatedHandler(WCSession session, Dictionary<string, object> applicationContext);

public event MessageReceivedHandler MessageReceived;

public delegate void MessageReceivedHandler(Dictionary<string, object> message, Action<Dictionary<string, object>> replyHandler);

private WCSession validSession
{
get
{
#if __IOS__
// Even though session.Paired and session.WatchAppInstalled are underlined, it will still build as they are available on the iOS version of WatchConnectivity.WCSession
Console.WriteLine($"Paired status:{(session.Paired ? '✓' : '✗')}\n");
Console.WriteLine($"Watch App Installed status:{(session.WatchAppInstalled ? '✓' : '✗')}\n");
return (session.Paired && session.WatchAppInstalled) ? session : null;
//return session;
#else
return session;
#endif
}
}

private WCSession validReachableSession
{
get
{
return session.Reachable ? validSession : null;
}
}

private WCSessionManager() : base() { }

public static WCSessionManager SharedManager
{
get
{
return sharedManager;
}
}

public void StartSession()
{
if (session != null)
{
session.Delegate = this;
session.ActivateSession();
Console.WriteLine($"Started Watch Connectivity Session on {Device}");
}
}

[Export("sessionReachabilityDidChange:")]
public void SessionReachabilityDidChange(WCSession session)
{
Console.WriteLine($"Watch connectivity Reachable:{(session.Reachable ? '✓' : '✗')} from {Device}");
// handle session reachability change
if (session.Reachable)
{
// great! continue on with Interactive Messaging
}
else {
//

Answer

I solved it. Instead of implementing IWCSessionDelegate, I simply implement WCSessionDelegate instead and override the functions as needed.