#Blog

Blog articles on programming and problems focused on iOS and some web. I also write stories on my travel and hiking pursuits outside my work.

Managing Authentication in Image Loading Using SDWebImage

by

Bearer tokens serve as a form of access token authentication, providing a secure method for authorizing API requests. They are typically included in the Authorization header of HTTP requests and grant access to protected resources.

We can bolster the security of loading our images using bearer tokens. Common SDWebImage’s extension method we use to load an image into UIImageView is:

imageView.sd_setImage(with: url, placeholderImage: nil, options: [.refreshCached]) { (image, error, cacheType, url) in
//Handle if required
}

How do we pass a bearer token in SDWebImage?

CONTINUE READING


Making UIScrollView Scrollable with keypad in iOS

by

Making UIScrollView Scrollable with Keyboard in iOS

One common challenge in iOS app development is handling user input when the on-screen keyboard is displayed.

Register for Keyboard Notifications

Register for keyboard notifications in view controller. These notifications will inform us when the keyboard shows and hides.

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
CONTINUE READING


UIButton addTarget does not work inside custom UITableViewCell

by

When it comes to programmatically creating custom UITableViewCells, there's a common stumbling block that developers often encounter. It's the scenario where you add a button to the cell, but the target action doesn't get triggered when the button is pressed. While there could be several factors contributing to this issue, I'll delve into one of the fundamental design flaws that can lead to such behavior.

The crux of the problem often lies in the way we add subviews to our custom cell programmatically. A common practice is to add the subviews directly to the cell itself. However, this approach can cause unexpected behavior, including the failure of target action calls when a button is pressed.

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        addSubview(button)
        button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
}

So, what’s the solution? Let’s dive into it.

CONTINUE READING


How to convert Swift String to Array of String

by

To convert the string literal into an array of strings, we use the map function on the string literal.

map(_:)

Returns an array containing the results of mapping the given closure over the sequence’s elements.

CONTINUE READING


How To Efficiently Read Set Lines of Range in Text File

by

In order to crawl a large text file containing URLs, I required a method that would allow me to specify and read only a certain range of lines, without having to load the entire file into memory. To achieve this, I utilized the islice() function from Python's itertools module: itertools.islice()

Here is a code snippet that demonstrates this:

CONTINUE READING


Exploring the Wild: A Memorable Road Trip and Safari Adventure in Bardiya National Park

by

I could hear the sound of rain hitting against the window. Accompanied by thunder, it was a familiar sound that filled me with a sense of calm. I was half awake, still lost in the world of dreams, when I looked at my phone. The time was 6:00 AM. One hour before the morning safari starts. I sighed deeply, knowing that the rain meant that the tigers would not come out to drink today.

I had been waiting for weeks to go on this safari, and I had hoped to see these majestic creatures up close. But the weather was not on my side. I closed my eyes and listened to the sound of the rain. It was a soothing sound, but it also filled me with a sense of disappointment. I knew that I would have to wait for another day to see the tigers.

But as I lay there, half-awake, I began to notice other sounds around me. I could hear the sound of birds singing, and the rustling of leaves in the wind. The forest was alive, even in the rain.

The following day, we arrived at the park’s office to reserve our safari. Unfortunately, the fee for the two of us was quite steep, and we were charged 10000 NPR. However, our vehicle and guide arrived shortly after, and they greeted us warmly. We were given a quick briefing on the proper etiquette to follow during the safari, particularly when inside the park. Our guide suggested we wear dark clothing. As soon as we entered the wilderness, we were captivated by the aroma that surrounded us. The vehicle moved at a slow pace to minimize noise. We engaged our guide in conversation, and I inquired about the population of tigers in Bardiya. According to him, there were over 152 tigers. Our first stop was one of the most popular spots for tiger sightings. There were several areas tourists could visit in hopes of encountering a tiger. We reached the shoreline of a stream, where tigers frequently come to drink water. Our guide shared a memory of narrowly escaping a rhinoceros attack.

The safari guide continued his story, reliving the adrenaline rush and the fear that he had felt. It had been a close call, and he had been lucky to escape unscathed. The memory was still vivid in his mind, even though it had happened several years ago.

As he spoke, we leaned in, fascinated by the tale of the rhinoceros attack. We could almost feel the heat of the sun and the coolness of the water as we imagined themselves in the same situation.

CONTINUE READING


How to remove more than one item at once from Array with matching condition in Swift

by

In Swift most sequence collections provides removeAll(where:) - an efficient way to remove at once multiple items matching the coditional logic from collection.

removeAll(where:)

Removes all the elements that satisfy the given predicate.

CONTINUE READING


How to limit the number of characters in a UITextField or UITextView

by

Very common user experience we come across when using UITextField and UITextview is to limit the number of characters.

It’s implementation is straightforward using their standard delegates implementing either shouldChangeCharactersIn (for text fields) or shouldChangeTextIn (for text views).


For UITextField:


func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if (textField.text?.count ?? 0) +  (string.count - range.length) >= characterLimit{
        return false
    }
    
    return true
}


For UITextView:

CONTINUE READING


How to sort an array of objects by a property value in JavaScript

by

There is always a need to sort an array. What if array consists of custom objects? How do we sort array of custom objects by a property value? Let us find out!


const array = [
    { name: ‘John’, order: ‘2’ },
    { name: ‘Jack’, order: ‘1’ },
    { name: ‘James’, order: ‘10’ }
]

CONTINUE READING


Error 400: Remote Push Notifications Not Working For iOS

by

In my localhost server, remote push notifications worked fine. It had stopped working on the production server. I have designed a neat system to separate the development environment from production mode for testing remote push notifications. Different certificates are used automatically based on the running environment. Device tokens are stored in a database based on the nature of the iOS app’s distribution. It ensures sandbox mode uses debug-version of device tokens else we will be running often into the “Baddevice Token” response from the apns server.

The system is well-tested with time. Remote push notifications stopped working in production mode all of sudden. I am not using the third-party service to manage our push notifications. I have our well-designed and well-tested middleware to manage push notifications. It follows a modern recommendation from apple to use http/2 to process all requests to apns server. Many good tutorials out there on the subject. I will share my troubleshooting experience and its solutions.

I turned on the verbose settings to display every information of cURL execution. It gives insight on if the connection is going through the apns server and what response we get.


CURLOPT_VERBOSE => true
CONTINUE READING


Recommended posts