gpt4 book ai didi

ios - 在显示来自 viewDidload 的警报之前显示来自应用程序委托(delegate)的警报

转载 作者:可可西里 更新时间:2023-11-01 04:34:33 24 4
gpt4 key购买 nike

我正在尝试通过 parse.com 文档中概述的应用程序委托(delegate)显示推送通知中包含的消息。

我遇到的问题是,在我的第一个 View Controller 的 viewdidload 方法中,我呈现了一个警告,用户在使用该应用程序之前必须看到该警告。

在用户看到来自 viewdidload 方法的警报后,如何从我的应用程序委托(delegate)中调用该方法?

编辑:

所以我已经按照评论中的建议添加了一个全局变量,一旦我显示了来 self 的 ViewDidload 方法的警报,我将其设置为 true,但是来 self 的 appDelegate 的通知警报仍然没有出现。

这是我的应用程序 delegate.m 文件:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[Parse setApplicationId:@"xxxxxxxxxxxxxxxx"
clientKey:@"xxxxxxxxxxxx"];

// Register for Push Notitications, if running iOS 8
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes
categories:nil];
[application registerUserNotificationSettings:settings];
[application registerForRemoteNotifications];
} else {
// Register for Push Notifications before iOS 8
[application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound)];
}
return YES;



NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];


if (Notification == true) {
if (![pushText isEqual: @""]) {
pushText = [[notificationPayload objectForKey:@"aps"] objectForKey:@"alert"];
UIAlertView *alert_news = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"News", "")
message:pushText
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles: nil];
[alert_news show];


}
}


}

这是我的 viewdidload 方法:

 RoadSafetyAppAppDelegate *AppDelegate;

- (void)viewDidLoad
{
AppDelegate = (RoadSafetyAppAppDelegate *)[[UIApplication sharedApplication] delegate];
[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.



backgroundImage.alpha = 0.3;
toRecipients = [[NSArray alloc]initWithObjects:@"records@shellharbour.nsw.gov.au", nil];
static int appCounter;
if ( appCounter < 1 ) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Disclaimer", "")
message:NSLocalizedString(@"Using a mobile phone whilst driving is against the law. Ensure that you are not behind the wheel when using this app.", "")
delegate:nil
cancelButtonTitle:@"I agree to not use a mobile phone while driving"
otherButtonTitles: nil];
[alert show];
appCounter = appCounter+1;

AppDelegate.NotificationAlert = @"1";
AppDelegate.Notification = true;



}

}

最佳答案

因为您想一次性显示免责声明,并确保用户在显示任何通知之前看到它并贴在同意按钮上。您可以使用简单的本地通知来做到这一点。

在委托(delegate)中(...didFinishLaunchingWithOptions:)

 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

//......you code here
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"disclaimerShown"]==nil)

[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"disclaimerShown"];
[[NSUserDefaults standardUserDefaults] synchronize];
}


//......you code here

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"disclaimerShown"]){ //YES

if (![pushText isEqual: @""]) {
pushText = [[notificationPayload objectForKey:@"aps"] objectForKey:@"alert"];
UIAlertView *alert_news = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"News", "")
message:pushText
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles: nil];
[alert_news show];


}


}
}


-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

NSString *value=[NSString stringWithFormat:@"%@",[notification.userInfo valueForKey:@"key"]];
if ([value isEqualToString:@"disclaimerShown"]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"disclaimerShown"];
[[NSUserDefaults standardUserDefaults] synchronize];
///continue handle parse.com notification
}

}

在你的 ViewController 中:

-(void)viewDidLoad{
//...


if ([[NSUserDefaults standardUserDefaults] boolForKey:@"disclaimerShown"]==NO){

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Disclaimer", "")
message:NSLocalizedString(@"Using a mobile phone whilst driving is against the law. Ensure that you are not behind the wheel when using this app.", "")
delegate:nil
cancelButtonTitle:@"I agree to not use a mobile phone while driving"
otherButtonTitles: nil];
alert.tag = 1;
[alert show];
}


//...
}

编译指示标记 - UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1) {//the disclaimer alert
if (buttonIndex == 0) {
UILocalNotification *alarm = [[UILocalNotification alloc] init];
alarm.userInfo = @{@"key": @"disclaimerShown"};
alarm.fireDate = [NSDate date];
alarm.timeZone = [NSTimeZone defaultTimeZone];

[[UIApplication sharedApplication] scheduleLocalNotification:alarm];
}
}

}

关于ios - 在显示来自 viewDidload 的警报之前显示来自应用程序委托(delegate)的警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27240181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com