SoFunction
Updated on 2025-04-04

Detailed explanation of IOS proxy implementation examples

IOS proxy implementation

In client development, notifications, proxy, and block are often used to realize the association between each page. Notifications are transmitted in a always "blind" way. Agent, block
You can clearly know the relationship between each interface. Taking the agent as an example, the general practice is as follows:

DesViewController *des = [[DesViewController alloc] init]; = self;[ pushViewController:des animated:YES];

In this case, the two interfaces generally have a certain relationship, for example: jumping from interface A to interface B or the view of a is part of the A controller. However, how to deal with it if there is no contact? For example: The A interface needs to display different data according to the user's login status, or display different interface situations:

Practical combat:

Idea: Create a management class to process, set up the corresponding proxy method, and then add or delete the corresponding proxy method when needed.

Core code:

.h file

//// // iOSDelegate/// Created by admin on 2016/11/6.// Copyright © 2016 Reading. All rights reserved.//#import <Foundation/>@protocol UserLoginStatusDelegate <NSObject>
- (void)userDidLoginIn;
- (void)userWillLoginOut;
@end@interface RSLoginService : NSObject+ (instancetype)sharedInstance;
@property (nonatomic, strong) NSMutableSet *delegates;
- (void)onWillLoginOut;
- (void)onDidLoginIn;
- (void)addDelegate:(id<UserLoginStatusDelegate>) delegate;
- (void)removeDelegate:(id<UserLoginStatusDelegate>) delegate;@end

.m file

//// // iOSDelegate//// Created by admin on 2016/11/6.

// Copyright © 2016 Reading. All rights reserved.
//#import ""@implementation 

RSLoginService+ (instancetype)sharedInstance{  static id instance;  static dispatch_once_t onceToken;  dispatch_once(&onceToken, ^{ 
   instance = [[RSLoginService alloc] init];  });  return instance;}- (void)onWillLoginOut{  

// do something you need to do before Login out 
 [ makeObjectsPerformSelector:@selector(userWillLoginOut)];}- (void)onDidLoginIn{  

 // do something you need to do after Login In  
 [ makeObjectsPerformSelector:@selector(userDidLoginIn)];}- 
(void)addDelegate:(id<UserLoginStatusDelegate>) delegate{  if (![ containsObject:delegate]) 
{    [ addObject:delegate];  }
}- (void)removeDelegate:(id<UserLoginStatusDelegate>) 
delegate{  if (![ containsObject:delegate]) { 
    [ removeObject:delegate];  }}- (NSMutableSet *)delegates{  if (!_delegates) {    _delegates = [NSMutableSet set];  }  return _delegates;}@end

Thank you for reading, I hope it can help you. Thank you for your support for this site!