Xcode 11 buried even more this option allowing to reveal the current file in the Project Navigator:

Good news, there is also a keyboard shortcut for this action: ⇧ + ⌘ + J
Xcode 11 buried even more this option allowing to reveal the current file in the Project Navigator:
Good news, there is also a keyboard shortcut for this action: ⇧ + ⌘ + J
When using custom cells in UICollectionViews, you usually have to go through two things:
To make cell registration easier, each cell has to provide two things:
I created a protocol that will make more sense later on:
protocol UICollectionViewRegisterable {
static var cellIdentifier: String { get }
static var cellNib: UINib { get }
}
So the cell just needs to conform to this protocol:
extension HomeCell: UICollectionViewRegisterable {
static var cellIdentifier: String {
return "HomeCellID"
}
static var cellNib: UINib {
return UINib(nibName: "HomeCell", bundle: nil)
}
}
Then we have to register the cell in the collection view, here an extension on UICollectionView will help us get rid of the optional and enforce type:
extension UICollectionView {
func register(type: UICollectionViewRegisterable.Type) {
register(type.cellNib, forCellWithReuseIdentifier: type.cellIdentifier)
}
func dequeueCell<CellType: UICollectionViewRegisterable>(at indexPath: IndexPath) -> CellType {
guard let cell = dequeueReusableCell(withReuseIdentifier: CellType.cellIdentifier, for: indexPath) as? CellType else {
fatalError("UICollectionView should dequeue a cell of type \(CellType.self)")
}
return cell
}
}
Registering the cell becomes really easy:
override func viewDidLoad() {
super.viewDidLoad()
// Configure collection view
collectionView.register(type: HomeCell.self)
}
To dequeue the cell, the extension is using generics, so we can’t let type inference do the work here, and have to specify the type of the cell for the code to compile:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: HomeCell = collectionView.dequeueCell(at: indexPath)
// TODO: configure cell
return cell
}
That’s it! Here is the final code:
These two tools are now part of my tool belt when creating an iOS project, I don’t see how I can go back working without, and I recommend them for any type of project.
Automatically enforce Swift style, conventions and best practices. It’s a good idea to just install it when creating a new project and treat all warnings and errors as they come. Everything can be configured or disabled using a .swiftlint.yml file at the root of the project.
Repository: https://github.com/realm/SwiftLint
You have a preferred style for formatting your code, your teammates eventually have a different preferred style when formatting their code, and you find yourself spending too much time arguing about the format and/or asking others to update their code during reviews over format issues?
It’s helpful to agree on a common coding style with your team, and it’s even better if you can enforce it. SwiftFormat to the rescue, it enforces the coding style by automatically formatting the code.
I personally installed it for it to run during each build, the overhead is small and I don’t have to think about formatting anymore.
Repository: https://github.com/nicklockwood/SwiftFormat
Let’s say you want to have your UI change when a scroll view is bouncing (or over-scrolling).
First you need to conform to the UIScrollViewDelegate methods, more specifically scrollViewDidScroll(_ scrollView: UIScrollView).
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// TODO
}
}
For example, let’s change the background color of the scroll view depending on bouncing status. If user is over-scrolling from the top, let’s change it to red, green if over-scrolling from the bottom, and white otherwise.
First, let’s detect when bouncing from the top:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let topInsetForBouncing = scrollView.safeAreaInsets.top != 0.0 ? -scrollView.safeAreaInsets.top : 0.0
let isBouncingTop: Bool = scrollView.contentOffset.y < topInsetForBouncing - scrollView.contentInset.top
if isBouncingTop {
scrollView.backgroundColor = .red
} else {
scrollView.backgroundColor = .white
}
}
It’s fairly easy, if the current vertical contentOffset is smaller than all space at the top, then we are over-scrolling. Because the top space will differ depending on devices, we have to use the safeAreaInsets
, but mind that -0.0 is different than 0, thus the ternary operator.
Now let’s detect when the scroll view is bouncing from the bottom:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let topInsetForBouncing = scrollView.safeAreaInsets.top != 0.0 ? -scrollView.safeAreaInsets.top : 0.0
let isBouncingTop: Bool = scrollView.contentOffset.y < topInsetForBouncing - scrollView.contentInset.top
let bottomInsetForBouncing = scrollView.safeAreaInsets.bottom
let threshold: CGFloat
if scrollView.contentSize.height > scrollView.frame.size.height {
threshold = (scrollView.contentSize.height - scrollView.frame.size.height + scrollView.contentInset.bottom + bottomInsetForBouncing)
} else {
threshold = topInsetForBouncing
}
let isBouncingBottom: Bool = scrollView.contentOffset.y > threshold
if isBouncingTop {
scrollView.backgroundColor = .red
} else if isBouncingBottom {
scrollView.backgroundColor = .green
} else {
scrollView.backgroundColor = .white
}
}
It’s a bit more tricky in this case, scroll views are bouncing wether or not there is enough content for them to reach them, so we need to define what threshold the content offset needs to pass. When the content size is not big enough, then any movement that is not bouncing from the top is bouncing from the bottom. If the content size is big enough, then we can use a similar logic than before.
Let’s cleanup a little, this looks like a good candidate for an extension:
Now we simply need to call isBouncingTop
or isBouncingBottom
on the scroll view:
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.isBouncingTop {
scrollView.backgroundColor = .red
} else if scrollView.isBouncingBottom {
scrollView.backgroundColor = .green
} else {
scrollView.backgroundColor = .white
}
}
}
And because UITableView and UICollectionView inherit from UIScrollView, it will work for these as well.
We would all prefer to have access to a public API but sometimes, services you would love to add automation to only offer their website to use.
First let’s state that not all mobile or web applications can be (easily) reversed engineered, some companies invest a lot of money to secure and protect their API, user data. I’m not a hacker, but sometimes being resourceful let you create scripts and automations that can make you save tons of time. Note that private API, as in not documented, are usually not intended to be used by others than developers internal to the service or product, so don’t count on it to be stable, don’t expect to receive a notice if something is going to break, don’t base your entire business on something that can disappear any day.
In some cases, web browser inspectors comes handy. With the surge nowadays of websites developed using React, or more generally client side web applications, the network inspector of your browser often shows all the requests the website is doing in the background in response to your actions on the interface. Coupled with a HTTP proxy like Charles to inspect traffic going through your phone, you can scan pretty much all endpoints a service is using and try to mimic this behavior in scripts.
There is often only one problem: authentication. Most web services nowadays are using JWT session tokens, passed in the headers of each authenticated request, and even though you could re-execute a request you copied from your network inspector / HTTP proxy using the same URL, parameters and headers, this session token will eventually expire after an hour (a day at best depending on how their authentication is configured) and you will have to manually extract your access token again and again.
To be honest, I’m often able to reverse engineer everything but the authentication, as a user, I’m fine with that 🙂
This is the best case scenario: if you can sniff and extract the refresh token from the original authentication request, and reverse engineer the token retrieval request, then you can generate a fresh access token every time your script run.
Chances are the website is refreshing its own access token periodically as well as when launching it, this provides a seamless experience without having to log in every time. Obviously websites trying to secure sensitive data like banks are not doing this, but a lot of other consumer websites and tools are.
In that case, it’s pretty common for these web applications to store access tokens and / or refresh tokens in their local storage. You can see the content of the local storage using the “application” tab of the web inspector (Google Chrome), sometimes this information can also be stored in cookies.
Don’t be evil.
Remember that you can be in trouble for doing that, only use these techniques to automate something you have the right to do.