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 :-)