Spring Boot quick start

前言

Spring是一个Java框架,可以用于MicroservicesReactiveCloudWeb AppServerlessEventDrivenBatch等,具有应用广泛、高效、灵活、高速、安全、丰富的社区支持等特性

官网:https://spring.io

当前文章的主要内容是启动一个Spring Boot项目,它具有一个/hello接口,这个接口支持传递name参数,如果没有读取到name参数,则使用默认值的World,并基于name参数返回一个字符串

1、下载demo

在官网http://start.spring.io生成一个demo。

该页面的Project选择MavenSpring Boot选择3.2.3,在Add Dependence中搜索web并选择Spring Web,最后点击下方的Generate按钮就会下在一个demo.zip

spring boot generate

解压后得到的文件结构如下:

demo

2、新增hello函数及接口

这里使用VS Code打开demo,并修改源码如下:

DemoApplication.java

DemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
	@GetMapping("/hello")
	public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
		return String.format("Hello %s", name);
	}
}

介绍一下新增的hello()方法

该方法接收一个String类型的参数name,并用字符串Hello与其拼接,返回拼接之后的字符串。举个例子如果传入的name=Jack,那么最终返回的是一个"Hello Jack"字符串

类名之上@RestController声明意味着当前代码是一个支持web后端代码

方法声明之前@GetMapping("/hello")表示我们的hello()方法是用于响应发送到"http://localhost:8080/hello"地址的请求。

hell0()函数签名中,位于name之前的@RequestParam(value = "name", defaultValue = "World")意味着希望从请求中读取name字段,若未读取到该字段则使用默认的字符串"World"传递给当前函数的参数name

在我们编辑代码的同时会自动导入所需要的包,如顶部的import org.springframework.web.bind.annotation.RestController;等。

3、启动并调试该接口

点击VS Code右上角的三角形▶️按钮即可运行当前程序。运行成功后会在控制台的terminal显示以下内容:

spring boot run

如果需要停止运行,点击以下Disconnect按钮即可:

spring disconnect

在程序运行过程中,我们在浏览器访问"http://localhost:8080/hello",将会看到以下内容:

http://localhost:8080/hello

这表示我们访问本地服务器的/hello接口,并且没有带任何参数。由于我们的hello()方法在没有读取到name参数时会使用默认的"World"字符串当作默认参数,所以最终我们看到浏览器显示了拼接Hello之后的字符串"Hello World"

我们再尝试带上指定的name参数Hsiao:再次访问"http://localhost:8080/hello?name=Hsiao",此时浏览器将显示:

“http://localhost:8080/hello?name=Hsiao”

iOS制作私有库

前言:

在项目使用私有库,能带来以下好处:

  • 代码复用:可以很方便地在多个项目中使用一份代码
  • 模块化:比如可以按照业务模块/公共模块/基础模块将代码分类到不同的库中,使项目整体结构清晰、同时可以减少代码间的耦合,提高代码质量
  • 易于维护:当业务变化时,只需在私有库中更新,然后就能轻松推送到使用这个私有库的所有项目中。
  • 提升开发效率:一次编写,就能在多个项目重化工复用,提高开发效率,节省时间。
  • 持续集成:可以与CI/CD工具集成。私有库更新后可以自动地被推送到所有相关的项目中,让项目都能使用最新版本的库。

1、前期准备

  • 安装并使用CocoaPods

2、创建过程

假设我们的库的名称就叫XPod,只需要在目标文件夹中,使用创建指令

pod lib create XPod

需要设置用户名邮箱

Cloning `https://github.com/CocoaPods/pod-template.git` into `XPod`.
Configuring XPod template.
! Before you can create a new library we need to setup your git credentials.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

 What is your name?
 > XW

! Setting your name in git to XW
  git config user.name "XW"

 What is your email?
 > admin@xiaowen.com

! Setting your email in git to admin@xiaowen.com
  git config user.email "admin@xiaowen.com"

------------------------------

然后根据提示配置lib,如platformlanguage等等,即可完成默认的创建过程

To get you started we need to ask a few questions, this should only take a minute.

2024-03-06 15:45:36.289 defaults[9529:76159]
The domain/default pair of (org.cocoapods.pod-template, HasRunBefore) does not exist
If this is your first time we recommend running through with the guide:
 - https://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and click links to open in a browser. )

 Press return to continue.


What platform do you want to use?? [ iOS / macOS ]
 > iOS

What language do you want to use?? [ Swift / ObjC ]
 > ObjC

Would you like to include a demo application with your library? [ Yes / No ]
 > No

Which testing frameworks will you use? [ Specta / Kiwi / None ]
 > None

Would you like to do view based testing? [ Yes / No ]
 > No

What is your class prefix?
 > X
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

Running pod install on your new library.

Analyzing dependencies
[!] CocoaPods could not find compatible versions for pod "XPod":
  In Podfile:
    XPod (from `../`)

Specs satisfying the `XPod (from `../`)` dependency were found, but they required a higher minimum deployment target.

[!] Automatically assigning platform `iOS` with version `9.3` on target `XPod_Tests` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'XPod/Example/XPod.xcworkspace'
The file /Users/rayvision/Desktop/Code/XPod/Example/XPod.xcworkspace does not exist.

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `https://guides.cocoapods.org/making/making-a-cocoapod`.
➜  Code

创建完成后的目录是这样的:

XPod

3、自定义配置

podspec是文件库的描述文件,默认是这样的:

XPod.podspec default

s.source_files

source_files通常用于指定源代码文件,而不是资源文件(如图片、音频等),如果想包含资源文件,可以使用s.resource_bundless.resources来指定资源文件的路径。

默认情况下是只包含了Classes目录下的文件

s.source_files = 'XPod/Classes/**/*.{h,m}'

如果我们还想用子文件夹开区分不同模块,比如这里新建了一个叫做category的文件夹来放各种分类,UIColor的分类放在UIColor+这个文件夹中,类似地还可以创建一个叫NSString+的文件夹来存放各种NSString的分类

s.source_files = 'XPod/**/*/.{h,m}'
create pod sub directory

这样配置后,XPod及其子目录下的所有.h和.m文件都会被包含在Pod中。下面绿色部分是我们新增的文件夹,红色部分是默认的文件夹。

xcode pod directory

s.resources

使用s.resources可以将指定的资源文件直接复制到生成的Framework静态库中。这意味着这些资源文件会被直接暴露在Bundle的根目录下,可以通过[NSBundle mainBundle] pathForResource:ofType:等方法访问这些资源文件。

s.resource_bundles

使用s.resource_bundles可以将资源文件打包成一个独立的Bundle,并将这个Bundle作为一个整体引入到Pod中。这样做的好处是可以更好地组织资源文件,避免资源文件之间的命名冲突,也可以更方便地加载和管理资源文件。

s.resource_bundles = {
    'XPod' => ['XPod/Assets/**/*.{png, pdf}']
  }

因此,如果你希望将资源文件直接暴露在Bundle的根目录下,可以使用s.resources;如果你希望将资源文件打包成一个独立的Bundle引入到Pod中,可以使用s.resource_bundles。根据你的需求选择合适的方式来包含资源文件。

4、使用

在我们的项目中要使用XPod也很简单只要在Podfile中新增:

 pod 'XPod', :path => '../XPod'

注意这里使用的是相对路径,然后再执行pod install

XPod install

通常情况下,在开发过程中我们都直接使用本地的私有库。当阶段性地完成开发后,可以将私有库放到git上托管。以下是两种不同的引用库的方式:

本地:

  pod 'XPod', :path => '../XPod'

git:

  pod 'XPod', :git => 'https://git.xiaowen.org/ios/XPod.git', :branch => '0.1.0'

WordPress隐藏文章作者日期等信息

文章默认在标题下都会显示相关信息如:

post on by

如果决定这些不好看,希望移除它们,这里有一个很简单的方法:使用自定义css,具体步骤是:

访问WordPress后台,点击:外观 -> 自定义 -> 附加CSS

在编辑框中输入:

.entry-meta .byline, .entry-meta .cat-links { display: none; }
.entry-meta .posted-on { display: none; }

保存即可。

这样在发布的文章标题下就不会再显示发布时间作者了。

post on by hide

Xcode Simulator

升级Xcode之后创建项目提示需要下载iOS 17.2版本的模拟器

需要下载iOS 17.2 Simulator

下载中⌛️

下载中⌛️

等待很久之后报下载失败

下载失败💔

原因是失去了网络连接

失败原因:lost connection

重试很多次依然如此,但是可以排除网络问题。在开发者论坛看到类似的讨论:same issuehttps://forums.developer.apple.com/forums/thread/740040

跳转到苹果官方给出的方案:https://developer.apple.com/documentation/xcode/installing-additional-simulator-runtimes#

install and manager simulator runtimes

最终解决方案是,手动下载然后通过命令行安装

模拟器下载地址:https://developer.apple.com/download/all/ 进入这里需要登录开发者账号。

Apple download all

搜索模拟器iOS Simulator

Apple downloading search simulator

出现很多版本的模拟器,包括iOS17.4 beta,这里我们只需要iOS 17.2,所以我们搜索指定版本

iOS 17.2 Simulator search

找到指定版本,点击View Details即可看到下载runtime.dmg的地址

iOS 17.2 Simulator downloading

点击即可下载⏬。

simulator手动下载中⌛️

这里直接给出具体的下载链接🔗 👇https://download.developer.apple.com/Developer_Tools/iOS_17.2_Simulator_Runtime/iOS_17.2_Simulator_Runtime.dmg

安装方法:

sudo xcode-select -s /Applications/Xcode.app

需要输入密码,然后

xcodebuild -runFirstLaunch

接着:

xcrun simctl runtime add "/Users/rayvision/Downloads/iOS_17.2_Simulator_Runtime.dmg"

这里引号内的地址,可以直接找到下载的文件拖拽过来,会验证runtime。

simulator runtime validate

最后验证完成出现👇就完成了。

D: F56EFCE4-115C-490B-881C-C86B00B23EEB iOS (17.2 - 21C62) (Ready)

simulator add

zsh自动填充插件zsh-autosuggestionszsh

brew安装

brew install zsh-autosuggestions

Logs:

Last login: Wed Mar  6 10:07:19 on ttys000
➜  ~ brew install zsh-autosuggestions
==> Downloading https://formulae.brew.sh/api/formula.jws.json
##O=-#     #
==> Downloading https://formulae.brew.sh/api/cask.jws.json
##O=-#     #
==> Downloading https://ghcr.io/v2/homebrew/core/zsh-autosuggestions/manifests/0.7.0-1
############################################################################################################################################################################## 100.0%
==> Fetching zsh-autosuggestions
==> Downloading https://ghcr.io/v2/homebrew/core/zsh-autosuggestions/blobs/sha256:4537653cbf3540a0785481966b36291255839f1be4012a22409ef97c46a1860b
############################################################################################################################################################################## 100.0%
==> Pouring zsh-autosuggestions--0.7.0.all.bottle.1.tar.gz
==> Caveats
To activate the autosuggestions, add the following at the end of your .zshrc:

  source /opt/homebrew/share/zsh-autosuggestions/zsh-autosuggestions.zsh

You will also need to restart your terminal for this change to take effect.
==> Summary
🍺  /opt/homebrew/Cellar/zsh-autosuggestions/0.7.0: 6 files, 44.8KB
==> Running `brew cleanup zsh-autosuggestions`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

根据上述提示,编辑~/.zshrc:在末尾追加

source /opt/homebrew/share/zsh-autosuggestions/zsh-autosuggestions.zsh

最后,使之生效

source ~/.zshrc

效果:

zsh高亮插件zsh-syntax-highlighting

brew安装

brew install zsh-syntax-highlighting

logs:

Last login: Tue Mar  5 15:41:48 on ttys000
➜  ~ brew install zsh-syntax-highlighting
==> Downloading https://formulae.brew.sh/api/formula.jws.json
############################################################################################################################################################################## 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
############################################################################################################################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/zsh-syntax-highlighting/manifests/0.8.0
############################################################################################################################################################################## 100.0%
==> Fetching zsh-syntax-highlighting
==> Downloading https://ghcr.io/v2/homebrew/core/zsh-syntax-highlighting/blobs/sha256:603dabae4003cd3d95ab7f872a7fd9944e67cf0d963ffe42a07c8f3c191211ea
############################################################################################################################################################################## 100.0%
==> Pouring zsh-syntax-highlighting--0.8.0.arm64_sonoma.bottle.tar.gz
==> Caveats
To activate the syntax highlighting, add the following at the end of your .zshrc:
  source /opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

If you receive "highlighters directory not found" error message,
you may need to add the following to your .zshenv:
  export ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR=/opt/homebrew/share/zsh-syntax-highlighting/highlighters
==> Summary
🍺  /opt/homebrew/Cellar/zsh-syntax-highlighting/0.8.0: 27 files, 203.4KB
==> Running `brew cleanup zsh-syntax-highlighting`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
➜  ~ 

根据上述提示,编辑~/.zshrc:在末尾追加

source /opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

最后,使之生效

source ~/.zshrc

效果:

使用之前
使用之前

使用之后
使用之后

Mac配置CocoaPods

本文介绍安装并使用CocoaPods

CocosPods的官网: https://www.cocoapods.org/

前言

CocoaPods是一个ruby gem,不知道具体是啥暂时也没关系~

安装CocoaPods的完整流程是:1、安装Homebrew -> 2、安装Ruby –> 3、安装并配置CocoaPods

三者之间有一个链式的依赖关系:Homebrew 用于安装 Ruby,然后 Ruby 用于运行 CocoaPods。

所以开始之前,我们先要了解两个东西,分别是Homebrew和Ruby。

1、Homebrew

官网:https://brew.sh/

Homebrew 是 Mac 操作系统上的一个包管理器,它可以让我们更方便地安装、更新和删除软件。Homebrew 可以用来安装 Ruby,也可以安装许多其他类型的软件。

安装Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Logs:

Last login: Tue Mar  5 10:23:16 on ttys000
➜  ~ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
==> Checking for `sudo` access (which may request your password)...
Password:
==> This script will install:
/opt/homebrew/bin/brew
/opt/homebrew/share/doc/homebrew
/opt/homebrew/share/man/man1/brew.1
/opt/homebrew/share/zsh/site-functions/_brew
/opt/homebrew/etc/bash_completion.d/brew
/opt/homebrew

Press RETURN/ENTER to continue or any other key to abort:
==> /usr/bin/sudo /usr/sbin/chown -R rayvision:admin /opt/homebrew
==> Downloading and installing Homebrew...
remote: Enumerating objects: 1215, done.
remote: Counting objects: 100% (886/886), done.
remote: Compressing objects: 100% (270/270), done.
remote: Total 1215 (delta 648), reused 819 (delta 605), pack-reused 329
Receiving objects: 100% (1215/1215), 1.01 MiB | 1.88 MiB/s, done.
Resolving deltas: 100% (682/682), completed with 140 local objects.
From https://github.com/Homebrew/brew
 * [new branch]          build-cask-internal-json-v3 -> origin/build-cask-internal-json-v3
 * [new branch]          dependabot/bundler/Library/Homebrew/json_schemer-2.2.0 -> origin/dependabot/bundler/Library/Homebrew/json_schemer-2.2.0
   96f861e94..cf7c093b3  master     -> origin/master
 * [new branch]          memoize-installed-tap-loading -> origin/memoize-installed-tap-loading
 * [new branch]          sponsors-maintainers-man-completions -> origin/sponsors-maintainers-man-completions
 * [new tag]             4.2.10     -> 4.2.10
 * [new tag]             4.2.11     -> 4.2.11
HEAD is now at cf7c093b3 Merge pull request #16812 from cho-m/formula_auditor-also-check-tag
Updated 3 taps (homebrew/services, homebrew/core and homebrew/cask).
==> Installation successful!

==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics
No analytics data has been sent yet (nor will any be during this install run).

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations

==> Next steps:
- Run brew help to get started
- Further documentation:
    https://docs.brew.sh

if there is a Warning like:

 Warning: /opt/homebrew/bin is not in your PATH.
  Instructions on how to configure your shell for Homebrew
  can be found in the 'Next steps' section below.

Steps

Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
    (echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/hsiao/.zprofile
    eval "$(/opt/homebrew/bin/brew shellenv)"

we should follow the steps with two command:

1、(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/hsiao/.zprofile

2、eval "$(/opt/homebrew/bin/brew shellenv)"

2、Ruby

官网:https://www.ruby-lang.org/en/

Ruby 是一种开源的编程语言,它易于理解和编写。CocoaPods 是用 Ruby 编写的一个gem,因此我们需要在计算机上安装 Ruby 才能使用 CocoaPods。

Mac系统自带的 Ruby 版本通常位于/usr/bin/ruby路径下,但自带的通常是较低版本,不建议用它来开发。建议同时安装最新版本的Ruby,并制定使用我们所安装的版本(当前最新版本为3.3.0)的 Ruby 版本进行开发。

我们可以采用rvm来管理多个不同版本的Ruby,就像flutter也可以使用fvm来管理。

查看默认的ruby:

/usr/bin/ruby -v

Logs:

ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.arm64e-darwin23]

使用homebrew安装ruby

brew install ruby

Logs:

Last login: Tue Mar  5 11:05:10 on ttys001
➜  ~ brew install ruby
==> Downloading https://ghcr.io/v2/homebrew/core/ruby/manifests/3.3.0
############################################################################################################################################################################## 100.0%
==> Fetching dependencies for ruby: libyaml, ca-certificates and openssl@3
==> Downloading https://ghcr.io/v2/homebrew/core/libyaml/manifests/0.2.5
############################################################################################################################################################################## 100.0%
==> Fetching libyaml
==> Downloading https://ghcr.io/v2/homebrew/core/libyaml/blobs/sha256:98c0cf81bcdf7577d5fdc8cc18732970b9ae7e0e7423a733f88f0f566ba483ad
############################################################################################################################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2023-12-12
############################################################################################################################################################################## 100.0%
==> Fetching ca-certificates
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/blobs/sha256:5c99ffd0861f01adc19cab495027024f7d890e42a9e7b689706b85c8e2b9c9b3
############################################################################################################################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/manifests/3.2.1-1
############################################################################################################################################################################## 100.0%
==> Fetching openssl@3
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/blobs/sha256:fce7bf159988bf2b0960fa3d33ea810688d422c433b6461c7020cd0c937827a6
############################################################################################################################################################################## 100.0%
==> Fetching ruby
==> Downloading https://ghcr.io/v2/homebrew/core/ruby/blobs/sha256:f1df6988bf95afc9fd2f0f3c81f5ebbc44c329e0256e0cb9e6d1ff0afa87042c
############################################################################################################################################################################## 100.0%
==> Installing dependencies for ruby: libyaml, ca-certificates and openssl@3
==> Installing ruby dependency: libyaml
==> Downloading https://ghcr.io/v2/homebrew/core/libyaml/manifests/0.2.5
Already downloaded: /Users/rayvision/Library/Caches/Homebrew/downloads/5f6b0af1730aa1bc02f8021c90ed7ffe988eeba95eec83e6c828f77332ba6406--libyaml-0.2.5.bottle_manifest.json
==> Pouring libyaml--0.2.5.arm64_sonoma.bottle.tar.gz
🍺  /opt/homebrew/Cellar/libyaml/0.2.5: 10 files, 352.3KB
==> Installing ruby dependency: ca-certificates
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2023-12-12
Already downloaded: /Users/rayvision/Library/Caches/Homebrew/downloads/13aa86e429e05d02a76005d1881eaf625091a5ac4dc7d7674c706d12ba48796a--ca-certificates-2023-12-12.bottle_manifest.json
==> Pouring ca-certificates--2023-12-12.all.bottle.tar.gz
==> Regenerating CA certificate bundle from keychain, this may take a while...
🍺  /opt/homebrew/Cellar/ca-certificates/2023-12-12: 3 files, 226.7KB
==> Installing ruby dependency: openssl@3
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/3/manifests/3.2.1-1
Already downloaded: /Users/rayvision/Library/Caches/Homebrew/downloads/f7b6e249843882452d784a8cbc4e19231186230b9e485a2a284d5c1952a95ec2--openssl@3-3.2.1-1.bottle_manifest.json
==> Pouring openssl@3--3.2.1.arm64_sonoma.bottle.1.tar.gz
🍺  /opt/homebrew/Cellar/openssl@3/3.2.1: 6,874 files, 32MB
==> Installing ruby
==> Pouring ruby--3.3.0.arm64_sonoma.bottle.tar.gz
==> Caveats
By default, binaries installed by gem will be placed into:
  /opt/homebrew/lib/ruby/gems/3.3.0/bin

You may want to add this to your PATH.

ruby is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ruby first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc

For compilers to find ruby you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/ruby/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/ruby/include"
==> Summary
🍺  /opt/homebrew/Cellar/ruby/3.3.0: 19,656 files, 51.4MB
==> Running `brew cleanup ruby`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> ruby
By default, binaries installed by gem will be placed into:
  /opt/homebrew/lib/ruby/gems/3.3.0/bin

You may want to add this to your PATH.

ruby is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have ruby first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc

For compilers to find ruby you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/ruby/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/ruby/include"
➜  ~

⚠️注意:要将当前Ruby设置为默认版本,不然在下一步安装CocoaPods会报兼容性错误,因为默认ruby版本太低了

默认版本太低报错

ERROR:  Error installing cocoapods:
	The last version of drb (>= 0) to support your Ruby & RubyGems was 2.0.6. Try installing it with `gem install drb -v 2.0.6` and then running the current command again
	drb requires Ruby version >= 2.7.0. The current ruby version is 2.6.10.210.

配置ruby到$PATH环境变量,默认使用当前最新版

echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc
echo 'export LDFLAGS="-L/opt/homebrew/opt/ruby/lib"' >> ~/.zshrc
echo 'export CPPFLAGS="-I/opt/homebrew/opt/ruby/include"' >> ~/.zshrc
source ~/.zshrc

查看当前使用的Ruby版本

login: Tue Mar  5 11:18:48 on ttys002
➜  ~ ruby -v
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]

🏅没错,当前已经使用我们所安装的最新Ruby,版本是3.3.0。

接下来安装CocoaPods

3、CocoaPods

官网:https://www.cocoapods.org/

安装CocoaPods

sudo gem install cocoapods

Logs:

~ sudo gem install cocoapods
Password:
Fetching nanaimo-0.3.0.gem
Fetching colored2-3.1.2.gem
Fetching claide-1.1.0.gem
Fetching CFPropertyList-3.0.7.gem
Fetching atomos-0.1.3.gem
Fetching xcodeproj-1.24.0.gem
Fetching ruby-macho-2.5.1.gem
Fetching nap-1.1.0.gem
Fetching molinillo-0.8.0.gem
Fetching gh_inspector-1.1.3.gem
Fetching fourflusher-2.3.1.gem
Fetching escape-0.0.4.gem
Fetching cocoapods-try-1.2.0.gem
Fetching netrc-0.11.0.gem
Fetching cocoapods-trunk-1.6.0.gem
Fetching cocoapods-search-1.0.1.gem
Fetching cocoapods-plugins-1.0.0.gem
Fetching cocoapods-downloader-2.1.gem
Fetching cocoapods-deintegrate-1.0.5.gem
Fetching ffi-1.16.3.gem
Fetching ethon-0.16.0.gem
Fetching typhoeus-1.4.1.gem
Fetching public_suffix-4.0.7.gem
Fetching fuzzy_match-2.0.4.gem
Fetching concurrent-ruby-1.2.3.gem
Fetching httpclient-2.8.3.gem
Fetching algoliasearch-1.27.5.gem
Fetching addressable-2.8.6.gem
Fetching tzinfo-2.0.6.gem
Fetching i18n-1.14.1.gem
Fetching connection_pool-2.4.1.gem
Fetching activesupport-7.1.3.2.gem
Fetching cocoapods-1.15.2.gem
Fetching cocoapods-core-1.15.2.gem
Successfully installed nanaimo-0.3.0
Successfully installed colored2-3.1.2
Successfully installed claide-1.1.0
Successfully installed CFPropertyList-3.0.7
Successfully installed atomos-0.1.3
Successfully installed xcodeproj-1.24.0
Successfully installed ruby-macho-2.5.1
Successfully installed nap-1.1.0
Successfully installed molinillo-0.8.0
Successfully installed gh_inspector-1.1.3
Successfully installed fourflusher-2.3.1
Successfully installed escape-0.0.4
Successfully installed cocoapods-try-1.2.0
Successfully installed netrc-0.11.0
Successfully installed cocoapods-trunk-1.6.0
Successfully installed cocoapods-search-1.0.1
Successfully installed cocoapods-plugins-1.0.0
Successfully installed cocoapods-downloader-2.1
Successfully installed cocoapods-deintegrate-1.0.5
Building native extensions. This could take a while...
Successfully installed ffi-1.16.3
Successfully installed ethon-0.16.0
Successfully installed typhoeus-1.4.1
Successfully installed public_suffix-4.0.7
Successfully installed fuzzy_match-2.0.4
Successfully installed concurrent-ruby-1.2.3
Successfully installed httpclient-2.8.3
A new major version is available for Algolia! Please now use the https://rubygems.org/gems/algolia gem to get the latest features.
Successfully installed algoliasearch-1.27.5
Successfully installed addressable-2.8.6
Successfully installed tzinfo-2.0.6
Successfully installed i18n-1.14.1
Successfully installed connection_pool-2.4.1
Successfully installed activesupport-7.1.3.2
Successfully installed cocoapods-core-1.15.2
Successfully installed cocoapods-1.15.2
Parsing documentation for nanaimo-0.3.0
Installing ri documentation for nanaimo-0.3.0
Parsing documentation for colored2-3.1.2
Installing ri documentation for colored2-3.1.2
Parsing documentation for claide-1.1.0
Installing ri documentation for claide-1.1.0
Parsing documentation for CFPropertyList-3.0.7
Installing ri documentation for CFPropertyList-3.0.7
Parsing documentation for atomos-0.1.3
Installing ri documentation for atomos-0.1.3
Parsing documentation for xcodeproj-1.24.0
Installing ri documentation for xcodeproj-1.24.0
Parsing documentation for ruby-macho-2.5.1
Installing ri documentation for ruby-macho-2.5.1
Parsing documentation for nap-1.1.0
Installing ri documentation for nap-1.1.0
Parsing documentation for molinillo-0.8.0
Installing ri documentation for molinillo-0.8.0
Parsing documentation for gh_inspector-1.1.3
Installing ri documentation for gh_inspector-1.1.3
Parsing documentation for fourflusher-2.3.1
Installing ri documentation for fourflusher-2.3.1
Parsing documentation for escape-0.0.4
Installing ri documentation for escape-0.0.4
Parsing documentation for cocoapods-try-1.2.0
Installing ri documentation for cocoapods-try-1.2.0
Parsing documentation for netrc-0.11.0
Installing ri documentation for netrc-0.11.0
Parsing documentation for cocoapods-trunk-1.6.0
Installing ri documentation for cocoapods-trunk-1.6.0
Parsing documentation for cocoapods-search-1.0.1
Installing ri documentation for cocoapods-search-1.0.1
Parsing documentation for cocoapods-plugins-1.0.0
Installing ri documentation for cocoapods-plugins-1.0.0
Parsing documentation for cocoapods-downloader-2.1
Installing ri documentation for cocoapods-downloader-2.1
Parsing documentation for cocoapods-deintegrate-1.0.5
Installing ri documentation for cocoapods-deintegrate-1.0.5
Parsing documentation for ffi-1.16.3
Installing ri documentation for ffi-1.16.3
Parsing documentation for ethon-0.16.0
Installing ri documentation for ethon-0.16.0
Parsing documentation for typhoeus-1.4.1
Installing ri documentation for typhoeus-1.4.1
Parsing documentation for public_suffix-4.0.7
Installing ri documentation for public_suffix-4.0.7
Parsing documentation for fuzzy_match-2.0.4
Installing ri documentation for fuzzy_match-2.0.4
Parsing documentation for concurrent-ruby-1.2.3
Installing ri documentation for concurrent-ruby-1.2.3
Parsing documentation for httpclient-2.8.3
Installing ri documentation for httpclient-2.8.3
Parsing documentation for algoliasearch-1.27.5
Installing ri documentation for algoliasearch-1.27.5
Parsing documentation for addressable-2.8.6
Installing ri documentation for addressable-2.8.6
Parsing documentation for tzinfo-2.0.6
Installing ri documentation for tzinfo-2.0.6
Parsing documentation for i18n-1.14.1
Installing ri documentation for i18n-1.14.1
Parsing documentation for connection_pool-2.4.1
Installing ri documentation for connection_pool-2.4.1
Parsing documentation for activesupport-7.1.3.2
Couldn't find file to include 'activesupport/README.rdoc' from lib/active_support.rb
Installing ri documentation for activesupport-7.1.3.2
Parsing documentation for cocoapods-core-1.15.2
Installing ri documentation for cocoapods-core-1.15.2
Parsing documentation for cocoapods-1.15.2
Installing ri documentation for cocoapods-1.15.2
Done installing documentation for nanaimo, colored2, claide, CFPropertyList, atomos, xcodeproj, ruby-macho, nap, molinillo, gh_inspector, fourflusher, escape, cocoapods-try, netrc, cocoapods-trunk, cocoapods-search, cocoapods-plugins, cocoapods-downloader, cocoapods-deintegrate, ffi, ethon, typhoeus, public_suffix, fuzzy_match, concurrent-ruby, httpclient, algoliasearch, addressable, tzinfo, i18n, connection_pool, activesupport, cocoapods-core, cocoapods after 10 seconds
34 gems installed

A new release of RubyGems is available: 3.5.4 → 3.5.6!
Run `gem update --system 3.5.6` to update your installation.

➜  ~

安装过程结束,接下来我们需要初始化pod,这个过程会设置master repo,也就是配置默认的源,但是这个过程也可能异常,稍后会介绍。

初始化pod

pod setup

🤯Oops,发生了一个意外:

zsh:command not found:pod

这个错误通常表示pod命令没有被正确添加到当前的$PATH环境变量中,也就是说,系统找不到pod命令的位置。

我们是通过gem install cocoapods命令安装的 CocoaPods,那么 pod 命令应该位于 Ruby Gems 的bin目录中。但这个目录可能没有被加入到$PATH环境变量中,因此需要手动添加。

依然以 zsh 为例,你需要做的是,打开~/.zshrc文件,然后添加下面这一行:

export PATH="$PATH:$(gem env gemdir)/bin"

或者使用命令行:

echo 'export PATH="$PATH:$(gem env gemdir)/bin"' >> ~/.zshrc
source ~/.zshrc

然后重新打开终端。

查验一下是否已经添加到环境变量中:

cat ~/.zshrc

export PATH="/user/local/bin:$PATH"
export PATH="/opt/homebrew/opt/ruby/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/ruby/lib"
export CPPFLAGS="-I/opt/homebrew/opt/ruby/include"
export PATH="$PATH:$(gem env gemdir)/bin"

包含了export PATH="$PATH:$(gem env gemdir)/bin"就能使用pod指令了。

继续完成初始化工作

pod setup
Setup completed

使用Pod的搜索功能

pod search AFNetworking

搜索遇到问题:

Setup completed
[!]Unable to find a pod with name, author, summary, or description matching 'AFNetworking'

针对该问题,网络上一种

方案A:是认为是search缓存导致的,解决方案:

rm ~/Library/Caches/CocoaPods/search_index.json

实测无效🙅。另一种

方案B:是不管当前问题,直接使用Xcode创建一个iOS项目,并使用pod随便引用一个三方库如AFNetworking,最后执行pod install完成之后,再回到这里就能正常使用pod search功能。本文没尝试这种方案。。。

那该怎么解决呢?先看看pod环境

pod env

Logs:

xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance

解决方案是:

sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer

其实这里补充很关键的一点是,首先,确保已经安装了 Xcode 和 Xcode 命令行工具。如果没有,请在 App Store 中下载并安装 Xcode,并打开终端输入以下命令以安装 Xcode 命令行工具:

xcode-select --install

最后我们再次检查pod环境:pod env,好了,环境已经没有问题了。

  ~ pod env

### Stack

```
   CocoaPods : 1.15.2
        Ruby : ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23]
    RubyGems : 3.5.4
        Host : macOS 14.3.1 (23D60)
       Xcode : 15.2 (15C500b)
         Git : git version 2.39.3 (Apple Git-145)
Ruby lib dir : /opt/homebrew/Cellar/ruby/3.3.0/lib
Repositories :
```

### Installation Source

```
Executable Path: /opt/homebrew/lib/ruby/gems/3.3.0/bin/pod
```

### Plugins

```
cocoapods-deintegrate : 1.0.5
cocoapods-plugins     : 1.0.0
cocoapods-search      : 1.0.1
cocoapods-trunk       : 1.6.0
cocoapods-try         : 1.2.0

到这一步,pod环境已经正常,但在此使用pod的搜索功能还是报错找不到对应的库。

我们查看一下本地已添加的私有仓库

pod repo

😧发现竟然一个都没有:

0 repos

这意味着没有本地仓库,那我们可以使用pod repo add命令添加一个私有仓库。例如,执行pod repo add <repo-name> <repo-url>来添加一个私有仓库。

这里添加官方私有仓库

pod repo add cocoapods https://github.com/CocoaPods/Specs.git

Logs

Cloning spec repo `cocoapods` from `https://github.com/CocoaPods/Specs.git`

这个过程需要等待一段时间⏳,指令结束后我们再次查看本地似有仓库,看到已经包含官方私有仓库了:

pod repo

cocoapods
- Type: git (remotes/origin/master)
- URL:  https://github.com/CocoaPods/Specs.git
- Path: /Users/rayvision/.cocoapods/repos/cocoapods

📌也有安装清华大学的私有仓库,但是本文主打一个能够科学上网、所以未采用该方案。

使用清华源:

pod repo add tuna https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git

[清华源]:https://mirrors.tuna.tsinghua.edu.cn/help/CocoaPods/

安装过程终于快收尾了,再来试试搜索功能:

pod search AFNetworking

没想到还是报一样的错误❌,找不到!!!

😊其实到这里重开一遍终端再次执行搜索指令,就能正常工作了。

或者在设置官方私有仓库之后执行:

pod repo update --verbose

Sandbox: rsync.samba(8351) deny(1)

使用Pod管理第三方库,在编译过程报错:

error: Sandbox: rsync.samba(8351) deny(1) file-write-create /Users/username/Library/Developer/Xcode/DerivedData/X-gslddcmdhniyplhiexxukkwkgtrz/Build/Products/Debug-iphoneos/X.app/Frameworks/AFNetworking.framework/.AFNetworking.s3QZ28 (in target 'X' from project 'X')
error: Sandbox: rsync.samba(8351) deny(1) file-write-create /Users/username/Library/Developer/Xcode/DerivedData/X-gslddcmdhniyplhiexxukkwkgtrz/Build/Products/Debug-iphoneos/X.app/Frameworks/AFNetworking.framework/.Info.plist.lzdXCz (in target 'X' from project 'X')

解决方案:

Build Settings找到ENABLE_USER_SCRIPT_SANDBOXING设置为NO

引用 https://forums.developer.apple.com/forums/thread/731041