-1

I'm trying to use Amazon API for iOS that handles the login for Amazon website in Objective-C. I'm using this source. However, when I implement AMZNAuthorizeUserDelegate, I get the following error message:

ARC forbids explicit message send of 'retain'.

I've never used Objective-C before so I would appreciate if anyone could help me with the code.

Here is my code:

#import <LoginWithAmazon/LoginWithAmazon.h>
#import "AMZNAuthorizeUserDelegate.h"
#import "AMZNGetProfileDelegate.h"

@implementation AMZNAuthorizeUserDelegate

- (id)initWithParentController:(ViewController*)aViewController {
    if(self = [super init]) {
        parentViewController = [aViewController retain];
}

return self;
}

- (void)requestDidSucceed:(APIResult *)apiResult {
  AMZNGetProfileDelegate* delegate = [[[AMZNGetProfileDelegate alloc]         initWithParentController:parentViewController] autorelease];
[AIMobileLib getProfile:delegate];
}

- (void)requestDidFail:(APIError *)errorResponse {
NSString *message = errorResponse.error.message;
// Your code when the authorization fails.

[[[[UIAlertView alloc] initWithTitle:@"" message:[NSString
                                                      stringWithFormat:@"User authorization failed with message: %@",
                                                  errorResponse.error.message] delegate:nil
                   cancelButtonTitle:@"OK"otherButtonTitles:nil] autorelease] show];
}

@end
Cocoatype
  • 2,572
  • 25
  • 42
katya
  • 99
  • 3
  • 9
  • This seems to be very old code. Retain/release isn't needed with ARC and ARzc has been around for many years. You can replace that line with `parentViewController = aViewController` – Paulw11 Aug 07 '16 at 11:00
  • Possible duplicate of [ARC forbids explicit message send of 'retain' issue](http://stackoverflow.com/questions/11877025/arc-forbids-explicit-message-send-of-retain-issue) – Cocoatype Aug 07 '16 at 12:35

1 Answers1

2

The error message is correct: the call to retain is no longer valid.

Automatic Reference Counting, or ARC, was added to the Objective-C language in 2011 with iOS 5 and Mac OS X 10.7. Prior to ARC, you had to manage the memory usage of your app manually with calls to methods such as -retain, -release, and -autorelease. ARC manages these calls automatically at compile time, and, as such, does not allow you to call them yourself.

As @Paulw11 mentions in his comment, you should be able to replace that line with

parentViewController = aViewController

and ARC will do the correct thing automatically.

Cocoatype
  • 2,572
  • 25
  • 42
  • 1
    I would be suspicious of code that old. In addition to needing to be updated for ARC, It's likely to use deprecated frameworks and be otherwise out of date. AWS is still actively supported. You should look for updated sample code rather than working with code that's that outdated. – Duncan C Aug 07 '16 at 12:24
  • @DuncanC: Agreed, but having dealt with crappy third-party libraries before, sometimes there really _isn't_ anything newer, and you have to work with the hand you're dealt. Also worth noting: this isn't the AWS SDK that OP is using, it's the “Login with Amazon” SDK. – Cocoatype Aug 07 '16 at 12:29