UIMainScreen 不是函数错误 NativeScript iOS

Posted

技术标签:

【中文标题】UIMainScreen 不是函数错误 NativeScript iOS【英文标题】:UIMainScreen is not a function error NativeScript iOS 【发布时间】:2018-09-24 08:56:40 【问题描述】:

我正在尝试在 ios 模拟器上运行我的 NativeScript-Application。由于 iTunes 希望应用程序由 Xcode 9.0 或更高版本发布,我不得不从带有 Xcode 7.3 的 NativeScript iOS v3.1(我的应用程序运行得非常好)升级到带有 Xcode 9.0 的 NativeScript iOS v4.2。现在可悲的是我无法在任何设备上执行我的应用程序。它一直在启动,我看到了加载屏幕,然后遇到了这个错误:

CONSOLE ERROR file:///app/tns_modules/zone.js/dist/zone-node.js:419:27: Unhandled Promise rejection: UIScreen.mainScreen is not a function. (In 'UIScreen.mainScreen()', 'UIScreen.mainScreen' is an instance of UIScreen) ; Zone: angular ; Task: Promise.then ; Value: TypeError: UIScreen.mainScreen is not a function. (In 'UIScreen.mainScreen()', 'UIScreen.mainScreen' is an instance of UIScreen)
CONSOLE ERROR file:///app/tns_modules/zone.js/dist/zone-node.js:421:23: Error: Uncaught (in promise): TypeError: UIScreen.mainScreen is not a function. (In 'UIScreen.mainScreen()', 'UIScreen.mainScreen' is an instance of UIScreen)

当我使用 NativeScript-iOS 3.1.0 回到 Xcode 7.3 时,它再次完美运行。我不知道我能做些什么来解决这个问题。

我已经看到一些与 UIScreen.MainScreen 错误相关的问题(例如here),但它们似乎与本机 iOS 相关,我无法通过 NativeScript 影响(至少我不知道我该怎么做)。

现在这是我的一些配置:

package.json:

 //...
    "tns-ios": 
      "version": "4.2.0"
    
  ,
  "dependencies": 
    "@angular/common": "2.0.0-rc.4",
    "@angular/compiler": "2.0.0-rc.4",
    "@angular/core": "2.0.0-rc.4",
    "@angular/http": "2.0.0-rc.4",
    "@angular/platform-browser": "2.0.0-rc.4",
    "@angular/platform-browser-dynamic": "2.0.0-rc.4",
    "@angular/platform-server": "2.0.0-rc.4",
    "@angular/router": "3.0.0-beta.2",
    "nativescript-angular": "0.3.1-2016-08-04-787",
    "nativescript-calendar": "1.1.2",
    "nativescript-drop-down": "1.3.2",
    "nativescript-fresco": "1.0.6",
    "nativescript-ng2-translate": "1.1.1",
    "nativescript-social-share": "1.3.0",
    "nativescript-telerik-ui": "1.3.1",
    "ng2-translate": "2.2.2",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.6",
    "tns-core-modules": "^2.4.4",
    "typescript": "^1.8.10",
    "url": "0.10.3",
    "zone.js": "0.6.12"
  ,
  "devDependencies": 
    "babel-traverse": "6.10.4",
    "babel-types": "6.10.2",
    "babylon": "6.8.1",
    "filewalker": "0.1.3",
    "lazy": "1.0.11",
    "nativescript-dev-typescript": "0.3.2",
    "nativescript-dev-webpack": "0.0.20",
    "typescript": "1.8.10"
  

build.xcconfig:

CODE_SIGN_IDENTITY = iPhone Developer;
DEVELOPMENT_TEAM = %Number%;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
IPHONEOS_DEPLOYMENT_TARGET = 8.1;

info.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleDisplayName</key>
    <string>%App_Name%</string>
    <key>CFBundleExecutable</key>
    <string>$EXECUTABLE_NAME</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$PRODUCT_NAME</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>2.0.9</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>2.0.9.4</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UIRequiresFullScreen</key>
    <true/>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>armv7</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
    </array>
    <key>CFBundleSupportedPlatforms</key>
    <array>
        <string>iPhoneOS</string>
    </array>
    <key>NSCalendarsUsageDescription</key>
    <string>my app needs access to the calendar to create new entries</string>
    <key>Minimum </key>
    <string></string>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>localhost</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSIncludesSubdomains</key>
                <true/>
            </dict>
            <key>%ip%</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSIncludesSubdomains</key>
                <true/>
            </dict>
        </dict>
    </dict>
</dict>
</plist>

【问题讨论】:

【参考方案1】:

问题是您的 tns-core-modules 是旧版本(大约 2 岁)并且正在使用已弃用的 iOS API,其中 mainScreen 是一个函数(现在它是一个属性)。您只更新了使用新 iOS API 的 tns-ios 运行时,而旧 tns-core-modules 中的代码使用旧 API。简而言之 - 这不起作用,您将不得不更新 tns-core-modulesnativescript-angular + 所有相关的依赖项。您可能还需要在这里和那里更新代码隐藏文件。

package/json 依赖项的升级说明见here

从 2.x.x here987654322@ 迁移时需要考虑的重大更改

另请参阅tns-core-modules here 的更改日志

最后,nativescript-angular 的 CHANGELOG 可以在 here 找到

【讨论】:

以上是关于UIMainScreen 不是函数错误 NativeScript iOS的主要内容,如果未能解决你的问题,请参考以下文章

在 NativeScript 中后台运行任务

Nativescript Vue:访问类函数

如何修复这个 react-native iOS 错误?

为啥导出的模块错误报告定义的函数“不是函数”

reactjs错误-onChange不是函数错误

Angular 中的“toPromise 不是函数”错误是不是有解决方案?