WebSocket in Background transfer and NSURLSession in Xamarin.iOS

mjrzvgn 1 Reputation point
2021-06-29T09:39:54.093+00:00

I am looking into creating a Xamarin.iOS app that can connect to a WebSocket server when the app is backgrounded or suspended. The app is expected to close the channel once it receives a certain 'close' message from the server.

As per Apple's documentation, NSUrlSession allows to create a WebSocket task for the provided URL.

Xamarin has a short guide about using NSUrlSession, but the API reference documentation does not have any WebSocket-related methods.

A search engine search for 'xamarin nsurlsession webSocketTaskWithURL' returns zero results.

Is it possible to use WebSocket in NSUrlSession in Xamarin.iOS?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,316 Reputation points Microsoft Vendor
    2021-06-30T06:05:26.947+00:00

    Hello,
    Welcome to our Microsoft Q&A platform!

    If you want to use WebSocket in Xamarin.iOS, declare a namespace(using Foundation) to access NSUrlSessionWebSocketTask,then use C# to implement it. The following code shows a sample:

    NSUrlSession session = NSUrlSession.SharedSession;  
    NSUrlSessionWebSocketTask task = session.CreateWebSocketTask(new NSUrl(""));  
    

    Best Regards,
    Wenyan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments