Đầu tiên bạn cần tải sources code này về bằng dòng lệnh:
https://github.com/aryaxt/iOS-Rich-Text-Editor
Bây giờ ta thấy source code như sau:
Sau khi phân tích thì mình nhận thấy các vấn đề sau:
Source code này không có ARC – tự động dọn dẹp bộ nhớ, vì nó tạo cách đây 8 năm, cái thời xcode còn chưa hỗ trợ -> nên cần xóa toàn bộ các lệnh release, retain hay autorelease..
Source đang để nhiều thư mục. Muốn tiến hành làm library và viết bớt script thì đưa vào 2 thư mục chính là Classes(chứa files .h và .m) và Assets(chứa các file ảnh).
Có bug không đọc được ảnh khi chuyển sang project lib của mình.
Vậy chúng ta sẽ tiến hành tạo mới 1 framework bằng Xcode như sau:
file -> new project -> Framework
Đặt tên nó là QRichTextView(hoặc tên gì tùy bạn).
Tiếp theo tạo 2 thư mục chính Classes và Assets như hình:
Sau đó sang source code của họ để copy toàn bộ các file .h và .m từ các thư mục Source, Categories, iphone Popover vào thư mục Classes của chúng ta. Làm tương tự với các file .png vào thư mục Assets.
Bấm build thì được 1 đống lỗi. Những đoạn sau sẽ sinh ra lỗi:
Xóa và sửa lại đơn giản hơn:
Các bạn tiến hành xóa và sửa hết, rồi build thành công. Tuy nhiên khi dùng framework này sẽ phải sửa thêm 1 đoạn code như sau để nó đọc được hình ảnh từ thư mục Asset sang:
Đoạn code trên cần chỉ rõ đọc ảnh từ framework bundle, nếu không hình ảnh sẽ không load được.
Bây giờ làm sao để build cho cả simulator và device? Mình sẽ tạo thêm 1 target gọi tên là universal như sau:
Chọn file -> new target -> Aggregate và đặt tên là QRichTextView-Univesal như hình:
Tiếp theo bạn vào target này và chọn build Phases -> new run script phases và copy đoạn code dưới đây:
# Sets the target folders and the final framework product.
FMK_NAME=QRichTextView
FMK_VERSION=1.0# Install dir will be the final output to the framework.# The following line create it in the root folder of the current project.
INSTALL_DIR=${SRCROOT}/Products/${FMK_NAME}.framework
# Working dir will be deleted after the framework creation.
WRK_DIR=build
DEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.framework
SIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator/${FMK_NAME}.framework
# Building both architectures.
xcodebuild -configuration "Release"-target "${FMK_NAME}"-sdk iphoneos
xcodebuild -configuration "Release"-target "${FMK_NAME}"-sdk iphonesimulator
# Cleaning the oldest.if[-d "${INSTALL_DIR}"]then
rm -rf "${INSTALL_DIR}"fi# Creates and renews the final product folder.
mkdir -p "${INSTALL_DIR}"
mkdir -p "${INSTALL_DIR}/Versions"
mkdir -p "${INSTALL_DIR}/Versions/${FMK_VERSION}"
mkdir -p "${INSTALL_DIR}/Versions/${FMK_VERSION}/Resources"
mkdir -p "${INSTALL_DIR}/Versions/${FMK_VERSION}/Headers"# Creates the internal links.# It MUST uses relative path, otherwise will not work when the folder is copied/moved.
ln -s "${FMK_VERSION}""${INSTALL_DIR}/Versions/Current"
ln -s "Versions/Current/Headers""${INSTALL_DIR}/Headers"
ln -s "Versions/Current/Resources""${INSTALL_DIR}/Resources"
ln -s "Versions/Current/${FMK_NAME}""${INSTALL_DIR}/${FMK_NAME}"# Copies the headers and resources files to the final product folder.
cp -R "${DEVICE_DIR}/Headers/""${INSTALL_DIR}/Versions/${FMK_VERSION}/Headers/"
cp -R "${DEVICE_DIR}/""${INSTALL_DIR}/Versions/${FMK_VERSION}/Resources/"# Removes the binary and header from the resources folder.
rm -r "${INSTALL_DIR}/Versions/${FMK_VERSION}/Resources/Headers""${INSTALL_DIR}/Versions/${FMK_VERSION}/Resources/${FMK_NAME}"# Uses the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.
lipo -create "${DEVICE_DIR}/${FMK_NAME}""${SIMULATOR_DIR}/${FMK_NAME}"-output "${INSTALL_DIR}/Versions/${FMK_VERSION}/${FMK_NAME}"
rm -r "${WRK_DIR}"
Repository của bạn cần có 2 file README and MIT License. Bạn làm như hình:
Sau đó thêm tên là README và LICENSE. Github sẽ gợi ý bạn chọn MIT license..
Viết unit test cho project
Cái này mình không có thời gian nên không làm.
Configure Travis CI
Ở đây bạn có thể vào trang chủ Travis CI để làm theo hướng dẫn. Có thể bỏ qua nếu chưa cần thiết.
Public thư viện
Bạn cần cài đặt CocoaPod để đẩy lên. Nếu chưa cài hãy gõ lệnh:
gem install cocoapods
Sau đó gõ lệnh:
pod spec create QRichTextView
File này chứa thông tin về version, tên thư viện, các source code, Asset, thông tin tác giả… để public thư viện lên cho cộng đồng. Bạn có thể sửa như sau:
## Be sure to run `pod spec lint QRichTextView.podspec' to ensure this is a# valid spec and to remove all comments including this before submitting the spec.## To learn more about Podspec attributes see https://guides.cocoapods.org/syntax/podspec.html# To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/#Pod::Spec.newdo|spec|# ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― ### These will help people to find your library, and whilst it# can feel like a chore to fill in it's definitely to your advantage. The# summary should be tweet-length, and the description more in depth.#
spec.name ="QRichTextView"
spec.version ="0.0.3"
spec.summary ="Pod version for https://github.com/aryaxt/iOS-Rich-Text-Editor.git"# This description is used to generate tags and improve search results.# * Think: What does it do? Why did you write it? What is the focus?# * Try to keep it short, snappy and to the point.# * Write the description between the DESC delimiters below.# * Finally, don't worry about the indent, CocoaPods strips it!
spec.description ="Pod version for https://github.com/aryaxt/iOS-Rich-Text-Editor.git. You can the rich text view."
spec.homepage ="https://github.com/lexuanquynh/QRichTextView"# spec.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"# ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― ### Licensing your code is important. See https://choosealicense.com for more info.# CocoaPods will detect a license file if there is a named LICENSE*# Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.#
spec.license ={:type =>"MIT",:file =>"LICENSE"}# spec.license = { :type => "MIT", :file => "FILE_LICENSE" }# ――― Author Metadata ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― ### Specify the authors of the library, with email addresses. Email addresses# of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also# accepts just a name if you'd rather not provide an email address.## Specify a social_media_url where others can refer to, for example a twitter# profile URL.#
spec.author ={"Le Xuan Quynh"=>"le-xuan-quynh@goodcycle.net"}# Or just: spec.author = "Le Xuan Quynh"# spec.authors = { "Le Xuan Quynh" => "le-xuan-quynh@goodcycle.net" }# spec.social_media_url = "https://twitter.com/Le Xuan Quynh"# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― ### If this Pod runs only on iOS or OS X, then specify the platform and# the deployment target. You can optionally include the target after the platform.## spec.platform = :ios# spec.platform = :ios, "5.0"# When using multiple platforms
spec.ios.deployment_target ="9.0"# spec.osx.deployment_target = "10.7"# spec.watchos.deployment_target = "2.0"# spec.tvos.deployment_target = "9.0"# ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― ### Specify the location from where the source should be retrieved.# Supports git, hg, bzr, svn and HTTP.#
spec.source ={:git =>"https://github.com/lexuanquynh/QRichTextView.git",:branch =>"main",:tag =>"#{spec.version}"}# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― ### CocoaPods is smart about how it includes source code. For source files# giving a folder will include any swift, h, m, mm, c & cpp files.# For header files it will include any header in the folder.# Not including the public_header_files will make all headers public.#
spec.source_files ="QRichTextView/Classes/*.{h,m}"# spec.exclude_files = "Classes/Exclude"
spec.public_header_files ="QRichTextView/Classes/*.h"# ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― ### A list of resources included with the Pod. These are copied into the# target bundle with a build phase script. Anything else will be cleaned.# You can preserve files from being cleaned, please don't preserve# non-essential files like tests, examples and documentation.## spec.resource = "icon.png"
spec.resources ="QRichTextView/Assets/*.png"# spec.preserve_paths = "FilesToSave", "MoreFilesToSave"# ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― ### Link your library with frameworks, or libraries. Libraries do not include# the lib prefix of their name.## spec.framework = "SomeFramework"# spec.frameworks = "SomeFramework", "AnotherFramework"# spec.library = "iconv"# spec.libraries = "iconv", "xml2"# ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― ### If your library depends on compiler flags you can set them in the xcconfig hash# where they will only apply to your library. If you depend on other Podspecs# you can include multiple dependencies to ensure it works.# spec.requires_arc = true# spec.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }# spec.dependency "JSONKit", "~> 1.4"end