This is the success parameter to my login which is under the closing bracket in viewDidload
//check whether it's a login or register
NSString* command = (sender.tag==1)?#"register":#"login";
NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:command, #"command", fldUsername.text, #"username", hashedPassword, #"password", nil];
//make the call to the web API
[[API sharedInstance] commandWithParams:params onCompletion:^(NSDictionary *json) {
//result returned
NSDictionary* res = [[json objectForKey:#"result"] objectAtIndex:0];
if ([json objectForKey:#"error"]==nil && [[res objectForKey:#"IdUser"] intValue]>0) {
[[API sharedInstance] setUser: res];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
//show message to the user
[[[UIAlertView alloc] initWithTitle:#"Logged in" message:[NSString stringWithFormat:#"Welcome %#",[res objectForKey:#"username"]] delegate:nil cancelButtonTitle:#"Close" otherButtonTitles: nil] show];
} else {
//error
[UIAlertView error:[json objectForKey:#"error"]];
}
}];
}
And this is the programatic connection in the view that I want to show when the user is authorized
-(void)viewDidload
[super viewDidLoad];
{
if (![[API sharedInstance] isAuthorized]) {
[self performSegueWithIdentifier:#"ShowLogin" sender:nil];
}
The problem is when I enter saved logged in credentials in my database (i.e. username: user1 password: password) I get the correct UIAlert I'm supposed to receive (Logged in - Welcome user1). But when i enter (username:user1 password: fake password) I still get the logged in UIAlert. Also if i try to register a new user from the app i.e. (username:user2 password: password 2) I get an authentication failed UIAlert. Also my segue doesn't actually happen when I enter valid credentials. Any tips?
Updated: logout delegate
-(void)logout {
//logout the user from the server, and also upon success destroy the local authorization
[[API sharedInstance] commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:#"logout", #"command", nil] onCompletion:^(NSDictionary *json) {
//logged out from server
[API sharedInstance].user = nil;
[self performSegueWithIdentifier:#"ShowLogin" sender:nil];
}];
}
[[API sharedInstance] commandWithParams:params onCompletion:^(NSDictionary *json) {
//result returned
NSDictionary* res = [[json objectForKey:#"result"] objectAtIndex:0];
if ([json objectForKey:#"error"]==nil && [[res objectForKey:#"IdUser"] intValue]>0) {
[[API sharedInstance] setUser: res];
[API sharedInstance].isAuthorized = YES; //Added
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
//show message to the user
[[[UIAlertView alloc] initWithTitle:#"Logged in" message:[NSString stringWithFormat:#"Welcome %#",[res objectForKey:#"username"]] delegate:nil cancelButtonTitle:#"Close" otherButtonTitles: nil] show];
} else {
[[API sharedInstance] setUser: nil]; //Added
[API sharedInstance].isAuthorized = NO; //Added
//error
[UIAlertView error:[json objectForKey:#"error"]];
}
}];
Find the lines with inline comments //Added and try those in your code. Also as mentioned update login view -> viewWillAppear and logout method.
Hope this helps.
Related
I'm calling up a session in my LoginViewController as seen below. The problem is that, after it authenticates and closes the popupview... the session returns nil. So it initially reaches out to the api and starts a session (the first log records nil as the session is open and the second log records "Signed in as..." with the username) then when it closes the login view it records nil again. Not exactly use what I could be doing wrong...
Could it be because I'm using TWTRSession* session to check the session? Could that be logging me off? Or is it related to me closing the popupview?
I do essentially the same thing with my facebook login but the facebook session stays open until I logout...
- (void)viewDidLoad
{
[super viewDidLoad];
self.email.delegate = self;
self.pass.delegate = self;
//self.view.frame = CGRectMake(self.bounds.size.width, self.bounds.size.height);
[Fabric with:#[[Twitter sharedInstance]]];
TWTRSession* session;
if (session) {
NSLog(#"Early bird %#", [session userName]);
[self closePopup];
}
else {
NSLog(#"No dice");
}
/*
TWTRLogInButton *logInButton = [TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) {
// play with Twitter session
}];
logInButton.center = self.view.center;
[self.view addSubview:logInButton];
*/
TWTRLogInButton* logInButton = [TWTRLogInButton
buttonWithLogInCompletion:
^(TWTRSession* session, NSError* error) {
if (session) {
NSLog(#"signed in as %#", [session userName]);
[self closePopup];
} else {
NSLog(#"error: %#", [error localizedDescription]);
}
}];
I decided to include my actionsheet as well so you could see how I'm handling the login/logout situation:
- (void)sheet: (id) sender {
NSString *email = [[NSUserDefaults standardUserDefaults] objectForKey:#"email"];
UIActionSheet *sheet;
TWTRSession *session;
//if ([FBSDKAccessToken currentAccessToken]) {
if (email == nil && [FBSDKAccessToken currentAccessToken] == nil && session == nil) {
sheet = [[UIActionSheet alloc] initWithTitle:#"Profile"
delegate:nil // Can be another value but will be overridden when showing with handler.
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Login"
otherButtonTitles:nil];
}
else
{
sheet = [[UIActionSheet alloc] initWithTitle:#"Profile"
delegate:nil // Can be another value but will be overridden when showing with handler.
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Logout"
otherButtonTitles:#"User page", nil];
}
[sheet showInView:self.view
handler:^(UIActionSheet *actionSheet, NSInteger buttonIndex) {
if (buttonIndex == [actionSheet cancelButtonIndex]) {
NSLog(#"Cancel button index tapped");
} else if (buttonIndex == [actionSheet destructiveButtonIndex]) {
if (email == nil && [FBSDKAccessToken currentAccessToken] == nil && session == nil) {
LoginViewController *loginView = [[LoginViewController alloc] initWithNibName:#"LoginView" bundle:[NSBundle mainBundle]];
//[self.view modalViewController:loginView animated:YES];
//[self presentModalViewController:loginView animated:YES];
//[self.navigationController pushViewController:loginView animated:YES];
loginView.delegate = (id) self;
[self presentPopupViewController:loginView animationType:MJPopupViewAnimationSlideTopBottom];
NSLog(#"Login View");
}
else
{
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:#"email"];
FBSDKLoginManager *logout = [[FBSDKLoginManager alloc] init];
[logout logOut];
}
} else {
NSLog(#"Button %i tapped", buttonIndex);
if (buttonIndex == 1) {
UserViewController * userView = [[UserViewController alloc] initWithNibName:#"UserView" bundle:[NSBundle mainBundle]];
[userView setUEmail:email];
[self presentViewController:userView animated:YES completion:nil];
}
}
}];
}
So I ended up creating my own custom IBAction to handle twitter logins. This works much better. Basically you just have to be careful with how you check if a session is active:
-(IBAction)twlogin:(id)sender
{
[Fabric with:#[[Twitter sharedInstance]]];
[[Twitter sharedInstance] logInWithCompletion:^
(TWTRSession *session, NSError *error) {
if (session) {
NSLog(#"signed in as %#", [session userName]);
TWTRShareEmailViewController* shareEmailViewController =
[[TWTRShareEmailViewController alloc]
initWithCompletion:^(NSString* email2, NSError* error) {
NSLog(#"Email %#, Error: %#", email2, error);
}];
[self presentViewController:shareEmailViewController
animated:YES
completion:nil];
[self closePopup];
NSLog(#"session check %#", [session userName]);
} else {
NSLog(#"error: %#", [error localizedDescription]);
}
}];
[[Twitter sharedInstance] logOut];
}
As you can see TWTR *session has to be included with Twitter shared instance like so:
[[Twitter sharedInstance] logInWithCompletion:^
(TWTRSession *session, NSError *error) {
I didn't do that before, and that's why it wasn't working. Hopefully this helps someone. (now if someone would help me getting Twitter emails to work... that would be greatly appreciated...)
Step by step how would I segue a user after authentication seen below?
//curent code in viewcontroller2.h
-(IBAction)btnLoginRegisterTapped:(UIButton*)sender {
//form fields validation
if (fldUsername.text.length < 4 || fldPassword.text.length < 4) {
[UIAlertView error:#"Enter username and password over 4 chars each."];
return;
}
//salt the password
NSString* saltedPassword = [NSString stringWithFormat:#"%#%#", fldPassword.text, kSalt];
//prepare the hashed storage
NSString* hashedPassword = nil;
unsigned char hashedPasswordData[CC_SHA1_DIGEST_LENGTH];
//hash the pass
NSData *data = [saltedPassword dataUsingEncoding: NSUTF8StringEncoding];
if (CC_SHA1([data bytes], [data length], hashedPasswordData)) {
hashedPassword = [[NSString alloc] initWithBytes:hashedPasswordData length:sizeof(hashedPasswordData) encoding:NSASCIIStringEncoding];
} else {
[UIAlertView error:#"Password can't be sent"];
return;
}
//check whether it's a login or register
NSString* command = (sender.tag==1)?#"register":#"login";
NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:command, #"command", fldUsername.text, #"username", hashedPassword, #"password", nil];
//make the call to the web API
[[API sharedInstance] commandWithParams:params onCompletion:^(NSDictionary *json) {
//result returned
NSDictionary* res = [[json objectForKey:#"result"] objectAtIndex:0];
if ([json objectForKey:#"error"]==nil && [[res objectForKey:#"IdUser"] intValue]>0) {
[[API sharedInstance] setUser: res];
//this doesn't dismiss anything; its leftover from another tutorial =)
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
//show message to the user
[[[UIAlertView alloc] initWithTitle:#"Logged in" message:[NSString stringWithFormat:#"Welcome %#",[res objectForKey:#"username"]] delegate:nil cancelButtonTitle:#"Close" otherButtonTitles: nil] show];
} else {
//error
[UIAlertView error:[json objectForKey:#"error"]];
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
Would connecting a sufficient segue on the storyboard and putting this above didrecievememory warning suffice or no?
if (![[API sharedInstance] isAuthorized]) {
[self performSegueWithIdentifier:#"ShowLogin" sender:nil];
}
How does one go about creating segues from the storyboard ? I've seen these 3 links but still need a firm answer in creating a segue after a login. Custom segue after successful login http://www.raywenderlich.com/50308/storyboards-tutorial-in-ios-7-part-1 https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomSegues/CreatingCustomSegues.html
performSegueWithIdentifier is correct. You need to make sure you execute this on the main thread -
NSString* command = (sender.tag==1)?#"register":#"login";
NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:command, #"command", fldUsername.text, #"username", hashedPassword, #"password", nil];
//make the call to the web API
[[API sharedInstance] commandWithParams:params onCompletion:^(NSDictionary *json) {
//result returned
NSDictionary* res = [[json objectForKey:#"result"] objectAtIndex:0];
if ([json objectForKey:#"error"]==nil && [[res objectForKey:#"IdUser"] intValue]>0) {
[[API sharedInstance] setUser: res];
dispatch_async(dispatch_get_main_queue(),^{
[self performSegueWithIdentifier:#"someIdentifier" sender:self];
});
} else {
//error
[UIAlertView error:[json objectForKey:#"error"]];
}
}];
You will need to make sure "someIdentifier" is the identifier of the segue in your login scene to your next scene. To create the segue, ctrl drag from your View Controller icon in your login scene to the destination scene and give the segue an identifier.
You only need to create a custom segue if you want to alter the visual appearance of the segue.
I'm having trouble coming up with a solution to the following from the Simple Login iOS quick start:
Since it is possible for a device to have more than one Twitter account attached, you will need to provide a block which can be used to determine which account to log in. Replace "[yourApp selectUserName:usernames]" below with your own code to choose from the list of usernames.
This is the code that is provided:
[authClient loginToTwitterAppWithId:#"YOUR_CONSUMER_KEY"
multipleAccountsHandler:^int(NSArray *usernames) {
// If you do not wish to authenticate with any of these usernames, return NSNotFound.
return [yourApp selectUserName:usernames];
} withCompletionBlock:^(NSError *error, FAUser *user) {
if (error != nil) {
// There was an error authenticating
} else {
// We have an authenticated Twitter user
}
}];
Would a UIActionSheet that allows the user to pick which account to use be best? How would that be done?
#import <Twitter/Twitter.h>
#import <Accounts/Accounts.h>
For login:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
// Request access from the user to use their Twitter accounts.
// [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
//NSLog(#"%#",error);
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
//NSLog(#"%#",accountsArray);
twitterAccountsArray=[accountsArray mutableCopy];
if ([twitterAccountsArray count] > 0){
sheet = [[UIActionSheet alloc] initWithTitle:#"Choose an Account" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
for (ACAccount *acct in twitterAccountsArray) {
[sheet addButtonWithTitle:acct.username];
}
}
}
else{
dispatch_async(dispatch_get_main_queue(), ^{
//NSLog(#"%#",error);
if (![error.localizedDescription isEqual:[NSNull null]] &&[error.localizedDescription isEqualToString:#"No access plugin was found that supports the account type com.apple.twitter"]) {
}
else
[Utility showAlertWithString:#"We could not find any Twitter account on the device"];
});
}
}];
It will open an action sheet which will show you the list of accounts your device has.
For posting :
There must be at least one valid twitter account added on your iPhone device.
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
NSString *initialText=#"Text to be posted";
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
switch(result) {
// This means the user cancelled without sending the Tweet
case SLComposeViewControllerResultCancelled:{
}
break;
case SLComposeViewControllerResultDone:{
}
break;
}
};
[tweetSheet setInitialText:initialText];
if (initialText) {
[self presentViewController:tweetSheet animated:YES completion:nil];
}
}
I've found a nice example of how to do this with Awesome Chat's code. It uses a UIActionSheet like I was attempting to.
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (IBAction)actionTwitter:(id)sender
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[ProgressHUD show:#"In progress..." Interaction:NO];
//---------------------------------------------------------------------------------------------------------------------------------------------
selected = 0;
//---------------------------------------------------------------------------------------------------------------------------------------------
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
//---------------------------------------------------------------------------------------------------------------------------------------------
[account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
{
if (granted)
{
accounts = [account accountsWithAccountType:accountType];
//-------------------------------------------------------------------------------------------------------------------------------------
if ([accounts count] == 0)
[self performSelectorOnMainThread:#selector(showError:) withObject:#"No Twitter account was found" waitUntilDone:NO];
//-------------------------------------------------------------------------------------------------------------------------------------
if ([accounts count] == 1) [self performSelectorOnMainThread:#selector(loginTwitter) withObject:nil waitUntilDone:NO];
if ([accounts count] >= 2) [self performSelectorOnMainThread:#selector(selectTwitter) withObject:nil waitUntilDone:NO];
}
else [self performSelectorOnMainThread:#selector(showError:) withObject:#"Access to Twitter account was not granted" waitUntilDone:NO];
}];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)selectTwitter
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:#"Choose Twitter account" delegate:self cancelButtonTitle:nil
destructiveButtonTitle:nil otherButtonTitles:nil];
//---------------------------------------------------------------------------------------------------------------------------------------------
for (NSInteger i=0; i<[accounts count]; i++)
{
ACAccount *account = [accounts objectAtIndex:i];
[action addButtonWithTitle:account.username];
}
//---------------------------------------------------------------------------------------------------------------------------------------------
[action addButtonWithTitle:#"Cancel"];
action.cancelButtonIndex = accounts.count;
[action showInView:self.view];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
if (buttonIndex != actionSheet.cancelButtonIndex)
{
selected = buttonIndex;
[self loginTwitter];
}
else [ProgressHUD dismiss];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)loginTwitter
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
Firebase *ref = [[Firebase alloc] initWithUrl:FIREBASE];
FirebaseSimpleLogin *authClient = [[FirebaseSimpleLogin alloc] initWithRef:ref];
//---------------------------------------------------------------------------------------------------------------------------------------------
[authClient loginToTwitterAppWithId:TWITTER_KEY multipleAccountsHandler:^int(NSArray *usernames)
{
return (int) selected;
}
withCompletionBlock:^(NSError *error, FAUser *user)
{
if (error == nil)
{
if (user != nil) [delegate didFinishLogin:ParseUserData(user.thirdPartyUserData)];
[self dismissViewControllerAnimated:YES completion:^{ [ProgressHUD dismiss]; }];
}
else
{
NSString *message = [error.userInfo valueForKey:#"NSLocalizedDescription"];
[self performSelectorOnMainThread:#selector(showError:) withObject:message waitUntilDone:NO];
}
}];
}
I am using Parse in my application. I am try to login with Facebook. But I have faced one strange issue. If I am login in Facebook account from my iPhone's settings, then I am getting following error.
Error Domain=com.facebook.sdk Code=2 "The operation couldn’t be completed. (com.facebook.sdk error 2.)" UserInfo=0x17560700 {com.facebook.sdk:ErrorLoginFailedReason=com.facebook.sdk:SystemLoginCancelled, com.facebook.sdk:ErrorInnerErrorKey=Error Domain=com.apple.accounts Code=7 "The operation couldn’t be completed. (com.apple.accounts error 7.)", com.facebook.sdk:ErrorSessionKey=<FBSession: 0x175227e0, state: FBSessionStateClosedLoginFailed, loginHandler: 0x0, appID: 638916992864352, urlSchemeSuffix: , tokenCachingStrategy:<PFFacebookTokenCachingStrategy: 0x175a9900>, expirationDate: (null), refreshDate: (null), attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(null)>}
I am logout from iPhone's Settings->Facebook then its working fine.
I have done research on it, but not getting solutions.
Can anyone help to solve my issue ?
Thanks,
Try with below code..You need to give permissions in the code as in below code..
-(IBAction)facebookButtonAction:(id)sender
{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
self.accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSArray * permissions = [NSArray arrayWithObjects:#"email",#"user_location",#"user_birthday", nil];
NSDictionary * dict=[NSDictionary dictionaryWithObjectsAndKeys:#“xxxxxxxxxxxxxxx,ACFacebookAppIdKey,permissions,ACFacebookPermissionsKey,ACFacebookAudienceEveryone,ACFacebookAudienceKey, nil];
[self.accountStore requestAccessToAccountsWithType:accountType options:dict completion:^(BOOL granted, NSError *error) {
NSLog(#"-------------error =%#--------- check grant bol %d",error,granted);
if(granted ) {
NSArray *accountsArray = [self.accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 0)
{
ACAccount *fbAccount = [accountsArray objectAtIndex:0];
NSDictionary *tempDict = [[NSMutableDictionary alloc] initWithDictionary:[fbAccount dictionaryWithValuesForKeys:[NSArray arrayWithObject:#"properties"]]];
NSString *facebookname;
facebookname=#"Name not found";
facebookname = [[tempDict objectForKey:#"properties"] objectForKey:#"ACPropertyFullName"];
if ([facebookname isKindOfClass:[NSNull class]] || facebookname==NULL || [facebookname isEqualToString:#""] || [facebookname isEqualToString:#"(null)"])
{
facebookname = [[tempDict objectForKey:#"properties"] objectForKey:#"fullname"];
}
//if (SYSTEM_VERSION_LESS_THAN(#"7.0"))
{
}
NSString *facebookID = [NSString stringWithFormat:#"%#", [[fbAccount valueForKey:#"properties"] valueForKey:#"uid"]] ;
NSString *pictureURL=[NSString stringWithFormat:#"https://graph.facebook.com/%#/picture?type=large&return_ssl_resources=1",facebookID];
NSLog(#" check the properties %#",tempDict);
NSLog(#" name nemae %#",facebookname);
NSLog(#" facebookID %#",facebookID);
NSLog(#" image https://graph.facebook.com/%#/picture?type=large&return_ssl_resources=1",facebookID);
if ([facebookname isKindOfClass:[NSNull class]] || facebookname==NULL || [facebookname isEqualToString:#""] || [facebookname isEqualToString:#"(null)"])
{
UIAlertView *somethingerr=[[UIAlertView alloc]initWithTitle:#“App Name” message:#"Something went wrong with facebook login. Please send your console report to the developers." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[somethingerr show
];
[self hidespinner];
return;
}
}
else{
UIAlertView *alerts=[[UIAlertView alloc]initWithTitle:#"No facebok account" message:#"Please add or create an account in \"settings \" " delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alerts show];
[self performSelector:#selector(hidespinner) withObject:nil afterDelay:0.1] ;
}
}
else{
switch ([error code])
{
case 0:
[self showErrorAlertWithMessage:#"Unknown error occured, try again later!"];
break;
case 1:
[self showErrorAlertWithMessage:#"Unknown error occured, try again later!"];
break;
case 3:
[self showErrorAlertWithMessage:#"Authentication failed, try again later!"];
break;
case 6:
[self showErrorAlertWithMessage:#"Facebook account does not exists. Please create it in Settings and come back!"];
break;
case 7:
[self showErrorAlertWithMessage:#"Permission request failed. You won't be able to share information to Facebook"];
break;
default:
[self performSelector:#selector(hidespinner) withObject:Nil afterDelay:0.1];
break;
}
}
}];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No Facebook Accounts" message:#"There are no Facebook accounts configured. You can add or create a Facebook account in Settings." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[self performSelector:#selector(hidespinner) withObject:nil afterDelay:0.1] ;
}
}
Hope it helps you....!
I am building an App according to this tutorial (http://bit.ly/NI9kQe) which uses a custom web api to connect to the web server. One of the requirements is to detect whether or not the Login or Register button has been tapped. This is done using a "tag" which has been set for the button in interface builder (the register button has a tag of 1).
The chunk of code sits inside the btnLoginRegisterTapped method as follows (the error occurs on the line -> NSString* command = (sender.tag==1)?#"register":#"login";):
- (IBAction)btnLoginRegisterTapped:(id)sender {
//form fields validation
if (fldUserName.text.length < 4 || fldPassword.text.length < 4) {
// [UIAlertView error:#"Enter username and password over 4 chars each."];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Opps!!" message:#"Enter username and password over 4 chars each." delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
// optional - add more buttons:
[alert addButtonWithTitle:#"Yes"];
[alert show];
return;
}
//salt the password
NSString* saltedPassword = [NSString stringWithFormat:#"%#%#", fldPassword.text, kSalt];
//prepare the hashed storage
NSString* hashedPassword = nil;
unsigned char hashedPasswordData[CC_SHA1_DIGEST_LENGTH];
//hash the pass
NSData *data = [saltedPassword dataUsingEncoding: NSUTF8StringEncoding];
if (CC_SHA1([data bytes], [data length], hashedPasswordData)) {
hashedPassword = [[NSString alloc] initWithBytes:hashedPasswordData length:sizeof(hashedPasswordData) encoding:NSASCIIStringEncoding];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Opps!!" message:#"Password cannot be reset!" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
// optional - add more buttons:
[alert addButtonWithTitle:#"Yes"];
[alert show];
return;
}
//************ THIS IS WHERE THE ERROR OCCURS *****************//
//check whether it's a login or register
NSString* command = (sender.tag==1)?#"register":#"login";
NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:
command, #"command",
fldUserName.text, #"username",
hashedPassword, #"password",
nil];
//make the call to the web API
[[API sharedInstance] commandWithParams:params
onCompletion:^(NSDictionary *json) {
//handle the response
//result returned
NSDictionary* res = [[json objectForKey:#"result"] objectAtIndex:0];
if ([json objectForKey:#"error"]==nil && [[res objectForKey:#"IdUser"] intValue]>0) {
//success
[[API sharedInstance] setUser: res];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
//show message to the user
[[[UIAlertView alloc] initWithTitle:#"Logged in"
message:[NSString stringWithFormat:#"Welcome %#",[res objectForKey:#"username"] ]
delegate:nil
cancelButtonTitle:#"Close"
otherButtonTitles: nil] show];
} else {
//error
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Opps!!" message:#"Server down? Try Again" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
// optional - add more buttons:
[alert addButtonWithTitle:#"Yes"];
[alert show];
return;
}
}];
}
when I try to build the project (workspace actually) I get the error:
Property 'tag' not found on object of type '_strong id'
I am using xcode 5.0 deploying for iOS7.
Thanks,
Property syntax cannot be used with variables of the generic id type.
So either replace sender.tag by the method call [sender tag] or better,
use the actual type of the sender argument in the method definition:
- (IBAction)btnLoginRegisterTapped:(UIButton *)sender { ... }
Tip: When creating the action with "Control-Drag" in Xcode,
use the pop-up in the "Type" field to select the actual type of the sender.
Then the action method is created with the correct argument type.