LSExtensions Usage: UIApplication+

Lee young-jun
4 min readDec 1, 2023

--

When I was iOS Developer at Home, I created a lot of iOS Apps.
And I enjoyed using Swift Extension.

Therefore I made many Extensions for various types whenever I need it.

To share the extensions between my app projects. I decided to archive them into CocoaPods(LSExtensions) and I am using it still now.

However I didn’t provides any Readme and usages.

In this article I will show you usage of my UIApplication extension. And I will post usages for all other extensions one by one.

appId

Sometimes I need to check new version number and take user to Appstore. And then I need app’s identifier, so I put “Itunes App Id” into Info.plist and call this property to get it.

let appId = UIApplication.shared.appId
let rateUrl = URL(string: "https://itunes.apple.com/app/myapp/id\(appId)?mt=8&action=write-review");

displayName

Whenever I need the app name, I don’t have to remember CFBundleDisplayName.

let msg = "Please review if you like \(UIApplication.shared.displayName ?? "")"

version

This is useful to present current app version.

versionLabel.text = "App Version: \(UIApplication.shared.version)"

isVersion(lessThan: String, orSame: Bool = false)

I made this function to determine current app need to be updated.

let needToUpdate = UIApplication.shared.isVersion(lessThan: minVersion)

carrier

I created this property to get carrier(kt,skt,lgt?) info of this device.

if UIApplication.shared.carrier?.carrierName == ... {
//
}

isIPad

In one of my app, I should switch the layout if the device is iPad, so I created this shortcut.

if UIApplication.shared.carrier?.carrierName == ... {
//
}

urlForItunes

If the app is required to be updated, I should take user to Appstore

if UIApplication.shared.carrier?.carrierName == ... {
//
}

openItunes

I created open appstore url shortcut with urlForItunes.

UIApplication.shared.openItunes()

openReview

I can also take user to review page of Appstore.

openSettings

Sometimes I need take user to the settings of this app to get permission.

UIApplication.shared.openSettings{ result in
//
};

openTwitter

When I was developing Democracy Action, I need to open Twitter page of the Congressor with their Twitter id. If the user doesn’t have Twitter app, I will return the url to open browser.

UIApplication.shared.openTwitter(id) { url in
//
}

openFacebook

The Congressors alow have Facebook account. (You have to find numbered facebook id — Find Facebook ID)

UIApplication.shared.openFacebook(id) { url in
//
}

openInstagram

I can also open Instagram.

UIApplication.shared.openInstagram(id) { url in
//
}

openKakaoStory

Some people have Kakao Story in their profile. So I had to create this method also.

UIApplication.shared.openKakaoStory(id) { url in
//
}

openKakaoPlus

If you want to open kakao plus friend from your app, you can do it with this method.

UIApplication.shared.openKakaoPlus(id) { url in
//
}

openWeb

You don’t have to make URL instance, when you open browser with String url.

UIApplication.shared.openWeb("https://toyboy2.medium.com/")

openEmail

I can open email app with someone’s email address.

UIApplication.shared.openEmail("your@email.com")

openSms

If you know someone’s phone number, you can open message app also.

UIApplication.shared.openSms("+821000000000")

openTel

you can also open call app.

UIApplication.shared.openTel("+821000000000")

searchByGoogle

When you open the browser with google search page, just pass the search keyword to this method. You can also use your own browser or webView with the handler.

UIApplication.shared.searchByGoogle("toyboy2") { url in
//
}

searchByNaver

If you prefer Naver, not Daum, use this method.

UIApplication.shared.searchByDaum("toyboy2") { url in
//
}

onNetworking

If you want to show users this app is networking without any UI update, call this method. Then you can see the small activity indicator in the status bar.

UIApplication.shared.onNetworking()

offNetworking

Call this method to hide networking indicator again.

UIApplication.shared.offNetworking()

isForeground

I want to know if my app is in foreground or not, but I don’t like to know UIApplication.State.

if UIApplication.shared.isForeground {
//
}

isBackground

I also made a indicator for the inactive state.

if UIApplication.shared.isBackground {
//
}

buildVersion

I had to know current app version to determine if the app needs update.

if UIApplication.shared.buildVersion < latestVersion {
//
}

isHasSearchNavigationBarTouchBug

I wanted to know OS version is less than 13.2, because user can’t touch navigation bar with search bar(UISearchController)

if UIApplication.shared.buildVersion < latestVersion {
//
}

increaseBadgeNumber

I made this method to increase the app badge number easily.

UIApplication.shared.increaseBadgeNumber()

increaseBadgeNumber

I also made this to decrease it again.

UIApplication.shared.increaseBadgeNumber()

If you curious how I implemented these extensions, see UIApplication+.swift.

Use this extensions by installing my library.

pod ‘LSExtensions’

Feel free to copy or modify it.
If you want to make your own library, check this article.

Please give me 👏 if this post is helpful to you.
There are more posts about iOS.

Please visit my Linked-In profile if you want to know more about me.

References

--

--

No responses yet