1

I'm trying to create an actionsheet that will contain the list of twitter account, this is my code.

This is the code of some important methods

//SocialNetworking.m file

- (BOOL)loginWithTwitterCompletionBlock:(UIView *)sender :(void (^)(User *,ACAccount *selAccount, NSError *))completionBlock
{
    ACAccountStore *store = [[ACAccountStore alloc] init];
    ACAccountType *twitterType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    [store requestAccessToAccountsWithType:twitterType withCompletionHandler:^(BOOL granted, NSError *error) {
        NSArray *twitterAccounts = [store accountsWithAccountType:twitterType];

        if(twitterAccounts == nil || [twitterAccounts count] == 0) {
            NSMutableDictionary* details = [NSMutableDictionary dictionary];
            [details setValue:@"Account unavailable" forKey:NSLocalizedDescriptionKey];
            NSError *error=[NSError errorWithDomain:@"authentication" code:10 userInfo:details];

            completionBlock(nil,nil, error);

        } else {

                if (granted && !error)
                    {
                        twitterAccountsArray = [store accountsWithAccountType:twitterType];

                        if ([twitterAccountsArray count] > 1)
                        {

                            dispatch_sync(dispatch_get_main_queue(), ^{
                                [self accountListActionSheetDynamic:twitterAccountsArray Sender:sender];
                            });
                        }
                        else
                        {
                            selectedAccount = [twitterAccounts objectAtIndex:0];
                        }
                    }
NSURL *userDetailsURL=[NSURL URLWithString:@"https://api.twitter.com/1.1/users/show.json"];
//Getting the exception in this line below
                NSDictionary *params = @{@"screen_name" : selectedAccount.username,
                                         @"entities" : @"0"};

            TWRequest *request = [[TWRequest alloc] initWithURL:userDetailsURL parameters:params requestMethod:TWRequestMethodGET];
            [request setAccount:selectedAccount];
            [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
             {
                if (responseData)
                {
                    NSError *error = nil;
                    NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];


                User *socialUser=[[User alloc] init];

                socialUser.twitterName=[responseDict valueForKey:@"name"];
                socialUser.twitterUserName=selectedAccount.username;
                socialUser.twitterProfileId = [responseDict valueForKey:@"id_str"];
                socialUser.twitterProfileImageURLString=[responseDict valueForKey:@"profile_image_url"];
                socialUser.twitterProfileBackgroundURLString=[responseDict valueForKey:@"profile_background_image_url"];


                completionBlock(socialUser,selectedAccount, nil);
            }

            else
                {
                completionBlock(nil,nil, error);
                }
        }];

}

//Action sheet For Multiple TwitterAccounts
- (void)accountListActionSheetDynamic:(NSArray *) accounts Sender:(UIView*) senderView {

    UIActionSheet *sheet = [[UIActionSheet alloc]
                            initWithTitle:@"Choose a Twitter Account"
                            delegate:self
                            cancelButtonTitle:nil
                            destructiveButtonTitle:nil
                            otherButtonTitles:nil];

    for(int i=0;i<accounts.count;i++)
    {
        NSLog(@"i=%d,AccountName:%@",i,[[accounts objectAtIndex:i] valueForKey:@"username"]);
        [sheet addButtonWithTitle:[[accounts objectAtIndex:i] valueForKey:@"username"]]; 
    }
    [sheet addButtonWithTitle:@"Cancel"];
    sheet.cancelButtonIndex = sheet.numberOfButtons-1;

    [sheet showFromRect:senderView.bounds inView:senderView animated:YES];

}


-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == actionSheet.cancelButtonIndex)
    {
        return;
    }
    else
    {

     for(int i=0;i<twitterAccountsArray.count;i++)
        {
            if([[actionSheet buttonTitleAtIndex:buttonIndex] caseInsensitiveCompare:[[twitterAccountsArray objectAtIndex:i] valueForKey:@"username"]]==NSOrderedSame)
            {
                selectedAccount = [twitterAccountsArray objectAtIndex:i];

                return;
            }

        }
    }

}

Now the problem is I'm getting the exception at NSDictionary *params line, because the actionsheet doesnt appear until the whole code completes, is there any way to show the actionsheet before the executions moves to the NSDictionary *params line as the value of selectedAccount is not set yet.

Any suggestion or alternative solutions for this? I'm currently using Xcode 5 and running in simulation iOS 7.

Wain
  • 118,658
  • 15
  • 128
  • 151
Francis F
  • 3,157
  • 3
  • 41
  • 79
  • Put your code handling the account selection into the delegate method: -(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex – imihaly Nov 08 '13 at 10:22

1 Answers1

0

Encapsulate that code into a function with parameter which will be selectedAccount.

Call this function from 2 places:

  1. after selectedAccount = [twitterAccounts objectAtIndex:0];
  2. after selectedAccount = [twitterAccountsArray objectAtIndex:i];
Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71
  • I've thought about that, but the code that needs to be encapsulated contains a completion block which i need to pass in my current method if I write it in separate method what will I DO ABOUT THE COMPLETION BLOCK [completionBlock(socialUser,selectedAccount, nil)] – Francis F Nov 08 '13 at 10:45
  • make a @property copy for it to keep it ? and call later after you have all data ? :) – Grzegorz Krukowski Nov 08 '13 at 10:57
  • how will it handle the completion block statement inside the code? – Francis F Nov 08 '13 at 14:51
  • 1
    self.completionBlock(param1, param2); http://stackoverflow.com/questions/4081831/how-to-store-blocks-in-properties-in-objective-c – Grzegorz Krukowski Nov 08 '13 at 17:05