在 Swift 5 中使用可达性 [重复]

Posted

技术标签:

【中文标题】在 Swift 5 中使用可达性 [重复]【英文标题】:Using Reachability in Swift 5 [duplicate] 【发布时间】:2019-05-06 19:01:47 【问题描述】:

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

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

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

任何想法我错过了什么?我需要检测连接类型(3G、4G、Wifi 等)

NetworkStatus.swift 标头

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.

【问题讨论】:

您打开的是工作区文件而不是项目文件? 【参考方案1】:

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

【讨论】:

谢谢先生,就是这样。太习惯打开项目了,我没有看到另一个。【参考方案2】:

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

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

    构建您的项目(command + B),即使出现错误。有时您必须将项目构建到 Xcode 才能了解您正在导入一些 cocoapods 框架。

希望对你有帮助

【讨论】:

以上是关于在 Swift 5 中使用可达性 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

使用 SWIFT 检查 IP 地址可达性

Swift - 可达性崩溃

Xcode 8 e Swift 3 中的可达性错误

Swift 3 通过可达性检查互联网连接

swift 3(xCode8-beta6)上的可达性问题

Swift 中的 AFNetworking 可达性管理器