Reputation: 3968
This is my first day of Objective C so I apologise for the lack of knowledge.
I need to import an existing SKD into an App and I done it successfully. Now I need to create the delegate methods and I don't understand how can I do it.
This is the structure of the header file included from the SDK (SDKManager.h):
@protocol SDKManagerDelegate;
@interface SDKManager : NSObject
@property (nonatomic, weak) id<SDKDelegate> delegate;
+(void)initialize:(NSString*)appId withKEY:(NSString*)key;
+(void)setHandler:(id)delegate;
@end
@protocol SDKManagerDelegate <NSObject>
@required
-(void)appDidReceiveTokens:(NSDictionary*)items withResponse:(NSDictionary*)response;
@end
So, from my FirstViewController.m I was able to import the header and call two methods:
#import "FirstViewController.h"
#import "SDKManager.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[SDKManager setHandler:[UIApplication sharedApplication].delegate];
[SDKManager initialize:@"AppId"withKEY:@"1234"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
but I have noticed that I am not able to call the other methods (i.e. appDidReceiveTokens). Actually the instructions require to create those methods but I have no idea where.
Any help is really appreciated. Thank you
Upvotes: 0
Views: 125
Reputation: 21154
The reason for that is you have class methods which sets the calls setHandler method and the delegate is property, so where do you assign the delegate and when and how do you call the delegate. I hope you understand what a class and instance is. So, you cannot call the delegate until you create instance of your object.
You have two different class methods which is used to set some attributes to the class, would it make sense to have them as property.
More generic and better way to do this would be like this,
@protocol SDKManagerDelegate <NSObject>
@required
-(void)appDidReceiveTokens:(NSDictionary*)items
withResponse:(NSDictionary*)response;
@end
@protocol SDKManagerDelegate;
@interface SDKManager : NSObject
- (instancetype)initWithAppId:(NSString *)appId
key:(NSString *)key
delegate:(id<SDKManagerDelegate>)delegate;
@end
@interface SDKManager ()
@property (nonatomic, copy, readonly) NSString *appId;
@property (nonatomic, copy, readonly) NSString *key;
@property (nonatomic, weak, readonly) id<SDKManagerDelegate> delegate;
@end
@implementation SDKManager
- (instancetype)initWithAppId:(NSString *)appId
key:(NSString *)key
delegate:(id<SDKManagerDelegate>)delegate
{
if (self = [super init]) {
_appId = [appId copy];
_key = [key copy];
_delegate = delegate;
}
return self;
}
- (void)doSomeNetworkRequestHere
{
[self fetchTokenFromServer:^(NSDictionary *tokens, NSDictionary *response){
[self.delegate appDidReceiveTokens:tokens
withResponse:response];
}];
}
- (void)fetchTokenFromServer:(void(^)(NSDictionary *tokens, NSDictionary *response))completion
{
}
@end
Upvotes: 1
Reputation: 6907
You do not call delegate methods directly in the files in which you are implementing the delegate methods. Review Apples documentation on the concept of Delegation.
To implement this properly you would adopt the delegate in your class, then implement the delegate methods that are @required
and/or @optional
.
Upvotes: 1
Reputation: 2372
You've correctly created the delegate protocol and a property to store the SDKManager
's delegate.
Your setHandler:
and initialize:withKEY:
methods are class methods, whereas the delegate
property belongs to each instance of SDKManager
. Without seeing your implementation file (.m) for SDKManager
, it's hard to know why you've set it up this way. You may be attempting to follow a singleton pattern - if so, read up on it, e.g. here.
Upvotes: 1