UIAlertView供用户下载另一个应用程序

时间:2013-10-06 09:35:44

标签: objective-c uialertview

我从应用程序商店下载了一个应用程序并注意到,一旦我安装并打开了应用程序,我就得到了一个UIAlertView建议我下载另一个应用程序..

我怎样才能做到这一点?是否有我可以指向的现有代码?

看起来像一个简单的功能,但我想整合它。请参阅附件enter image description here

这是我编辑的代码,但“Hello”按钮不会链接到任何地方..我做错了什么?

UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Hello World!"
                                                  message:@"This is your first UIAlertview message."
                                                 delegate:nil
                                        cancelButtonTitle:@"OK"
                                        otherButtonTitles:@"Hello", nil];
[message show];


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Hello"])
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"https://itunes.apple.com/gb/app/islam/id667021108?mt=8&affId=1930871&ign-mpt=uo%3D4"]];
    }
}

2 个答案:

答案 0 :(得分:2)

首先你必须制作alertView,如果你想首先显示警报但是这个代码里面(ViewDidLoad)方法

UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:@"your title"
                      message:@"your message"
                      delegate:self
                      cancelButtonTitle:@"Not now"
                      otherButtonTitles:@"Download now"
                      , nil];
[alert show];

在viewDidLoad之后但是这个方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

if (buttonIndex != [alertView cancelButtonIndex]) {

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"here but your application link"]];
}else{

     }
}

// ============================================= ==== //

要知道什么是NSUserDefaults,我建议看看官方文档。

当然,你可以用它来实现你的目标。您可以使用用户默认值来存储有关应用程序当前运行量的信息。

或多或少喜欢:

BOOL NotTheFirstTime = [[NSUserDefaults standardUserDefaults]     boolForKey:@"NotTheFirstTime"];
if(!NotTheFirstTime){
// Show the alert view
// Then set the first run flag
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NotTheFirstTime"];
[[NSUserDefaults standardUserDefaults] synchronize];
}

如果有帮助,请将其作为最佳问题。

答案 1 :(得分:0)

非常简单:

1)像往常一样设置UIAlertView文本和你喜欢的按钮标题。

2)设置UIAlertView的委托并使用此方法确定单击了哪个按钮(用户是否想要去App Store):

alertView:clickedButtonAtIndex:

3)如果用户想要去App Store,请打开App Store链接:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"<#This is your App Store Link#>"]];