How to add another button that directs users to Facebook on uiAlert? - ios

I need to add a button Facebook, Then the action should take user to Facebook, I'm just not sure how to do it from the code below?
-(IBAction)btContinueClick:(id)sender{
if(Level == [list count]){
UIAlertView *info = [[UIAlertView alloc]initWithTitle:#"Good" message:#"Go to facebook?" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"Facebook"];
[info show]; //// Change Game End Message
}
}

If you want to go to the native app of Facebook in iOS.
Using delegate method of alertView
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 1) {
NSURL *url = [NSURL URLWithString:#"fb://profile"];
if ([[UIApplication sharedApplication] canOpenURL:url]) { //USE FB NATIVE APP
[[UIApplication sharedApplication] openURL:url];
}
else { //IF THE DEVICE IS UNABLE TO OPEN IN NATIVE APP, USE BROWSER INSTEAD
NSURL *browserURL = [NSURL URLWithString:#"http://www.facebook.com"];
[[UIApplication sharedApplication] openURL:browserURL];
}
}
}

Make sure you have added UIAlertViewDelegate in .h file
-(IBAction)btContinueClick:(id)sender{
if(Level == [list count]){
UIAlertView *info = [[UIAlertView alloc]initWithTitle:#"Good" message:#"Go to facebook?" delegate:self cancelButtonTitle:#"NO" otherButtonTitles:#"Facebook"];
[info show]; //// Change Game End Message
info.delegate=self;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if(buttonIndex==1)
{
NSLog(#"Facebook selected.");
NSURL *url = [NSURL URLWithString:#"fb://profile/<id>"]; // provide the app ID
[[UIApplication sharedApplication] openURL:url];
}
}

Related

Calling an app from within my app does not work

I have the following code:
NSString *customURL = #"photo://";
NSURL *url = [NSURL URLWithString:customURL];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:customURL]])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"URL error"
message:[NSString stringWithFormat:#"No custom URL defined for %#", customURL]
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
Which is trying to open iPhoto app by using the custom URL. But the code returns error message and iPhoto is not launched. Any idea why this is?
Thank you for your help.
photo:// doesn't seem to be a supported url.
https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007899-CH1-SW1

How to call NSURL for Rate My App properly in xcode?

Im trying to put in a rate App alert, but its not opening properly, in fact when I click the Yes button it dosnt open the url at all, How do I make it work?
Below is the call in the viewdidload:
The below code in viewdidload is also in app delegate
[super viewDidLoad];
//rate app
NSUserDefaults *prefsr = [NSUserDefaults standardUserDefaults];
NSInteger launchCount = [prefsr integerForKey:#"launchCount"];
if (launchCount >= 1) {
UIAlertView *alertRate = [[UIAlertView alloc] initWithTitle:#"Like this app?" message:#"Rate on the app store." delegate:nil cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];
[alertRate show];
}
-(void)alertViewRate:(UIAlertView *)alertViewRate clickedButtonAtIndex:(NSInteger)buttonIndexP{
if (buttonIndexP == 2) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
else if (buttonIndexP == 1){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
Add the delegate Self while creating the UIAlertView
UIAlertView *alertRate = [[UIAlertView alloc] initWithTitle:#"Like this app?" message:#"Rate on the app store." delegate:self cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];
[alertRate show];
Delegate method for UIAlertView also need to be chnaged
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 2) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
else if (buttonIndex == 1){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.google.com"]];
}
}
You haven't set the delegate to self while you are creating UIAlertView object. So -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex method is not getting called when you click on buttons in the AlertView.
Change your code From
UIAlertView *alertRate = [[UIAlertView alloc] initWithTitle:#"Like this app?" message:#"Rate on the app store." delegate:nil cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];
To
UIAlertView *alertRate = [[UIAlertView alloc] initWithTitle:#"Like this app?" message:#"Rate on the app store." delegate:self. cancelButtonTitle:#"No, thanks" otherButtonTitles:#"Yes",#"Remind me later", nil];

How to set an action in a UIAlertView?

The UIAlertView Delegate is not getting called. The alert is showing but the buttons do not work. I want one button to open google maps and the other button to open native maps app.
-(IBAction)showAlertView {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Obrir en..."
message:#""
delegate:self
cancelButtonTitle:#"Millor no..."
otherButtonTitles:#"Mapes",#"Google Maps",nil];
[alert show];
}
-(void)showAlertView:(UIAlertView *)alert clickedButtonAtIndex (NSInteger)otherButtonTitles
{
NSString *title = [alert buttonTitleAtIndex:otherButtonTitles];
if([title isEqualToString:#"Mapes"])
{
UIApplication *ourApplication = [UIApplication sharedApplication];
NSString *ourPath = #"http://maps.apple.com/?q=41.11670,1.25812";
NSURL *ourURL = [NSURL URLWithString:ourPath];
[ourApplication openURL:ourURL];
}
if([title isEqualToString:#"Google Maps"])
{
UIApplication *ourApplication = [UIApplication sharedApplication];
NSString *ourPath = #"http://maps.apple.com/?q=41.11670,1.25812";
NSURL *ourURL = [NSURL URLWithString:ourPath];
[ourApplication openURL:ourURL];
}
}
#end
Switch :
-(void)showAlertView:(UIAlertView *)alert clickedButtonAtIndex (NSInteger)otherButtonTitles
to
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

openURL not opening Safari

I am trying to use an action sheet to open safari with a link. The variable is set correct and displays the link accordingly, but for some reason, Safari won't open and I cannot figure out why...
Here's the code:
-(void)actionSheet {
sheet = [[UIActionSheet alloc] initWithTitle:#"Options"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Open in Safari", nil];
[sheet showInView:[UIApplication sharedApplication].keyWindow];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != -1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.url]];
}
}
NSString *strurl = [NSString stringWithFormat:#"http://%#",strMediaIconUrl];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strurl]];
use http:// must.
To open a link in Safari, all you should have to do is the following. urlAddress is a NSString that you set wherever you need it to be set. Alternatively you could replace urlAddress with #"someString".
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: urlAddress]];
Also, have you checked that your header file is implementing the UIActionSheetDelegate protocol?
Edit:
Try the following in your call to see if an error is generated:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != -1) {
NSUrl *myURL = [NSURL URLWithString:self.url];
if (![[UIApplication sharedApplication] openURL:myURL]) {
NSLog(#"%#%#",#"Failed to open url:",[myURL description]);
}
}
}

AlertView inside alertView

A button from the first alert executes the second alert which is basically a confirmation to call someone. I don't get any errors but it doesn't work.
When I press on the second alert the Call button it crashes
-(void) alertView: (UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonString isEqualToString:#"Phone"])
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Notice!" message:#"You are about to call .... Do you wish to continue?" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"Call", nil];
[alert2 show];
if ([buttonString isEqualToString:#"Call"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel://12345678"]]];
}
if([buttonString isEqualToString:#"Website"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"website"]];
}
if ([buttonString isEqualToString:#"Facebook"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.facebook.com/groups/..../"]];
}
}
}
You assigned to delegate as a nil in second alertview. That's why the alertView:clickedButtonAtIndex: delegate method is not called in second time. So you should assign to delegate as a self.
-(void) alertView: (UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex];
if([buttonString isEqualToString:#"Phone"]) {
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Notice!" message:#"You are about to call .... Do you wish to continue?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Call", nil];
[alert2 show];
}
if([buttonString isEqualToString:#"Call"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel://12345678"]]];
}
if([buttonString isEqualToString:#"Website"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"website"]];
}
if([buttonString isEqualToString:#"Facebook"]){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.facebook.com/groups/..../"]];
}
}
I think it will be helpful to you.
Launch the second alert with a delay. Problem is - alert needs some time to hide itself, before any new alerts can appear, thus directly calling second one will not work.
try this for example:
- (void) alertView: (UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonString isEqualToString:#"Phone"])
{
[self performSelector:#selector(callSecondAlertView) withObject:nil afterDelay:1];
}
}
- (void)callSecondAlertView
{
//show new alert view
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Notice!" message:#"You are about to call .... Do you wish to continue?" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"Call", nil];
[alert2 show];
[alert2 release];
}
If not working, or too long delay - play with afterDelay: value.
got it! for some reason the [[[alertView subviews] objectAtIndex:6] setBackgroundColor... for the fisrt alertView buttons i had interferes with the second alertView functionality. xcode though did not direct me directly to the problem until i updated xcode to 4.4.1
Use the alertView:didDismissWithButtonIndex: delegate method instead of the alertView:clickedButtonAtIndex: delegate method. The former is called after the alert is gone. This make more sense when you want to show a second based on the tapped button of the first alert view.
Also give tag to UIAlertview objects.

Resources