IOS implementing SignalR using Objective-C
For the past few weeks i been doing some research on SignalR in Objective C and i found very few resources available on the internet for implementing signlR in Objective-C. Therefore i thought of writing this tutorial, hoping it will help some developers one day.
In this tutorial i will explain you, how to get connected to SingalR hub using IOS and Objective-C. This tutorial is not for beginners.
All Information about SignalR Objective-C library can be found in this Link.
Lets setup the environment first. flow the steps below
- First of all create a new IOS Objective C project.
- Close the project and navigate to the project directory Using Terminal.
- Create a new podfile and save the following code. (make sure to change your project name instead of
signalRObjective2
)
1 2 3 4 5 |
//------------------------------------ target 'signalRObjective2' do pod 'SignalR-ObjC', '~> 2.0' end |
4. Now run the below command in your terminal
. This will install all the required libraries and create a file with extension .xcworkspace in side your project folder.pod install
5. Open the file yourproject.xcworkspace file
Now we got over environment setup , lets do the real coding to get connected to SignalR HUB.
In order to do a signal R communication, You need to understand three main factors in Signal R, They are explained below
- get connected – You must get connected to signalR Hub and keep the connection with out closing it.
- Subscribe to hub method – You must create a subscription to your hub method at the time of connection.
- Invoke hun method – You must invoke a hub method, when ever you want to send some message to the Hub and also receive the response through the subscribed method as explained before.
Now lets see how to implement it.
Connection
1 2 3 4 5 6 7 8 |
/************************/ hubConnection = [SRHubConnection connectionWithURLString:@"https://controlpannel.sathyabaman.com/"]; // Create a proxy to the chat service chat = [hubConnection createHubProxy:@"chatingHub"]; [chat on:@"serviceStatus" perform:self selector:@selector(testMethod:)]; |
As you can see from the above code, you will have to tell the signalR hub URl and also the hub proxy name to the signalR-ObjectiveC library.
1 2 3 4 5 6 7 8 |
/**************************/ [chat on:@"serviceStatus" perform:self selector:@selector(testMethod:)]; -(void) testMethod:(NSString *) mystr{ NSLog(@"my test method : %@", mystr); } |
Then you need to subscribe to the Hub method as shown in the above code. serviceStatus is the hub method and it will return the response to testMethod which is in the client side.
1 2 3 4 5 6 7 8 |
/***********/ - (IBAction)invokehub:(id)sender { [chat invoke:@"GetIncomQueue" withArgs:[NSArray arrayWithObjects:@"da51feb9-a41e675", @"9bb2d8af-883385-c5ca0f385ad0", @"PfBxGAutCGh7UBPRU0lgYHnhEHlOmQ==", nil]]; } |
Invoking a method in signalR hub is very simple as shown in the above code. And if you code dont have any parameters. Your invoke will be like this as shown below
1 2 3 4 5 6 |
/****************/ [chat invoke:@"CheckStatus" withArgs:[NSArray arrayWithObjects:nil, nil]]; |
That it. Over all Code will be as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
// // ViewController.m // signalRObjective2 // // Created by Sathyabaman on 12/15/16. // Copyright © 2016 sathyabaman. All rights reserved. // #import "ViewController.h" #import "SignalR.h" @interface ViewController (){ int *count; SRHubConnection *hubConnection; SRHubProxy *chat; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. count = 0; hubConnection = [SRHubConnection connectionWithURLString:@"https://controlpannel.sathyabaman.com/"]; // Create a proxy to the chat service chat = [hubConnection createHubProxy:@"chatingHub"]; [chat on:@"serviceStatus" perform:self selector:@selector(testMethod:)]; // Register for connection lifecycle events [hubConnection setStarted:^{ NSLog(@"Connection Started"); self.tv_returnMessage.text =[NSString stringWithFormat:@"Connection Started successfully."]; }]; [hubConnection setReceived:^(NSString *message) { NSLog(@"Connection Recieved Data: %@",message); NSString* count_string = [NSString stringWithFormat:@"%d", count]; NSString * base_string =@"Hello Sathya : "; NSString *myString = [NSString stringWithFormat:@"%@ %@", base_string, count_string]; NSString* dict_string = [NSString stringWithFormat:@"%@", message]; self.tv_returnMessage.text = [NSString stringWithFormat:@"%@ : -- : %@", myString , dict_string]; count = count + 1; }]; [hubConnection setConnectionSlow:^{ NSLog(@"Connection Slow"); }]; [hubConnection setReconnecting:^{ NSLog(@"Connection Reconnecting"); }]; [hubConnection setReconnected:^{ NSLog(@"Connection Reconnected"); }]; [hubConnection setClosed:^{ NSLog(@"Connection Closed"); }]; [hubConnection setError:^(NSError *error) { NSLog(@"Connection Error %@",error); }]; // Start the connection } - (IBAction)checkStatus:(id)sender { [chat invoke:@"CheckWebStatus" withArgs:[NSArray arrayWithObjects:nil, nil]]; } - (IBAction)invokehub:(id)sender { [chat invoke:@"GetChatQueue" withArgs:[NSArray arrayWithObjects:@"da51feb9-0162ca41e675", @"9bb2d8afa0f385ad0", @"PfBxGtCGsETyUBPRU0lgYHnhEHlOmQ==", nil]]; } - (IBAction)startConnection:(id)sender { self.tv_returnMessage.text =@"Starting Connection....... Please wait....."; [hubConnection start]; } -(void) testMethod:(NSString *) myobj{ NSLog(@"my test method : %@", myobj); } - (void)addMessage:(NSString *)message { // Print the message when it comes in NSLog(@"%@", message); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end |
That is. If you have any problems are suggestions please be free to comment below, I will be happy to respond them.Thank you. Happy Coding 🙂
Aditi Tiwari
Hello Sathyabaman,
I have a query regarding this blog , I also using signalR for realTime dating within two person,Then I have to facing the problem which is whenever an user goes into background then they did not able to call the callback method of signalR, So can we call this method of signalR whenever we will be in the background mode.
Waiting for your response.