Is the best solution for implementing a web connection throughout an iOS app?

In my application, the connection and communication websocketare in a specific ViewController, but later a request appeared to receive messages through websocketfor the entire application.

What would be the best way to do this? To move code websocketto AppDelegateor create super ViewController? Or is something else better?

+4
source share
2 answers

If you want your application to constantly communicate with the outside, and that several view controllers can be present and act on messages, you need to move the message to a separate instance and provide an interface for your view controllers to interact with it.

I would create a communication class or a set of classes that you can create a shared instance inside your appDelegate application. Different view managers can access this shared instance through the appDelegate application or some other mechanism that you provide.

How complicated it should be depends on your exact requirement.

Modify the following requirement to update comments:

, , , , , .

, , API . - ( , API- - ), runloop, , , , . , , .

. (, ) . . . , , . , , .

API , . , . , .

.

+4

. :

#import <foundation/Foundation.h>

@interface MyManager : NSObject {
    NSString *someProperty;
}

@property (nonatomic, retain) NSString *someProperty;

+ (id)sharedManager;

@end

#import "MyManager.h"

@implementation MyManager

@synthesize someProperty;

#pragma mark Singleton Methods

+ (id)sharedManager {
    static MyManager *sharedMyManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMyManager = [[self alloc] init];
    });
    return sharedMyManager;
}

- (id)init {
  if (self = [super init]) {
      someProperty = [[NSString alloc] initWithString:@"Default Property Value"];
  }
  return self;
}

@end

MyManager *sharedManager = [MyManager sharedManager];

, websocket , .

,

+2

Source: https://habr.com/ru/post/1585085/


All Articles