我的系统:
MacOS: Big Sur
XCode: Version 12.5.1 (12E507)
iOS Simulator: iPhone 8 (iOS 14.5)
Node: v16.10.0
我创建了新的 React Native 项目:
npx react-native@latest init MyFreshProject
创建成功。
npm run ios
在iOS模拟器上运行成功。
然后我安装了
@react-native-community/geolocation
(^3.0.6)。我将以下代码添加到Info.plist
中:
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string></string>
并在
App.tsx
中添加了以下代码:
import Geolocation from '@react-native-community/geolocation';
...
...
useEffect(() => {
Geolocation.getCurrentPosition(info => console.log(info));
}, []);
这些更改之后,我再次运行
npm run ios
并构建失败并输出:
...
...
...
/Users/aberikashvili/Documents/.../MyFreshProject/node_modules/@react-native-community/geolocation/ios/RNCGeolocation.mm:393:23: error: property 'sourceInformation' not found on object of type 'CLLocation *'
mocked = location.sourceInformation.isSimulatedBySoftware;
^
1 error generated.
** BUILD FAILED **
The following build commands failed:
CompileC /Users/aberikashvili/Library/Developer/Xcode/DerivedData/MyFreshProject-cviktleltpvwpcdkzpdqowhphiwn/Build/Intermediates.noindex/Pods.build/Debug-iphonesimulator/react-native-geolocation.build/Objects-normal/x86_64/RNCGeolocation.o /Users/aberikashvili/Documents/.../MyFreshProject/node_modules/@react-native-community/geolocation/ios/RNCGeolocation.mm normal x86_64 objective-c++ com.apple.compilers.llvm.clang.1_0.compiler
(1 failure)
我尝试注释掉
Geolocation
中的 App.tsx
用法,但构建仍然失败。
有什么想法吗?
附注这个问题已经折磨我好几天了
实际问题是,
CLLocation Source Information
支持iOS 15+,而iOS 15本身需要XCode 13+(我的是12)。
所以,解决方案是将 MacOS 从
Big Sur
更新到 Ventura
,安装最新的 XCode 就可以了