Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-12-2011, 07:10 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 2
sinnis is on a distinguished road
Default Twitter - AlertView blocks UI

Hey guys,

I'm new to iOS development since my employer wanted to do some stuff on iPad and iPhone. (iOS 5)
Till now I've only developed applications and so on in c#, which I've learned some years ago. So objective-c is absolutely new to me.
So I've started in some programming tutorials last week, which I've found in apples dev-center. Especially the twitter sample (Sample Code) interested me.
I changed the code a bit to understand the syntax a little bit more.

Code:
- (IBAction)customTweet:(id)sender {
    // Create an account store object.
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    
    // Create an account type that ensures Twitter accounts are retrieved.
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    
    // Request access from the user to use their Twitter accounts.
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
        if(granted) {
            // Get the list of Twitter accounts.
            NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
            
            //check how many accounts exists and handle each case
            if ([accountsArray count] == 1) {
                // Grab the first and only account
                ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
                
                // Send Tweet to Twitter with specified account and text.
                [self SendTweetTo:twitterAccount with: [myTweet text]];
                
            }
            else if([accountsArray count] > 1) {
                //Do other stuff
            }
            else {
                UIAlertView *alertWindow = [[UIAlertView alloc]initWithTitle:@"No Account" message:@"Sorry dude, there is no twitter-account specified yet." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
                [alertWindow show];
            }
        }
        
    }];    
}
void(^performRequest)(NSData*, NSHTTPURLResponse*, NSError*);

-(void)SendTweetTo:(ACAccount*)account with:(NSString*)tweet {
    
    TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters: [NSDictionary dictionaryWithObject:tweet forKey:@"status"] requestMethod: TWRequestMethodPOST];
    
    performRequest = ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        
        NSString *output = [NSString stringWithFormat:@"HTTP Response with %i", [urlResponse statusCode]];
        
        UIAlertView *alertWindow = [[UIAlertView alloc]initWithTitle:@"Result" message:output delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        
        [alertWindow show];
        //[self performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO];

    };
    
    [request setAccount:account];
    [request performRequestWithHandler:performRequest];
}
So if I type a tweet and send it to Twitter I want to have a alert-window popping up after the the request is finished and received the status, while the UI should be accessible. In my code the tweet is sent, the UI is blocked (I can't do anything) and after I received the status the alert-window opens. How can I accomplish that?
In C# I would say, I have to implement an EventHandler that recognizes the reception of the status and as a result of this open the alert-window.
How can I do that in objective-C? Any ideas?

Thanks in advance :-)

Last edited by sinnis; 12-12-2011 at 07:13 AM.
sinnis is offline   Reply With Quote
Old 12-12-2011, 09:09 AM   #2 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 2
sinnis is on a distinguished road
Default

Ok, I think I figured it out.

Code:
-(void)SendTweetTo:(ACAccount*)account with:(NSString*)tweet {
    
    TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters: [NSDictionary dictionaryWithObject:tweet forKey:@"status"] requestMethod: TWRequestMethodPOST];
    
    
    performRequest = ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        
        if([urlResponse statusCode]){
            
                NSString *output = [NSString stringWithFormat:@"HTTP Response with %i", [urlResponse statusCode]];
                [self performSelectorOnMainThread:@selector(ShowAlertWindow:) withObject:output waitUntilDone:NO];
        }
    };
    
    [request setAccount:account];
    [request performRequestWithHandler:performRequest];
}

-(void) ShowAlertWindow:(NSString*)text {
    UIAlertView *alertWindow = [[UIAlertView alloc]initWithTitle:@"Result" message:text delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    
    [alertWindow show];
}
I've called the function performSelectorOnMainThread with the function ShowAlertWindow as Selector.

Thread can be closed, if necessary.
sinnis is offline   Reply With Quote
Reply

Bookmarks

Tags
alert window, ios 5, twitter

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 403
14 members and 389 guests
7twenty7, AppsBlogger, Creativ, David-T, Duncan C, HemiMG, heshiming, LunarMoon, Murphy, pbart, sacha1996, Sami Gh, teebee74, Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,915
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:48 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0