Files
awesome-ios-developer/README.md

521 lines
14 KiB
Markdown
Raw Normal View History

2021-04-10 04:21:15 -05:00
# Useful Swift Things
2021-04-10 03:42:23 -05:00
2021-04-10 04:27:15 -05:00
<div align="center">
2021-04-13 01:32:42 -05:00
<img src="https://github.com/jphong1111/Useful_Swift/blob/main/Images/swift.jpeg">
2021-04-10 04:27:15 -05:00
</div>
2021-04-27 00:19:44 -05:00
<p>
</p>
2021-04-27 00:11:04 -05:00
<p align="center">
<img src="https://img.shields.io/badge/-OneDayOneCommit-critical?style=plastic&logo=swift" />
</p>
2021-04-13 01:33:49 -05:00
2021-04-10 04:15:30 -05:00
## Content
2021-04-09 16:14:59 -05:00
- [Coding convention](#Coding-convention)
2021-05-05 03:26:00 -05:00
- [Swift Lint](#Swift-lint)
2021-04-10 04:05:27 -05:00
- [Design Pattern](#Design-Pattern)
2021-04-10 14:05:11 -05:00
- [Delegate](#Delegate)
- [Singleton](#Singleton)
2021-04-26 23:39:49 -05:00
- [Observer](#Observer-Pattern)
2021-04-10 04:18:23 -05:00
- [Code Structuring](#Code-Structuring)
2021-04-10 04:20:15 -05:00
- [MVC](#MVC)
- [MVVM](#MVVM)
2021-04-26 23:39:33 -05:00
- [VIPER](#VIPER)
2021-04-09 16:19:52 -05:00
- [UIDesign](#UIDesign)
2021-04-22 05:06:56 -05:00
- [Helper](#Helper)
2021-05-03 02:35:24 -05:00
- [Email, Message, Call](#email-message-call)
2021-05-03 01:57:35 -05:00
- [Network Layer](#Network-Layer)
2021-05-03 04:50:11 -05:00
- [Image Picker](#Image-Picker)
2021-05-03 06:21:44 -05:00
- [File Manager](#File-Manager)
- [Video Downloader](#Video-Downloader)
2021-05-19 22:50:21 -05:00
- [Image Downloader](#Image-Downloader)
2021-05-03 07:09:09 -05:00
- [Location Manager](#Location-Manager)
2021-04-09 16:19:52 -05:00
- [API](#API)
2021-04-10 03:55:09 -05:00
- [JSON](#JSON)
2021-04-09 16:30:48 -05:00
- [Third Party Library](#Third-Party-Library)
2021-04-28 23:42:01 -05:00
- [GCD](#GCD)
2021-04-30 01:40:48 -05:00
- [DispatchQueue](#DispatchQueue)
- [DispatchGroup](#DispatchGroup)
- [DispatchWorkItem](#DispatchWorkItem)
2021-04-30 05:16:16 -05:00
- [Thread Sanitizer](#Thread-Sanitizer)
2021-05-07 03:10:24 -05:00
- [Testing](#Testing)
2021-05-14 05:33:02 -05:00
- [Code Coverage](#Code-Coverage)
2021-05-07 03:10:24 -05:00
- [Unit Test](#Unit-Test)
- [UI Test](#UI-Test)
2021-05-20 22:16:54 -05:00
- [IAP](#IAP)
- [APNS](#APNS)
2021-04-23 02:19:47 -05:00
- [Useful Stuff](#Useful-Stuff)
2021-04-23 11:34:04 -05:00
- [Show Preview in UIKit(Build UI with Code Base)](#show-preview-in-uikitbuild-ui-with-code-base-----)
2021-04-24 16:50:50 -05:00
- [Write README.md](#write-readmemd)
2021-04-09 15:16:44 -05:00
2021-04-09 15:25:46 -05:00
2021-04-09 16:13:06 -05:00
## Coding convention
2021-04-10 03:22:54 -05:00
set of guidelines for a specific programming language that recommend programming style
### Swift Style Guide
2021-04-09 16:12:26 -05:00
2021-04-10 02:51:46 -05:00
- [Swift Style Guide](https://github.com/linkedin/swift-style-guide)
2021-04-09 16:34:45 -05:00
### Swift Lint
2021-04-10 03:20:40 -05:00
The way of force you to adapt coding convention
>otherwise project build will **FAILED**
2021-04-10 03:14:13 -05:00
- [Swift Lint](https://github.com/realm/SwiftLint) apply for all project:+1:
2021-04-15 07:36:44 -05:00
```swift
2021-04-15 07:36:01 -05:00
if which swiftlint >/dev/null; then
swiftlint
else
echo "error: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
exit 1
fi
```
2021-04-09 16:34:45 -05:00
put .yml file into root folder and apply following code in Build Phases
2021-04-09 15:47:49 -05:00
2021-04-22 05:19:49 -05:00
**You can modify(delete) SwiftLint Option with opening .yml file**
2021-04-22 05:20:10 -05:00
2021-04-22 05:21:49 -05:00
> Shift + Command + . will show the hidden file
2021-04-22 05:19:49 -05:00
<img src="https://github.com/jphong1111/Useful_Swift/blob/main/Images/swiftLintChange.png">
2021-04-10 04:11:11 -05:00
## Design Pattern
2021-04-13 16:00:57 -05:00
Design pattern is
2021-04-13 16:01:22 -05:00
2021-04-13 16:00:57 -05:00
Check [this](https://refactoring.guru/design-patterns/swift) website for design pattern in Swift
2021-04-10 04:11:11 -05:00
### Delegate Pattern
2021-04-10 04:30:44 -05:00
```swift
2021-04-10 04:11:11 -05:00
weak var delegate: SomeProtocol?
```
### Singleton Pattern
2021-04-10 04:30:44 -05:00
```swift
2021-04-10 04:11:11 -05:00
class SingletonPattern {
static let manager = SingletonPattern()
private init() {}
2021-04-10 04:11:45 -05:00
}
2021-04-10 04:11:11 -05:00
```
2021-04-13 16:02:14 -05:00
### Observer Pattern
2021-05-06 08:56:57 -05:00
The observer pattern lets one object observe changes on another object. Apple added language-level support for this pattern in Swift 5.1 with the addition of Publisher in the Combine framework.
2021-05-09 14:48:29 -05:00
<img src = "https://github.com/jphong1111/Useful_Swift/blob/main/Images/observer.png" />
2021-04-13 16:02:14 -05:00
2021-04-10 04:18:23 -05:00
## Code Structuring
2021-04-10 04:05:27 -05:00
2021-04-10 04:18:23 -05:00
### MVC
2021-04-10 04:05:27 -05:00
2021-04-14 22:00:46 -05:00
<img src="https://github.com/jphong1111/Useful_Swift/blob/main/Images/MVCModel.png">
2021-04-13 01:33:49 -05:00
2021-04-10 04:20:15 -05:00
### MVVM
2021-04-14 22:01:10 -05:00
##### MVC vs MVVM
2021-04-14 22:00:46 -05:00
<img src="https://github.com/jphong1111/Useful_Swift/blob/main/Images/MVVMvsMVC.png">
2021-04-19 03:39:24 -05:00
M -> Model Which holds the application data
V > View It displays the data that is stored in model. These are visual elements through which a user interacts. These are subclasses of UIView
VM > View Model Transform model information/data and it interacts with controller or view to display those informations.
C > Controller class It will be there but the responsibility of view business logic has been removed and give to view model
2021-04-26 23:39:33 -05:00
### VIPER
2021-04-13 16:02:14 -05:00
2021-04-09 16:13:06 -05:00
## UIDesign
2021-04-09 15:58:50 -05:00
2021-04-09 16:16:24 -05:00
### HIG(Human Interface Guidelines)
2021-04-10 02:51:46 -05:00
- [Apple UI Kit](https://developer.apple.com/documentation/uikit)
2021-05-02 22:58:56 -05:00
- [iOS Design Guide](https://ivomynttinen.com/blog/ios-design-guidelines)
2021-04-09 16:16:24 -05:00
2021-04-09 16:13:06 -05:00
### iOS icon
2021-04-09 16:12:26 -05:00
2021-05-05 05:35:44 -05:00
- [icon8](https://icons8.com/) You can download icons imge for your **APP**
2021-05-05 05:36:18 -05:00
- [appicon](https://appicon.co/) generate the app icon size
2021-04-09 15:58:50 -05:00
2021-04-09 16:13:06 -05:00
### UIdesign inspiration
2021-04-09 15:58:50 -05:00
2021-04-09 16:33:17 -05:00
- [dribble](https://dribbble.com/)
- [pinterest](https://pinterest.com/)
- [behance](https://www.behance.net/)
- [pttrns](https://pttrns.com/)
- [awwwards](https://www.awwwards.com/)
- [flickr](http://www.flickr.com/)
- [mobbin](https://mobbin.design/)
## Helper
2021-04-22 05:06:56 -05:00
2021-05-03 01:57:35 -05:00
All files are resuable files and protocol oriented. **Just Copy and Paste inside your project and use it!!** 👍
2021-05-03 04:51:46 -05:00
## Email, Message, Call
2021-05-03 01:57:35 -05:00
2021-04-22 05:06:56 -05:00
You can check the file in the follow link
- [Email, Message, Call](https://github.com/jphong1111/Useful_Swift/blob/main/Helper/ConversationManager.swift)
2021-05-03 06:21:44 -05:00
### Usage
2021-04-22 05:14:29 -05:00
import MesaageUI first
```swift
import MessageUI
```
Then use it
2021-05-03 06:29:22 -05:00
> Don't forget to extend the mail, message delegate to your ViewController!
2021-04-22 05:14:29 -05:00
```swift
2021-05-03 06:29:22 -05:00
lazy var conversation = ConversationManager(presentingController: self, mailDelegate: self, messageDelegate: self, viewController: self)
2021-05-03 06:29:22 -05:00
@IBAction private func sendEmail(_ sender: UIButton) {
conversation.sendEmail(feedback: MailFeedback(recipients: ["abcd@google.com"], subject: "FeedBack", body: "Write feedback here"))
}
2021-05-03 06:29:22 -05:00
@IBAction private func sendMessage(_ sender: UIButton) {
conversation.sendMessage(feedback: MessageFeedBack(recipients: ["1111111111"], body: "Type here"))
}
2021-05-03 06:29:22 -05:00
@IBAction private func startCall(_ sender: UIButton) {
conversation.makeCall(number: "1111111111")
}
```
2021-04-09 15:58:50 -05:00
2021-05-03 06:41:57 -05:00
Good To GO 👏👏👏
> See Example [here](https://github.com/jphong1111/ImageMessageHandler_DemoApp)
2021-05-03 04:51:46 -05:00
## Network Layer
2021-05-03 01:57:35 -05:00
2021-05-03 01:58:22 -05:00
- [Network Layer](https://github.com/jphong1111/Useful_Swift/tree/main/Helper/Network%20Layer)
2021-05-03 01:57:35 -05:00
2021-05-03 04:51:46 -05:00
### Usage
2021-05-03 02:26:28 -05:00
First, set the base URL in **EndPointType file**
2021-05-03 05:05:30 -05:00
> Don't forget to put your API key in it!
2021-05-03 02:26:28 -05:00
```swift
var baseURL: URL {
guard let url = URL(string: "https://api.openweathermap.org/data/2.5/") else {
fatalError("baseURL could not be configured.")
}
return url
}
```
then make a instance of router.swift file in your code
```swift
private let router = Router<YourAPI>()
```
2021-05-03 02:27:54 -05:00
for **YourAPI part**, simply create a new **enum** with cases about specific api URL
2021-05-03 02:26:28 -05:00
> It will make your router more dynamic!
> Don't forget extension to EndPointType!
```swift
enum YourAPI {
case first(country: String)
case second(time: Int)
case third(name: String)
}
extension YourAPI: EndPointType {
var path: String {
switch self {
case .first(let country):
return "\(country).json"
case .second(let time):
return "\(time).json"
2021-05-03 02:27:54 -05:00
case .third(let name):
2021-05-03 02:26:28 -05:00
return "\(name).json"
}
}
}
```
then, use it like this
```swift
router.request(.first(country: London)) { [weak self] (results: Result<CountryWeather, AppError>) in
guard let self = self else { return }
switch results {
case .success(let data):
// insert your modifications!
case .failure(let error):
// insert your modifications!
print(error)
}
}
```
> **CountryWeather should be a model with Decodable**
2021-05-19 22:42:57 -05:00
If you want to see how can I use Network Layer in Project, check [this](https://github.com/jphong1111/Unsplash_Clone/tree/main/Unsplah_Clone/ReusableComponent/NetworkLayer)
2021-05-03 02:26:28 -05:00
2021-05-03 02:29:43 -05:00
This reusable network layer files for referenced from [here](https://medium.com/flawless-app-stories/writing-network-layer-in-swift-protocol-oriented-approach-4fa40ef1f908)
2021-05-19 22:42:57 -05:00
> Also [Alamofire](https://github.com/Alamofire/Alamofire) will be a great option for Network Layer!
2021-05-03 04:51:46 -05:00
## Image Picker
2021-05-03 04:50:11 -05:00
2021-05-05 12:51:33 -05:00
- [Image Picker](https://github.com/jphong1111/Useful_Swift/blob/main/Helper/ImagePickerHandler/ImagePicker.swift)
2021-05-03 04:50:11 -05:00
2021-05-03 04:51:46 -05:00
### Usage
2021-05-03 04:50:11 -05:00
Copy and Paste in your project and then declare Image Picker object inside your project
```swift
lazy var imagePicker = ImagePicker(presentationController: self, delegate: self)
```
Then, extend ImagePickerDelegate to your viewController
```swift
extension ViewController: ImagePickerDelegate {
func didSelect(image: UIImage?) {
self.yourImageView.image = image
self.dismiss(animated: true, completion: nil)
}
}
```
Good To GO 👏👏👏
> See Example [here](https://github.com/jphong1111/ImageMessageHandler_DemoApp)
2021-05-03 06:21:44 -05:00
## File Manager
2021-05-03 06:46:08 -05:00
- [File Manager](https://github.com/jphong1111/Useful_Swift/blob/main/Helper/FileManageHelper.swift)
### Usage
Copy and Paste in your project
```swift
let readData = FileManageHelper.manager.readFile(filename: fileNameTextField.text ?? "", type: extensionTextField.text ?? "")
resultTextField.text = readData
```
> File Manager are wrote with singleton pattern, therefore no need to declare in side your code!
2021-05-03 06:21:44 -05:00
2021-05-03 06:51:05 -05:00
Good To GO 👏👏👏
2021-05-03 06:21:44 -05:00
## Video Downloader
2021-05-03 06:51:05 -05:00
- [Video Downloader](https://github.com/jphong1111/Useful_Swift/blob/main/Helper/VideoManager.swift)
## Usage
Make an object of VideoManager inside your code
```swift
let videoManager = VideoManager()
```
use downloadVideoLinkAndCreateAsset function to start download with entering URL
```swift
self.videoManager.downloadVideoLinkAndCreateAsset(text)
```
Good To GO 👏👏👏
2021-05-03 06:21:44 -05:00
2021-05-19 22:50:21 -05:00
## Image Downloader
There is no file for Image Downloader.
To download images into device, only thing is this
```swift
if let data = try? Data(contentsOf: urls),
let image = UIImage(data: data) {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
```
2021-05-19 22:52:34 -05:00
Just **change urls into your image URL**
2021-05-19 22:50:21 -05:00
> UIImageWriteToSavedPhotosAlbum will take care it to download to device.
2021-05-19 22:52:34 -05:00
2021-05-19 22:50:21 -05:00
> For more info go [here](https://www.hackingwithswift.com/example-code/media/uiimagewritetosavedphotosalbum-how-to-write-to-the-ios-photo-album)
Good To GO 👏👏👏
2021-05-03 07:09:09 -05:00
## Location Manager
2021-05-05 12:50:10 -05:00
- [Location Manager](https://github.com/jphong1111/Useful_Swift/tree/main/Helper/LocationHandler/LocationManager.swift)
2021-05-03 07:09:09 -05:00
2021-04-09 16:13:06 -05:00
## API
2021-04-09 16:12:26 -05:00
2021-04-30 21:20:43 -05:00
API(Application Programming Interface) is an interface that defines interactions between multiple software applications or mixed hardware-software intermediaries. It defines the kinds of calls or requests that can be made, how to make them, the data formats that should be used, the conventions to follow, etc.
2021-04-09 16:30:48 -05:00
2021-04-10 03:55:09 -05:00
## JSON
2021-04-10 03:58:08 -05:00
JSON is a language-independent data format
2021-04-10 04:01:31 -05:00
> Which is relative with **KEY - VALUE** pair
2021-04-10 04:30:44 -05:00
```json
2021-04-10 03:58:08 -05:00
{
2021-04-10 04:00:40 -05:00
"main": [
2021-04-10 03:58:08 -05:00
{
2021-04-10 04:00:40 -05:00
"title": "example1",
2021-04-10 04:31:31 -05:00
"body": "body1"
2021-04-10 03:58:08 -05:00
},
{
2021-04-10 04:00:40 -05:00
"title": "example2",
2021-04-10 04:31:31 -05:00
"body: "body2"
2021-04-24 16:53:10 -05:00
}
2021-04-10 03:58:08 -05:00
]
}
2021-04-10 04:00:40 -05:00
```
2021-04-10 03:55:09 -05:00
### JSON parser extension for Chrome
2021-04-10 04:05:27 -05:00
This extension makes JSON more structable
2021-04-10 03:55:09 -05:00
[JSON parser pro](https://chrome.google.com/webstore/detail/json-viewer-pro/eifflpmocdbdmepbjaopkkhbfmdgijcc) **FREE** :+1:
2021-04-10 04:15:30 -05:00
### JSON Decoding
2021-04-30 21:20:43 -05:00
To use JSON Decoding in swift, you have to define the model to be Codable or Decodable
```swift
public typealias Codable = Decodable & Encodable
```
> Decodable can only decode the json data. Can't encoded json file!!
```swift
struct User: Codable {
var first_name: String
var last_name: String
var country: String
}
```
2021-05-19 22:34:45 -05:00
To use JSONDecoding, declare JSONDecoder and use decode() function
```swift
do {
let data = try JSONDecoder().decode(T.self, from: unwrappedData)
completionOnMain(.success(data))
} catch {
print(error)
completionOnMain(.failure(.parseError))
}
```
T.self -> Model(Struct) of the data that you want to decode
> data will decoded to form of T
unwrappedData -> Input actual data from file or server
> This should be a Data Type!!
2021-04-10 04:15:30 -05:00
### JSONSerialization
2021-04-30 21:20:43 -05:00
### Various API Site
- [rapidAPI](https://www.rapidapi.com)
2021-05-07 03:10:24 -05:00
## Third Party Library
[This github](https://github.com/vsouza/awesome-ios) contains all the popular libraries in Swift:+1:
2021-05-20 22:10:28 -05:00
Recommand Useful Library
- [SDWebImage]
- [Hero]
- [Alamofire]
- [RxSwift]
2021-04-28 23:42:01 -05:00
## GCD
GCD(Grand Central Dispatch) is a low-level API for managing concurrent operations. It can help you improve your apps responsiveness by deferring computationally expensive tasks to the background.
2021-04-10 04:15:30 -05:00
2021-04-30 01:40:48 -05:00
### DispatchQueue
2021-04-30 02:06:31 -05:00
An object that manages the execution of tasks serially or concurrently on your app's main thread or on a background thread.
2021-04-30 01:40:48 -05:00
#### main
2021-04-30 02:06:31 -05:00
We can say main is a serial queue
2021-04-30 01:40:48 -05:00
#### global()
2021-04-30 02:06:31 -05:00
We can say global is a concurrent queue
2021-04-30 01:40:48 -05:00
### DispatchGroup
### DispatchWorkItem
2021-04-30 05:16:16 -05:00
### Thread Sanitizer
Thread Sanitizer is a tool to identifies the potential thread-related corruption issues. And it is a good way to find the [Readers and Writers problem](https://en.wikipedia.org/wiki/Readers%E2%80%93writers_problem) in your application.
2021-04-30 01:58:38 -05:00
#### How to Use Address Sanitizer
Go to this Option and Click **EDIT SCHEME...** 👈
2021-04-30 02:01:13 -05:00
<img src="https://github.com/jphong1111/Useful_Swift/blob/main/Images/address_sanitizer.png">
2021-04-30 05:16:16 -05:00
And then go to **RUN** and check **THREAD SANITIZER** 👈
2021-04-30 01:58:38 -05:00
2021-04-30 05:18:31 -05:00
<img src="https://github.com/jphong1111/Useful_Swift/blob/main/Images/thread_sanitizer.png">
2021-04-30 01:40:48 -05:00
2021-05-07 03:10:24 -05:00
## Testing
2021-05-14 05:30:52 -05:00
Before start your Testing, add coverage will be a good option to show the result of test
2021-05-14 05:33:02 -05:00
### Code Coverage
First, check code coverage
2021-05-14 05:30:52 -05:00
<img src ="https://github.com/jphong1111/Useful_Swift/blob/main/Images/addCoverage1.png" />
2021-05-14 05:33:02 -05:00
Then, go to **EDIT SHEME**, check like this
2021-05-14 05:30:52 -05:00
<img src ="https://github.com/jphong1111/Useful_Swift/blob/main/Images/addCoverage2.png" />
2021-05-07 03:10:24 -05:00
### Unit Test
### UI Test
2021-04-23 02:19:47 -05:00
2021-05-20 22:16:54 -05:00
## IAP
IAP stands for **In App Purchase**
## APNS
APNS stands for **Apple Push Notification service**
2021-04-23 02:19:47 -05:00
## Useful Stuff
2021-04-23 02:25:02 -05:00
### Show Preview in UIKit(Build UI with Code Base) 👍 👍 👍 👍 👍
Copy this code and Paste into your controller
2021-04-23 02:19:47 -05:00
```swift
2021-05-04 13:37:42 -05:00
#if canImport(SwiftUI) && DEBUG
2021-04-23 02:19:47 -05:00
import SwiftUI
2021-05-04 13:37:42 -05:00
struct SwiftLeeViewRepresentable: UIViewRepresentable {
func makeUIView(context: Context) -> UIView {
return UIStoryboard(name: "Main", bundle: Bundle.main).instantiateInitialViewController()!.view
2021-04-23 02:19:47 -05:00
}
2021-05-04 13:37:42 -05:00
func updateUIView(_ view: UIView, context: Context) {
2021-04-23 02:19:47 -05:00
}
}
2021-05-04 13:37:42 -05:00
@available(iOS 13.0, *)
struct SwiftLeeViewController_Preview: PreviewProvider {
2021-04-23 02:19:47 -05:00
static var previews: some View {
2021-05-04 13:37:42 -05:00
SwiftLeeViewRepresentable()
2021-04-23 02:19:47 -05:00
}
}
2021-05-04 13:37:42 -05:00
#endif
2021-04-23 02:19:47 -05:00
```
2021-04-23 02:25:02 -05:00
Enable canvas option like this
<img src="https://github.com/jphong1111/Useful_Swift/blob/main/Images/preview%20using%20canvas.png">
2021-04-23 02:34:10 -05:00
**You are GOOD TO GO** 👏👏👏
2021-04-23 02:38:50 -05:00
2021-04-23 02:39:56 -05:00
<img src="https://github.com/jphong1111/Useful_Swift/blob/main/Images/preivew_screenShot.png">
2021-04-23 12:01:19 -05:00
## Write README.md
2021-04-23 12:01:53 -05:00
[This](https://medium.com/@saumya.ranjan/how-to-write-a-readme-md-file-markdown-file-20cb7cbcd6f) will help you to write a README.md file more dynamically 👍
2021-04-30 21:22:58 -05:00
## Author
2021-04-30 21:23:20 -05:00
This README.md file is written by **Jungpyo Hong (Dennis)**