当我从 main.m 更改为 appdegelegate.swift 时,React-native IOS 应用程序在启动时变为空白
Posted
技术标签:
【中文标题】当我从 main.m 更改为 appdegelegate.swift 时,React-native IOS 应用程序在启动时变为空白【英文标题】:React-native IOS app goes blank on startup when i change from main.m to appdegelegate.swift 【发布时间】:2020-02-16 07:21:54 【问题描述】:我正在尝试通过创建 appdelegate.swift(当然删除这些文件)将 appdelegate.h .m 和 main.m(目标 c 文件)转换为 swift,但是当我这样做时,应用程序会出现黑屏启动
这是 main.m 代码
#import <UIKit/UIKit.h>
#import "AppDelegate.m"
int main(int argc, char * argv[])
@autoreleasepool
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
和 appdelegate.m
#import "AppDelegate.h"
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
@import Firebase;
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
moduleName:@"doctor_react_app"
initialProperties:nil];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
[FIRApp configure];
return YES;
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"
fallbackResource:nil];
#else
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
@end
这是我尝试在 appdelegate.swift 中使用的代码
import UIKit
import PushKit
import Firebase
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
var window: UIWindow?
private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
//Enable all notification type. VoIP Notifications don't present a UI but we will use this to show local nofications later
let notificationSettings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)
//register the notification settings
application.registerUserNotificationSettings(notificationSettings)
//output what state the app is in. This will be used to see when the app is started in the background
NSLog("app launched with state \(application.applicationState)")
FirebaseApp.configure()
return true
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings)
//register for voip notifications
let voipRegistry = PKPushRegistry(queue: DispatchQueue.main)
voipRegistry.desiredPushTypes = Set([PKPushType.voIP])
voipRegistry.delegate = self;
extension AppDelegate: PKPushRegistryDelegate
func pushRegistry(_ registry: PKPushRegistry,
didUpdate pushCredentials: PKPushCredentials,
for type: PKPushType)
//print out the VoIP token. We will use this to test the notification.
NSLog("voip token: \(pushCredentials.token)")
func pushRegistry(_ registry: PKPushRegistry,
didReceiveIncomingPushWith payload: PKPushPayload,
for type: PKPushType,
completion: @escaping () -> Void)
let payloadDict = payload.dictionaryPayload["aps"] as? Dictionary<String, String>
let message = payloadDict?["alert"]
//present a local notifcation to visually see when we are recieving a VoIP Notification
if UIApplication.shared.applicationState == UIApplication.State.background
let localNotification = UILocalNotification();
localNotification.alertBody = message
localNotification.applicationIconBadgeNumber = 1;
localNotification.soundName = UILocalNotificationDefaultSoundName;
UIApplication.shared.presentLocalNotificationNow(localNotification);
else
DispatchQueue.main.async(execute: () -> Void in
let alert = UIAlertController(title: "My Alert", message: "This is an alert.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: _ in
NSLog("The \"OK\" alert occured.")
))
// alert.show()
)
NSLog("incoming voip notfication: \(payload.dictionaryPayload)")
func pushRegistry(_ registry: PKPushRegistry,
didInvalidatePushTokenFor type: PKPushType)
NSLog("token invalidated")
【问题讨论】:
为什么您的application(_:didFinishLaunchingWithOptions:)
被声明为私有?
【参考方案1】:
你可以在这里尝试两件事:-
1:在didFinishLaunchingWithOptions
中设置Root-View-Controller
2:在 ios 13 中,Apple 引入了一个名为 SceneDelegate
的新类,所以所有与窗口相关的东西,你必须在这里实现,现在 SceneDelegate
类正在扩展 UIResponder
,所以只需将根视图控制器设置为SceneDelegate
类的这个函数:-
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)
//set root vc here
【讨论】:
以上是关于当我从 main.m 更改为 appdegelegate.swift 时,React-native IOS 应用程序在启动时变为空白的主要内容,如果未能解决你的问题,请参考以下文章
System.Xml.XmlException '根级别的数据无效,第 1 行,位置 1' 当我从 1 个 xml 文件更改为 5 时出现错误