Add Text in TweetSheet when an UIAlert is pressed - iphone

Can anyone tell me how i can add text in TweetSheet when an UIAlert Title button is pressed?
Currently i am using this code to achieve it, but failed so far.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
if ([title isEqualToString:#"Add custom text"]) {
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:#"Add a template" message:nil delegate:self cancelButtonTitle:#"Hi it was nice meeting you" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
if ([title isEqualToString:#"Hi it was nice meeting you"]) {
[tweetSheet setInitialText:#"hey it was nice meeting you"];
}
}
Can anyone tell me what's wrong here?

You're not presenting your TWTweetComposeViewController you can do this simply by adding :
[self presentModalViewController:tweetSheet animated:YES];
I guess this code is only for a simple test tweet.
For a serious project you should always:
Check if the user can send tweets by calling +(BOOL)canSendTweet before trying to show the TWTweetComposeViewController
If not sure about the properties you'll be setting (text, images, urls), you need to check if adding them was successful : -(BOOL)addImage:(UIImage *)image, -(BOOL)addURL:(NSURL *)url and -(BOOL)setInitialText:(NSString *)text all return YES if it was successful and NO if not.
You can set a completion handler to perform different actions depending on what the user decided (send tweet/cancel)
The WWDC session about Twitter integration from 2011 is very simple and detailed, you should watch it and read the documentation if you need more information.

Related

Social sharing url of current webview iPhone. ( Xcode)

so I have my app setup with share action button that shares the current URL to Twitter or Facebook however when I click the Facebook button it shows a Twiiter share sheet. The (Tiwtter option works fine)
After I click the FACEBOOK option, when testing on iPhone the standard twiiter share sheet appears.
- (IBAction)social:(id)sender {
UIActionSheet *share = [[UIActionSheet alloc] initWithTitle:#"Pass on the news!" delegate:self cancelButtonTitle:#"OK" destructiveButtonTitle:nil otherButtonTitles:#"Post to Twitter", #"Post to Facebook", nil];
//You must show the action sheet for the user to see it.
[share showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//Each button title we gave to our action sheet is given a tag starting with 0.
if (actionSheet.tag == 0) {
//Check Twitter accessibility and at least one account is setup.
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *tweetSheet =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet addURL:self.newsItemWebView.request.URL]; //This is setting the initial text for our share card.
[tweetSheet setInitialText:#"Check out this article I found using the 'Pass' iPhone app:, "];
//Brings up the little share card with the test we have pre defind.
[self presentViewController:tweetSheet animated:YES completion:nil];
} else {
//This alreat tells the user that they can't use built in socal interegration and why they can't.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"You can't send a tweet right now, make sure you have at least one Twitter account setup and your device is using iOS6 or above!." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
} else if (actionSheet.tag == 1) {
//Check Facebook accessibility and at least one account is setup.
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *facebookSheet =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebookSheet addURL:self.newsItemWebView.request.URL];
//This is setting the initial text for our share card.
[facebookSheet setInitialText:#"Check out this article I found using the 'Pass' iPhone app:"];
//Brings up the little share card with the test we have pre defind.
[self presentViewController:facebookSheet animated:YES completion:nil];
} else {
//This alreat tells the user that they can't use built in socal interegration and why they can't.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"You can't post a Facebook post right now, make sure you have at least one Facebook account setup and your device is using iOS6 or above!." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
}
You are using actionSheet.tag which will be the same for the entiere actionsheet.
If you want to know which button is pressed you should use buttonIndex in your if statements.
Even better is to use UIActivityViewController.
NSArray *items = #[self.newsItemWebView.request.URL];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
activityViewController.completionHandler = ^(NSString *activityType, BOOL completed) {
// do any thing you want when the user finishes the share activity.
// Like present a message that that activity has completed or not.
};
[self presentViewController:activityViewController animated:YES completion:nil];

How to get user input using cocos2d

I am looking to prompt the user to enter his/her name at the beginning of a game I am building.
What is the best way to get input from the user in cocos2d?
Thank you,
Joey
Cocos2d doesn't have any text input controls but you can easily add UIKit controls to the scene in Cocos2d 2.0
[[CCDirector sharedDirector] view] addSubview:myTextField];
You can use a UIAlertView with a text field embedded.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Done" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
//[alert release]; If not using ARC
To receive the events back from the UIAlertView you implement the UIAlertViewDelegate. In your header file add the delegate protocol to your interface
#interface BTMyScene : CCLayer <UIAlertViewDelegate>
Then in your implementation file add any of the methods from the delegate protocol you want to receive notifications for. You probably want this one
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
UITextField *textField = [alertView textFieldAtIndex:0];
NSString *name = textField.text;
}
I recommend reading the documentation for UIAlertView and UIAlertViewDelegate. You will see all the available methods that you can use.

UIAlertViewDelegate not acting as expected

I have a very simple process running where after each round of a simple game the scores are calculated, labels updated and all the normal, very simple stuff. I have a UIAlertView that informs the player of how s/he performed. I use a UIAlertViewDelegate to postpone all the updates, resetting of controls etc. till after the UIAlertView is dismissed. The methods are [startNewRound],[startOver] and [updateLabels]. It's fairly obvious what they all do. Anyway, when the user hits round ten, I've made another UIAlertView that informs the player that the game has ended and shows the overall score. Again, I hoped to use a delegate to postpone the resets till after the AlertView is dismissed. The only problem is, with the endGame AlertView, it seems to be using the first AlertView's delegate method causing the game to continue with a new round and not start from the beginning. I hope this makes sense. Anyway, here are snippets of my code.
if (round == 10){
UIAlertView *endGame = [[UIAlertView alloc]
initWithTitle: #"End of Game"
message: endMessage
delegate:self
cancelButtonTitle:#"New Game"
otherButtonTitles:nil];
[endGame show];
}
else {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: title
message: message
delegate:self
cancelButtonTitle:#"Next"
otherButtonTitles:nil];
[alertView show];
}
And then the delegate methods:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
[self startNewRound];
[self updateLabels];
}
- (void)endGame:(UIAlertView *)endGame didDismissWithButtonIndex:(NSInteger)buttonIndex
{
[self startOver];
}
So there it is. As I mentioned, the endGame AlertView appears to be using alertView's delegate, thus not activating the [self startOver] method. All the methods are working, it's just the AlertView is using the incorrect delegate method. Regards, Mike
Change your code like this,
if (round == 10){
UIAlertView *endGame = [[UIAlertView alloc]
initWithTitle: #"End of Game"
message: endMessage
delegate:self
cancelButtonTitle:#"New Game"
otherButtonTitles:nil];
endGame.tag = 111;
[endGame show];
}
else {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle: title
message: message
delegate:self
cancelButtonTitle:#"Next"
otherButtonTitles:nil];
alertView.tag = 222;
[alertView show];
}
and delegate method as,
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 111)
{
[self startNewRound];
[self updateLabels];
}
else if(alertView.tag == 222)
{
[self startOver];
}
}
You cant have two delegate method for dismisswithbuttonindex, you need to handle this situation with tag.
Give both alert view a different tag and check it on delegate object. Thus you can differentiat the both alert view.

How can i distinguish which UIAlertView's button was clicked from multiple alertview

In my appln I m using multiple UIAlertView and to determine which between was clicked in alertview i use the method
- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{
}
but i need only for the one alertview but it was responding for all alertviews. how to restrict to only to one alertview or to alertview to which i need determine the button action
Common practice is to use a unique number for the tag property of each UIAlertView, and then check the tag in your delegate callbacks. An easy way to do this is with an enum:
enum {
kServiceErrorAlert = 1,
kFailedToSaveAlert = 2
};
...
alertView.tag = kServiceErrorAlert;
[alertView show];
If you can afford to run on 4.x only, you can use blocks and forget about delegates and tags:
LambdaAlert *alert = [[LambdaAlert alloc]
initWithTitle:#"Test Alert"
message:#"See if the thing works."];
[alert addButtonWithTitle:#"Foo" block:^{ NSLog(#"Foo"); }];
[alert addButtonWithTitle:#"Bar" block:^{ NSLog(#"Bar"); }];
[alert addButtonWithTitle:#"Cancel" block:NULL];
[alert show];
[alert release];
See LambdaAlert on GitHub.

How to create a method to return UIAlertViews?

I have a question about showing an alert on the screen. The thing is: I have an app with 20 to 30 different screens (nibs) And in each nib I do some checking to see if the user has inserted text in a textedit. And some alert messages are identical to others. Like In 3 nibs there is a text field for the user to enter his age, and the an alert that shows up if he left it blank.
What i want to do is to create a method to show these alerts so I don`t need to have the same alert on different nibs. instead of calling an alert view in each nib, I would call the method and pass what kind of alertview to pop up.
What would be the best way to implement this method?
TIA.
You can just alloc init a new UIAlertView as usual but you have to remember to pass the delegate in.
Here is my method:
- (UIAlertView *)getUIAlertViewWithDelegate:(id)delegate title:(NSString *)title cancelTitle:(NSString *)cancel {
return [[[UIAlertView alloc] initWithTitle:title delegate:delegate cancelButtonTitle:cancel otherButtonTitles:nil] autorelease];
}
All right, I managed to do it. Thanks to all that helped. This is my final solution
I created a common.m classs for some methods that I use in various nibs.
Common.h
#interface MetodosGerais : NSObject <UIAlertViewDelegate>{...}
- (void)getUIAlertViewWithDelegate:(id)delegate title:(NSString *)title cancelTitle:(NSString *)cancel;
Common.m
- (void)getUIAlertViewWithDelegate:(id)delegate title:(NSString *)title cancelTitle:(NSString *)cancel {
if (title == #"Enter your Height"){
[[[[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Atention!", #"Atenção!")
message:#"You Should enter Your Height."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil] autorelease] show];
}
else if (title == #"Enter your Age"){
[[[[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Atention!", #"Atenção!")
message:#"You Should enter your Age."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil] autorelease] show];
}
...
and in my classes that I want to use it I did
Common *myAlert = (Common *)[[UIApplication sharedApplication] delegate];
if ([idade.text length] == 0) {
[myAlert getUIAlertViewWithDelegate:self title:#"Enter Your Age" cancelTitle:#"OK"];
}...