在Swift 5中使用可达性

时间:2019-05-06 19:01:47

标签: ios swift cocoapods reachability reachability-swift

我按照本教程尝试在我的项目(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'

Podfile

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

吊舱的安装过程

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.

2 个答案:

答案 0 :(得分:1)

确保打开的文件以.xcworkspace而不是.xcodeproj结尾

答案 1 :(得分:1)

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

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

  3. 构建项目(命令+ B),即使出现错误也是如此。有时您必须构建项目以使Xcode理解要导入一些cocoapods框架。

希望这会有所帮助