在显示来自 viewDidload 的警报之前显示来自应用程序委托的警报

2024-01-07

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

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

用户从 viewdidload 方法看到警报后,如何从我的应用程序委托调用该方法?

EDIT:

因此,正如评论中所建议的,我添加了一个全局变量,一旦我显示了 ViewDidload 方法中的警报,我将其设置为 true,但我的 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:@"[email protected] /cdn-cgi/l/email-protection", 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;



    }

}

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

在委托中 (...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
    }

}

在你的视图控制器中:

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

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在显示来自 viewDidload 的警报之前显示来自应用程序委托的警报 的相关文章

  • 水平 UICollectionView 单行布局

    我正在尝试使用以下命令设置简单的水平布局UICollectionView 兜圈子却没有达到预期的结果 所以任何指针或例子将不胜感激 我粘贴经常更改的代码但没有成功可能没什么意义 该图像显示两行 第一行是单个项目 尺寸正确并且在中心正确对齐
  • PFQueryTableViewController 错误

    我正在遵循在线教程 使用 Parse 作为后端创建照片共享应用程序 我已经运行了两次教程 两次都从头开始创建应用程序 但在同一位置仍然出现相同的错误 我到处寻找解决方案 但仍然没有运气 我正在使用 PFQueryTableViewContr
  • 如何判断 NSObject 是否具有某个属性?

    假设在 Apple API 1 0 版中 有一个 NSFoo 类 其属性为 color API 1 1 添加了属性 size 我想知道是否可以使用 getter myFoo size myFoo respondsToSelector sel
  • 如何动态获取 UITableViewCell 的高度

    我创建了自定义的tableViewCell 我在UITableViewCell中添加了UIView SubView 所以我在 UIView 中的所有动态文本和图像内容都会根据文本和图像大小而变化 但现在 HeightforRowAtInde
  • 如何使用 CNContacts 快速获取手机号码?

    我有一些代码可以检索用户联系人中的所有电话号码 但只想过滤掉手机号码 目前 我只是通过将第一个数字为 或第二个数字为 7 的数字添加到数组中来实现此目的 如下所示 func findContacts gt CNContact let key
  • 错误消息:您输入的捆绑包 ID 已被使用

    我正在尝试发布一个 iPhone 应用程序 这不是第一个 我过去已经发表过其他的 因此 我在第一个和第二个表单中输入了所需的信息 然后填写了第三个大表单 您还可以在其中上传图标和屏幕截图 好吧 我在上传屏幕截图之前按下了 保存 按钮 因为我
  • 从未调用过交互式委托方法

    我想在 ViewController 1 和 NavigationViewController 2 之间进行交互式转换 NavigationController 通过按钮调用 因此呈现时没有交互转换 它可以通过按钮或 UIPanGestur
  • 如何为 iPhone 6+、6 和 5 指定不同尺寸?

    我想让 iPhone 6 6 和 5 上的视图看起来几乎相同 在附图中 我的意思是 例如 取消 按钮在 iPhone 5 中距离屏幕左边缘应为 30 像素 在 6 中为 35 像素 在 6 中为 45 像素 其他元素也类似 如何为每种类型设
  • 有什么方法可以询问方法的名称吗?

    我正在尝试调试我正在开发的 iPhone 应用程序 向各种源文件添加 50 条 NSLog 语句的想法让我感到很兴奋 我想做的是写一对陈述 比如 NSString methodName self methodName NSLog metho
  • Xamarin - 错误:dsymutil 退出,代码为 72

    最近升级到 VS for Mac 8 10 21 在构建应用程序时 我得到 Xamarin Shared targets 3 3 Error dsymutil exited with code 72 这是 Xcode 13 3 的情况 完整
  • Objective-C 中是否有相当于 C++ 动态转换的功能?

    如果我有两个类 子类和超类 SuperClass super new SuperClass SubClass sub new SubClass SubClass sub pointer The nice one line cast belo
  • 如何按字母顺序对 UITableView 分区进行排序?

    我有一个包含 3 个类别的分段 UITableView 我正在使用这段代码 NSArray arrayOne NSArray arrayWithObjects one two three four nil NSDictionary dict
  • 未知异常和崩溃

    当我尝试快速滚动表格视图或从远程重新加载数据时 我的应用程序崩溃了 当我先进行远程获取然后滚动表格视图时 一切似乎都工作正常 我不知道下面的崩溃日志意味着什么 它只是有时工作正常 有时崩溃 Incident Identifier 710A1
  • 当应用程序进入前台时,如何重新启动基于块的动画?

    我有以下基于块的动画 UIView animateWithDuration 0 5f delay 0 0f options UIViewAnimationOptionRepeat UIViewAnimationOptionAutorever
  • GLKit的GLKMatrix“列专业”如何?

    前提A 当谈论线性存储器中的 列主 矩阵时 列被一个接一个地指定 使得存储器中的前 4 个条目对应于矩阵中的第一列 另一方面 行主 矩阵被理解为依次指定行 以便内存中的前 4 个条目指定矩阵的第一行 A GLKMatrix4看起来像这样 u
  • 如何使用 IOS 12 在 UITableViewCell 中正确添加 UICollectionView

    由于某些原因 在使用 Xcode 10 beta 时 我无法正确显示 tableview 单元格内集合中的某些项目 在过去的四天里我尝试了我所知道的一切 我做了一个小项目样本来看看我的问题是什么 如果有人想在本地运行完整代码 请参见此处 h
  • 通过 Button Swift 中的标签发送行和部分

    我里面有这个cellForRowAtIndexPath cell plusBut tag indexPath row cell plusBut addTarget self action plusHit forControlEvents U
  • 在 Objective-C 中的 Swift 类上调用 NSStringFromClass 返回模块损坏的名称

    我知道这个问题 https stackoverflow com questions 24107658 get a user readable version of the class name in swift in objc nsstri
  • 从现有坐标地图套件中查找最近的位置

    我正在为拥有多家商店的客户开发 iPhone 应用程序 目标 C 我有数组中所有商店 20 的坐标 纬度 长 目前我正在考虑循环遍历商店坐标数组并获取从用户当前位置到商店位置的距离 然后将它们添加到数组中并按最小距离进行排序 这是正确的方法
  • 使用强光混合模式时突出显示伪影

    我正在 iPhone 应用程序中使用顶部图像的 HardLight 混合模式混合两个图像 它看起来像这样 UIGraphicsBeginImageContext size sourceImage drawInRect rectangle b

随机推荐