在 Swift 5 中使用可达性

Using Reachability in Swift 5

我按照本教程尝试在我的项目中安装并使用可达性 (https://medium.freecodecamp.org/how-to-handle-internet-connection-reachability-in-swift-34482301ea57)

它说它已全部安装,但我的 Import 语句仍然出错。

下图显示了 Podfile、pod 安装过程、项目目标名称和错误。

知道我遗漏了什么吗?我需要检测连接类型(3G、4G、Wifi 等)

NetworkStatus.swift header

import Foundation
import Reachability //No such module 'Reachability'

播客文件

Dans-MacBook-Pro:gnccapp danpalmer$ cat Podfile
# Uncomment the next line to define a global platform for your project
  platform :ios, '12.2'

target 'gnccapp' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for gnccapp
  pod 'ReachabilitySwift'
  pod 'Alamofire'

end

Pods

的安装过程
Dans-MacBook-Pro:gnccapp danpalmer$ pod install
Analyzing dependencies
Setting up CocoaPods master repo
  $ /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress
  Cloning into 'master'...
  remote: Enumerating objects: 317, done.        
  remote: Counting objects: 100% (317/317), done.        
  remote: Compressing objects: 100% (256/256), done.        
  remote: Total 3118566 (delta 105), reused 136 (delta 45), pack-reused 3118249        
  Receiving objects: 100% (3118566/3118566), 646.21 MiB | 1.59 MiB/s, done.
  Resolving deltas: 100% (1856966/1856966), done.
  Checking out files: 100% (326428/326428), done.

CocoaPods 1.7.0.rc.1 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.

For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.7.0.rc.1

Setup completed
Downloading dependencies
Installing ReachabilitySwift (4.3.1)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `GNCC.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

[!] Automatically assigning platform `ios` with version `12.2` on target `gnccapp` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
Dans-MacBook-Pro:gnccapp danpalmer$ ls -l
total 16
drwxr-xr-x@  6 danpalmer  staff  192 May  6 11:50 GNCC.xcodeproj
drwxr-xr-x   3 danpalmer  staff   96 May  6 11:46 GNCC.xcworkspace
-rw-r--r--   1 danpalmer  staff  279 May  6 11:35 Podfile
-rw-r--r--   1 danpalmer  staff  309 May  6 11:46 Podfile.lock
drwxr-xr-x   8 danpalmer  staff  256 May  6 11:46 Pods
drwxr-xr-x   4 danpalmer  staff  128 May  3 10:39 fonts
drwxr-xr-x  20 danpalmer  staff  640 May  6 11:52 gnccapp
Dans-MacBook-Pro:gnccapp danpalmer$ vi Podfile
Dans-MacBook-Pro:gnccapp danpalmer$ pod install
Analyzing dependencies
Downloading dependencies
Installing Alamofire (4.8.2)
Using ReachabilitySwift (4.3.1)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.

确保您打开的文件以 .xc​​workspace 而不是 .xcodeproj 结尾

  1. 确保你打开的是cocoapods生成的xcworkspace文件(白色的)

  2. 清理构建文件夹(shift + command + K)

  3. 构建您的项目 (command + B),即使出现错误。有时你必须构建项目才能 Xcode 明白你正在导入一些 cocoapods 框架。

希望这对您有所帮助