You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

@nativescript-community/ui-material-bottomsheet在iOS调用install()时崩溃

解决iOS中MDCBottomSheetControllerDelegate未定义错误

问题根源

你遇到的错误是插件版本与NativeScript核心版本不兼容导致的:

  • 你使用的@nativescript-community/ui-material-bottomsheet和core是6.2.3版本,该版本仅适配NativeScript 6.x;
  • 但你的项目依赖@nativescript/core 7.3.0和@nativescript/ios 8.5.2,属于NativeScript 7+生态,两者版本不匹配导致iOS端无法正确加载Material Components的原生类MDCBottomSheetControllerDelegate。

解决方案

1. 升级Material插件到适配NS7的版本

卸载旧版插件,安装与NativeScript 7兼容的Material插件版本:

# 卸载旧插件
npm uninstall @nativescript-community/ui-material-bottomsheet @nativescript-community/ui-material-core

# 安装适配NS7的版本(7.x系列,与NS7.3.0兼容)
npm install @nativescript-community/ui-material-bottomsheet@^7.2.14 @nativescript-community/ui-material-core@^7.2.14

2. 彻底清理项目环境

清理残留依赖与构建缓存,确保新插件能正确集成:

# 删除依赖、平台、钩子和锁文件
rm -rf node_modules platforms hooks package-lock.json

# 重新安装依赖
npm install

# 执行NativeScript清理命令
ns clean

3. 调整main.ts中iOS端的代码逻辑

将install()调用移到自定义UIApplicationDelegate定义之前,确保Material组件的原生类先被加载:

import { Application } from '@nativescript/core';
import { Theme } from "@nativescript/theme";
import { ios } from '@nativescript/core/application';
import { init } from "@nativescript/background-http";
import { install } from "@nativescript-community/ui-material-bottomsheet";

if (Application.android) {
    init();
    setTimeout(() => {
        Theme.setMode(Theme.Light);
    }, 100);
    install(); // Android端正常执行
} else {
    // 先执行install,确保Material原生类加载完成
    install();

    @NativeClass()
    class MyDelegate extends UIResponder implements UIApplicationDelegate {
        public static ObjCProtocols = [UIApplicationDelegate];
        public ObjCExposedMethods = {
            "runOnBackground": { returns: interop.types.void }
        };
        private bgTask;
        private timer;
        private timerCounter;

        applicationDidFinishLaunchingWithOptions(
            application: UIApplication,
            launchOptions: NSDictionary<any, any>
        ): boolean {
            console.log("applicationWillFinishLaunchingWithOptions: " + launchOptions);
            return true;
        }
        applicationDidBecomeActive(application: UIApplication): void {
            console.log("applicationDidBecomeActive: " + application);
            if (this.timer) {
                clearInterval(this.timer);
            }
        }
        public applicationDidEnterBackground(application: UIApplication) {
            console.log("App entered background.");
            this.bgTask = application.beginBackgroundTaskWithNameExpirationHandler("MyTask", () => {
                this.endBackgroundTask();
            });
            this.timerCounter = 5;
            this.timer = setInterval(() => {
                this.runOnBackground();
            }, 5000);
        }
        private endBackgroundTask(): void {
            this.timerCounter = 5;
            UIApplication.sharedApplication.endBackgroundTask(this.bgTask);
            this.bgTask = UIBackgroundTaskInvalid;
            console.log("End of background task.");
        }
        public runOnBackground(): void {
            if (this.timerCounter <= 0) {
                this.endBackgroundTask();
                return;
            }
            console.log(`${this.timerCounter} (the app is in background)`);
            this.timerCounter--;
        }
    }

    ios.delegate = MyDelegate;
}

4. 确认Angular模块配置

在根模块AppModule中正确导入Material BottomSheet模块:

import { NativeScriptMaterialBottomSheetModule } from "@nativescript-community/ui-material-bottomsheet/angular";

@NgModule({
    imports: [
        // ...其他模块
        NativeScriptMaterialBottomSheetModule.forRoot()
    ]
})
export class AppModule {}

5. 重新构建并运行iOS项目

# 准备iOS平台
ns prepare ios

# 运行iOS项目
ns run ios

内容的提问来源于stack exchange,提问作者Nishad Jahan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 00:30:11