添加 AppDelegate 后应用程序为空

Application is Blank after adding AppDelegate

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

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

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

AppName Would like to send you notification

但之后一切都黑屏了。

我假设是因为下面的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

ID我也换了。在 XCode 日志中它显示了一些警告,它们是

2018-06-29 12:33:16.661 ReachApp[721:311194] DiskCookieStorage changing policy from 2 to 0, cookie file: 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] Called init with app ID: (null) 2018-06-29 12:33:16.934 ReachApp[721:311194] Called init with app ID: Displays_Actual_App_ID_here 2018-06-29 12:33:17.019 ReachApp[721:311194] WARNING: OneSignal has detected that your application delegate implements a deprecated method (application:didReceiveLocalNotification:). Please note that this method has been officially deprecated and the OneSignal SDK will no longer call it. You should use UNUserNotificationCenter instead 2018-06-29 12:33:17.020 ReachApp[721:311194] WARNING: OneSignal has detected that your application delegate implements a deprecated method (application:handleActionWithIdentifier:forLocalNotification:completionHandler:). Please note that this method has been officially deprecated and the OneSignal SDK will no longer call it. You should use UNUserNotificationCenter instead 2018-06-29 12:33:17.020 ReachApp[721:311194] WARNING: OneSignal has detected that your application delegate implements a deprecated method (application:didReceiveLocalNotification:). Please note that this method has been officially deprecated and the OneSignal SDK will no longer call it. You should use UNUserNotificationCenter instead 2018-06-29 12:33:17.028 ReachApp[721:311194] WARNING: OneSignal has detected that your application delegate implements a deprecated method (application:handleActionWithIdentifier:forLocalNotification:completionHandler:). Please note that this method has been officially deprecated and the OneSignal SDK will no longer call it. You should use UNUserNotificationCenter instead

请在您的 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
{

}

我认为有两个不同的问题。黑屏问题的原因可能是应用程序不知道要显示哪个 View Controller。如果您使用的是故事板,则可以将所需的视图控制器设置为 'Initial View Controller'。

或者您可以添加

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

到结尾:

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

就在return YES;

之上