What causes "missed method" in this code #2? - objective-c

I have the same problem as in this thread: What causes "Missed Method" in this code?
but i do not understand how to solve this or if it even is a problem.
I am doing the tutorial in the book "Beginning iOS Game Center and Game Kit" and get this problem. I do get "...Missed Method" all the time and is trying to understand why, unfortunately i do not. I also tried to use the answer on the thread above but to no avail.
I would very much appreciate some help on this one.
The code i am using currently:
#import "GameCenterManager.h"
#implementation GameCenterManager
#synthesize delegate;
+(BOOL)isGameCenterAvailable {
Class gcClass = (NSClassFromString(#"GKLocalPlayer"));
NSString *reqSysVer = #"4.1";
NSString *currSysVer = [[UIDevice currentDevice]systemVersion];
BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
return (gcClass && osVersionSupported);
}
-(void)retrieveFriendsList {
if ([GKLocalPlayer localPlayer].authenticated == YES) {
[[GKLocalPlayer localPlayer]loadFriendsWithCompletionHandler:^(NSArray *friends, NSError *error) {
[self callDelegateOnMainThread:#selector(friendsFinishLoading:error:) withArg:friends error:error];
}];
} else {
NSLog(#"...You must authenticate first");
}
}
-(void)authenticateLocalUser {
if ([GKLocalPlayer localPlayer].authenticated) {
return;
}
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error){
[self callDelegateOnMainThread:#selector(processGameCenterAuthentication:) withArg:NULL error: error];
}];
}
-(void)callDelegateOnMainThread:(SEL)selector withArg: (id) arg error:(NSError*) err {
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self callDelegate:selector withArg: arg error: err];
});
}
-(void)callDelegate: (SEL) selector withArg: (id) arg error: (NSError*) err {
assert([NSThread isMainThread]);
if ([delegate respondsToSelector: selector]) {
if(arg != NULL) {
[delegate performSelector: selector withObject: arg withObject: err];
} else {
[delegate performSelector: selector withObject: err];
}
} else {
NSLog(#"...Missed Method");
}
}
Output, after GC authorization:
...Missed Method

You code calls [self callDelegateOnMainThread:#selector(processGameCenterAuthentication:) withArg:NULL error: error]; which ends up in a method (callDelegate:withArg:error:) that checks wether your class implemented the processGameCenterAuthentication: method. You get the Missed Method output because you don't seem to have implemented it.

Related

NSURLSessionDownloadTask in NSOperation crashes on cancel

I'm trying to create a DownloadOperation subclass of NSOperation to download data asynchronously. Everything seemed to be working fine until I tried to add cancelling support. Basically, the completion handler of the operation's NSURLSessionDownloadTask seems to be called after the operation has been released. It will crash with EXC_BAD_ACCESS at the line weakSelf.state = kFinished.
The full sample project is here: https://github.com/angstsmurf/DownloadOperationQueue. Press Command+. after running to crash.
#import "DownloadOperation.h"
typedef enum OperationState : NSUInteger {
kReady,
kExecuting,
kFinished
} OperationState;
#interface DownloadOperation ()
#property NSURLSessionDownloadTask *task;
#property OperationState state;
#end
#implementation DownloadOperation
// default state is ready (when the operation is created)
#synthesize state = _state;
- (void)setState:(OperationState)state {
#synchronized(self) {
if (_state != state) {
[self willChangeValueForKey:#"isExecuting"];
[self willChangeValueForKey:#"isFinished"];
_state = state;
[self didChangeValueForKey: #"isExecuting"];
[self didChangeValueForKey: #"isFinished"];
}
}
}
- (OperationState)state {
#synchronized (self) {
return _state;
}
}
- (BOOL)isReady { return (self.state == kReady); }
- (BOOL)isExecuting { return (self.state == kExecuting); }
- (BOOL)isFinished { return (self.state == kFinished); }
- (BOOL)isAsynchronous {
return YES;
}
- (instancetype)initWithSession:(NSURLSession *)session downloadTaskURL:(NSURL *)downloadTaskURL completionHandler:(nullable void (^)(NSURL * _Nullable, NSURLResponse * _Nullable, NSError * _Nullable))completionHandler {
self = [super init];
if (self) {
__unsafe_unretained DownloadOperation *weakSelf = self;
// use weak self to prevent retain cycle
_task = [[NSURLSession sharedSession] downloadTaskWithURL:downloadTaskURL
completionHandler:^(NSURL * _Nullable localURL, NSURLResponse * _Nullable response, NSError * _Nullable error) {
/*
if there is a custom completionHandler defined,
pass the result gotten in downloadTask's completionHandler to the
custom completionHandler
*/
if (completionHandler) {
completionHandler(localURL, response, error);
}
/*
set the operation state to finished once
the download task is completed or have error
*/
weakSelf.state = kFinished;
}];
}
return self;
}
- (void)start {
/*
if the operation or queue got cancelled even
before the operation has started, set the
operation state to finished and return
*/
if (self.cancelled) {
self.state = kFinished;
return;
}
// set the state to executing
self.state = kExecuting;
NSLog(#"downloading %#", self.task.originalRequest.URL.absoluteString);
// start the downloading
[self.task resume];
}
-(void)cancel {
[super cancel];
// cancel the downloading
[self.task cancel];
}
#end
As pointed out in the comments by Scott Thompson, the correct keyword to use for the weakSelf variable is __weak, not __unsafe_unretained.

Snapchat's SnapKit addLoginStatusObserver not listening to events

I'm currently developing a React Native plugin for Snapchat's SnapKit SDK.
I can't seem to get the addLoginStatusObserver method to work (detailed here: https://snapkit.com/docs/api/ios/) and I suspect it's my lack of experience with Objective C's protocol/interface/implementation features.
Here's a trimmed down version of the code:
...
#interface RNSnapSDKListener : NSObject<SCSDKLoginStatusObserver> {
...
}
- (void)scsdkLoginLinkDidSucceed;
- (void)scsdkLoginLinkDidFail;
- (void)scsdkLoginDidUnlink;
...
#end
#implementation RNSnapSDKListener
- (void)scsdkLoginLinkDidSucceed{
NSLog(#"[RNSnapSDKListener] Snapchat Did Login!");
}
- (void)scsdkLoginLinkDidFail{
NSLog(#"[RNSnapSDKListener] Snapchat Did Fail!");
}
- (void)scsdkLoginDidUnlink{
NSLog(#"[RNSnapSDKListener] Snapchat Did Unlink!");
}
- (void)setDelegate: (RCTEventEmitter*) eventEmitter{
NSLog(#"[RNSnapSDKListener] Delegate Set!");
}
#end
#implementation RNSnapSDK
...
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(initialize){
RNSnapSDKListener *listener = [[RNSnapSDKListener alloc] init];
[listener setDelegate:self];
[SCSDKLoginClient addLoginStatusObserver:listener];
}
RCT_EXPORT_METHOD(login)
{
[SCSDKLoginClient loginFromViewController:[UIApplication sharedApplication].delegate.window.rootViewController completion:^(BOOL success, NSError * _Nullable error) {
}];
}
RCT_EXPORT_METHOD(logout: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
[SCSDKLoginClient unlinkAllSessionsWithCompletion:^(BOOL success) {
NSLog(#"Logout %s", success ? "true" : "false");
resolve(NULL);
}];
}
RCT_EXPORT_METHOD(getUserData: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
NSString *graphQLQuery = #"{me{externalId, displayName, bitmoji{avatar}}}";
NSDictionary *variables = #{#"page": #"bitmoji"};
[SCSDKLoginClient fetchUserDataWithQuery:graphQLQuery
variables:variables
success:^(NSDictionary *resources) {
NSDictionary *data = resources[#"data"];
resolve(data);
} failure:^(NSError * error, BOOL isUserLoggedOut) {
NSLog(#"%#",[error localizedDescription]);
NSLog(#" %s", isUserLoggedOut ? "true" : "false");
if(isUserLoggedOut){
[SCSDKLoginClient loginFromViewController:[UIApplication sharedApplication].delegate.window.rootViewController completion:^(BOOL success, NSError * _Nullable error) {
}];
}else{
reject(#"error", [error localizedDescription], error);
}
}];
}
NSURL *saved;
RCT_EXPORT_METHOD(authenticateDeepLink: (NSString *)url)
{
NSURL *finalUrl = [NSURL URLWithString:url];
saved = finalUrl;
[SCSDKLoginClient application:[UIApplication sharedApplication] openURL:finalUrl options:[NSMutableDictionary dictionary]];
}
...
#end
.initialize() is called inside the React Native module, and the setDelegate() method is called successfully (printing out "Delegate set" - this is for the react-native event bridge), but the other [RNSnapSDKListener]s dont print when they should (after logging in or logging out)
Is this something I'm doing wrong with objective-c or some other misuse of Snapchat's SDK?
Thanks!
The problem ended up being that the RNSnapSDKListener *listener needed to be declared as a global variable and initialized inside initialize() - not entirely sure why though - something with garbage collection maybe?

Creating NSError * results in application crash

I have the following situation:
My domain class gets some input validates it, and if validation passes it proceeds with saving data.
When control flow reaches the if statements, the application crashes
- (BOOL)createGmailAccountWithName:(NSString *)name
email:(NSString *)email
andPassword:(NSString *)password
error: (NSError **) error {
if (!name || name.length == 0) {
*error = [self createError:#"name"];
return NO;
}
if (!email || email.length == 0) {
*error = [self createError:#"email"];
return NO;
}
if (!password || password.length == 0) {
*error = [self createError:#"password"];
return NO;
}
//..
}
-(NSError *) createError: (NSString *) field {
NSString *errorMessage = [NSString stringWithFormat:#"Property %# is required", field];
NSDictionary *userInfo = #{
NSLocalizedFailureReasonErrorKey: NSLocalizedString(errorMessage, nil)
};
NSError *error = [NSError errorWithDomain:ACCOUNT_STORE_ERROR_DOMAIN
code:-1
userInfo:userInfo];
return error;
}
When I comment out all lines where the validation happens, the application does not crash.
I have no idea why this is happening. Can anyone point me into the right direction?
If you have this method:
- (BOOL)createGmailAccountWithName:(NSString *)name
email:(NSString *)email
andPassword:(NSString *)password
error: (NSError **) error
Folks are probably going to call it either like this:
NSError *error;
[accountCreator createGmailAccountWithName:#"Ben"
email:#"foo#example.com"
andPassword:#"pwd"
error:&error];
if (error)
{
NSLog(#"Hey I got an error: %#", error);
}
Or like this:
[accountCreator createGmailAccountWithName:#"Ben"
email:#"foo#example.com"
andPassword:#"pwd"
error:NULL];
// I couldn't care less about an error
In the second case, your code will will try to dereference **error, *error is not a valid pointer and would cause a crash.

Add open feature to Document based App

I have a Document based Application, with an TextView.
I want to add an open feature that writes it in the TextView.
I have the code, but it woundn't work.
The TextView is empty.
Heres my code:
#import "Document.h"
#implementation Document
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
NSFont *courier = [NSFont fontWithName: #"Courier" size:12];
[_textView setString: #"Blabla"];
[_textView setFont:courier];
NSLog(#"Tesg");
[_textView setString:#"TEST"];
}
- (id)init
{
NSLog(#"Tesg");
self = [super init];
if (self) {
// Add your subclass-specific initialization here.
}
return self;
}
- (NSString *)windowNibName
{
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
return #"Document";
}
- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
[super windowControllerDidLoadNib:aController];
// Add any code here that needs to be executed once the windowController has loaded the document's window.
}
+ (BOOL)autosavesInPlace
{
return YES;
}
/*- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
// Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil.
// You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
NSException *exception = [NSException exceptionWithName:#"UnimplementedMethod" reason:[NSString stringWithFormat:#"%# is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
#throw exception;
return nil;
}
*/
- (NSData *)dataOfType:(NSString *)pTypeName error:(NSError **)pOutError {
NSDictionary * zDict;
if ([pTypeName compare:#"public.plain-text"] == NSOrderedSame ) {
zDict = [NSDictionary dictionaryWithObjectsAndKeys:
NSPlainTextDocumentType,
NSDocumentTypeDocumentAttribute,nil];
} else {
NSLog(#"ERROR: dataOfType pTypeName=%#",pTypeName);
*pOutError = [NSError errorWithDomain:NSOSStatusErrorDomain
code:unimpErr
userInfo:NULL];
return NULL;
} // end if
NSString * zString = [[_textView textStorage] string];
NSData * zData = [zString dataUsingEncoding:NSASCIIStringEncoding];
return zData;
} // end dataOfType
/*
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
// Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.
// You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
// If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
NSLog(data);
NSException *exception = [NSException exceptionWithName:#"UnimplementedMethod" reason:[NSString stringWithFormat:#"%# is unimplemented", NSStringFromSelector(_cmd)] userInfo:nil];
#throw exception;
return YES;
}
*/
- (BOOL)readFromData:(NSData *)pData
ofType:(NSString *)pTypeName
error:(NSError **)pOutError {
if ([pTypeName compare:#"public.plain-text"] != NSOrderedSame) {
NSLog(#"** ERROR ** readFromData pTypeName=%#",pTypeName);
*pOutError = [NSError errorWithDomain:NSOSStatusErrorDomain
code:unimpErr
userInfo:NULL];
return NO;
} // end if
NSDictionary *zDict = [NSDictionary dictionaryWithObjectsAndKeys:
NSPlainTextDocumentType,
NSDocumentTypeDocumentAttribute,
nil];
NSDictionary *zDictDocAttributes;
NSError *zError = nil;
zNSAttributedStringObj =
[[NSAttributedString alloc]initWithData:pData
options:zDict
documentAttributes:&zDictDocAttributes
error:&zError];
if ( zError != NULL ) {
NSLog(#"Error readFromData: %#",[zError localizedDescription]);
return NO;
} // end if
NSString *content = [zNSAttributedStringObj string];
NSLog(#"%#", content);
NSLog(#"%c", [_textView isEditable]);
[_textView setString:content];
return YES;
} // end readFromData
#end
Thanks!
Please do not flag it as "Not a real Question" or something else.
The problem is the readXXX methods are called before the window is created. This means that _textView is nil. You need to use -(void)windowControllerDidLoadNib:(NSWindowController *)windowController to populate _textView with the information you load from the file.
You can avoid being caught out by this kind of problem in future by placing NSAssert calls in your code to confirm the preconditions required for your methods to operate correctly:
NSAssert(_textView != nil, #"_textView not initialized");

AFNetworking Asynchronous Data Fetching

I'm using the AFNetworking library to pull a JSON feed from a server to populate a UIPickerView, but I'm having a little trouble wrapping my head around the asynchronous way of doing things. The #property classChoices is an NSArray that's being used to populate the UIPickerView, so that the web call is only performed once. However, since the block isn't finished by the time the instance variable is returned, the getter returns nil, and it eventually causes my program to crash later on. Any help in fixing this would be greatly appreciated. Let me know if you need any additional information.
PickerViewController.m classChoices Getter
- (NSArray *)classChoices {
if (!_classChoices) {
// self.brain here refers to code for the SignUpPickerBrain below
[self.brain classChoicesForSignUpWithBlock:^(NSArray *classChoices) {
_classChoices = classChoices;
}];
}
return _classChoices;
}
SignUpPickerBrain.m
- (NSArray *)classChoicesForSignUpWithBlock:(void (^)(NSArray *classChoices))block {
[[UloopAPIClient sharedClient] getPath:#"mobClass.php" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseJSON) {
NSLog(responseJSON);
if (block) {
block(responseJSON);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
if (block) {
block(nil);
}
}];
}
You need a method like the following in your PickerViewController which returns the array once it has been downloaded. Once the callback has been returned, you can then continue on with your code:
- (void)classChoices:(void (^) (NSArray * classChoices)) _callback {
if (!self.classChoices) {
// self.brain here refers to code for the SignUpPickerBrain below
[self.brain classChoicesForSignUpWithBlock:^(NSArray *classChoices) {
_callback(classChoices);
}];
}
}
// call the method
- (void) viewDidLoad {
[super viewDidLoad];
[self classChoices:^(NSArray * updatedChoices) {
self.classChoices = updatedChoices;
[self.pickerView reloadAllComponents];
}];
}