如何在uiAlert上添加另一个将用户引导到Facebook的按钮?

时间:2014-04-28 06:43:42

标签: ios

我需要添加一个按钮Facebook,然后该操作应该将用户带到Facebook,我只是不确定如何从下面的代码中执行此操作?

-(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
    }
}

2 个答案:

答案 0 :(得分:1)

如果你想在iOS中使用Facebook的原生应用程序。

使用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];
    }
  }
}

答案 1 :(得分:0)

确保您已在.h文件中添加了UIAlertViewDelegate

-(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];


    }
}