【问题标题】:Application is Blank after adding AppDelegate添加 AppDelegate 后应用程序为空白
【发布时间】:2018-12-08 07:37:00
【问题描述】:

我在 Cordova 中有一个 iOS 应用程序,它使用 OneSignal 实现了推送通知。

我已按照here给出的文档中的所有步骤进行操作

问题是,当我使用 XCode 安装应用程序时,应用程序会询问,

AppName 想给你发送通知

但在那之后一切都黑屏了。

我认为这是因为 AppDelegate.m 文件如下

/*
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
 */

//
//  AppDelegate.m
//  ReachApp
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//  Copyright ___ORGANIZATIONNAME___ ___YEAR___. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"

#import <OneSignal/OneSignal.h>

@implementation AppDelegate

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

    // Replace '11111111-2222-3333-4444-0123456789ab' with your OneSignal App ID.
    [OneSignal initWithLaunchOptions:launchOptions
                               appId:@"11111111-2222-3333-4444-0123456789ab"
            handleNotificationAction:nil
                            settings:@{kOSSettingsKeyAutoPrompt: @false}];
    OneSignal.inFocusDisplayType = OSNotificationDisplayTypeNotification;

    // Recommend moving the below line to prompt for push after informing the user about
    //   how your app will use them.
    [OneSignal promptForPushNotificationsWithUserResponse:^(BOOL accepted) {
        NSLog(@"User accepted notifications: %d", accepted);
    }];

    return YES;
}

@end

我也更换了身份证。并且在 XCode 日志中显示很少的警告,即

2018-06-29 12:33:16.661 ReachApp[721:311194] DiskCookieStorage 将策略从 2 更改为 0,cookie 文件: file:///private/var/mobile/Containers/Data/Application/4C0D3ECB-1F40-4668-BE78-AE1EF3CB7810/Library/Cookies/Cookies.binarycookies 2018-06-29 12:33:16.838 ReachApp[721:311194] 使用应用 ID 调用初始化: (null) 2018-06-29 12:33:16.934 ReachApp[721:311194] 调用初始化 应用 ID:Displays_Actual_App_ID_here 2018-06-29 12:33:17.019 ReachApp[721:311194] 警告:OneSignal 检测到您的 应用程序委托实现了一个不推荐使用的方法 (应用程序:didReceiveLocalNotification :)。请注意,这 方法已被正式弃用,OneSignal SDK 将不会 再叫它。您应该改用 UNUserNotificationCenter 2018-06-29 12:33:17.020 ReachApp [721:311194] 警告:OneSignal 有 检测到您的应用程序委托实现了不推荐使用的方法 (应用程序:handleActionWithIdentifier:forLocalNotification:completionHandler :)。 请注意,此方法已被正式弃用,并且 OneSignal SDK 将不再调用它。你应该使用 UNUserNotificationCenter 改为 2018-06-29 12:33:17.020 ReachApp[721:311194] 警告:OneSignal 检测到您的 应用程序委托实现了一个不推荐使用的方法 (应用程序:didReceiveLocalNotification :)。请注意,这 方法已被正式弃用,OneSignal SDK 将不会 再叫它。您应该改用 UNUserNotificationCenter 2018-06-29 12:33:17.028 ReachApp [721:311194] 警告:OneSignal 有 检测到您的应用程序委托实现了不推荐使用的方法 (应用程序:handleActionWithIdentifier:forLocalNotification:completionHandler :)。 请注意,此方法已被正式弃用,并且 OneSignal SDK 将不再调用它。你应该使用 改为 UNUserNotificationCenter

【问题讨论】:

    标签: ios objective-c onesignal


    【解决方案1】:

    我认为有两个不同的问题。黑屏问题的原因可能是应用程序不知道要显示哪个 View Controller。如果您使用情节提要,您可以将所需的视图控制器设置为“初始视图控制器”。

    或者你可以添加

    self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible];

    到结尾:

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

    就在return YES;上方

    【讨论】:

      【解决方案2】:

      请在您的 appdelegate 中添加以下方法来处理通知。

         -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
      {
          NSLog(@"Application registered for notification");
      }
      
      -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
      {
          NSLog(@"Failed to register notification for application");
      }
      
      -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
      {
      
      }
      
      -(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
      {
      
      }
      
      -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
      {
      
      }
      

      【讨论】:

      • 能否提供Objective-C的相关代码。
      • 我已经在AppDelegate中添加了代码,仍然黑屏。
      • 是的,xcode 警告仍然存在。我发布的代码是我的 AppDelegate 的完整代码。
      • 更改应用程序设置。转到设置 -> YourApplication -> 通知 -> 允许通知 - SetON
      猜你喜欢
      • 2023-03-17
      • 2023-02-01
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多