Get AppName in sdk [duplicate] - ios

This question already has answers here:
Get App Name in Swift
(19 answers)
Closed 4 years ago.
How should I get the name of the application from the library where the library using on that particular application. Already, I have tried with CFBundelName and CFBundleDisplayName but its not working for me.

Have you tried kCFBundleNameKey? (used since Swift 3)
Bundle.main.infoDictionary![kCFBundleNameKey as String] as! String

Swift 3:
Bundle.main.infoDictionary![kCFBundleNameKey as String] as! String
Swift 4:
let appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String

I have used this in one of my projects working in an SDK:
var appName = Bundle.main.infoDictionary?["CFBundleName"] as? String ?? ""

Related

URL error char set? [duplicate]

This question already has answers here:
Swift - encode URL
(19 answers)
Closed 5 years ago.
I tried to call a custom url using this code below:
let myurl = "https://myserver.com/call?a|b|c"
let converted = URL(string: myurl)
print(converted)
But what I'm getting as result in converted is just "nil".
I'm pretty sure this is because of the wrong characters set in relation to the URL() class.
After some research all I got so far is this outdated Swift code:
var myurl = "https://myserver.com/call?a|b|c"
var newurl = myurl.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())
print(newurl)
But it doesn't seems working this way.
How can I achieve to avoid the "nil" result using (in my case) Swift 4?
Found the solution on my own:
let myurl = "https://myserver.com/call?a|b|c"
let converted = URL(string: myurl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
print(converted!)
The new function is called addingPercentEncoding & I have to call it with the urlQueryAllowed property.
The urlQueryAllowed charset adds some characters like this to the url encoding: "!*'();:#&=+$,/?%#[]|"
VoliĆ !

Swift 3: Populating plist data to UITableView [duplicate]

This question already has an answer here:
Xcode 8.2.1 / Swift 3 - Load TableView from Plist Array of Dictionaries
(1 answer)
Closed 5 years ago.
early this month i started learning swift as i found it fun and easier than obj-c
i came up with an idea trying to show english words and meaning on my language,
for that case i have a plist ready on my hand and a UITableViewController
with 2 Labels
here is my plist
so on my UITableViewController, i tried to get hands on the plist file with this code
let path = NSBundle.mainBundle().pathForResource("TableData", ofType: "plist")
let array = Array(contentsOfFile: path!)
and i got stuck on the rest
thanks
If you are just staring to learn Swift please start with Swift 3. Your Swift 2 code is outdated.
The recommended way to load a property list file is PropertyListSerialization
let url = Bundle.main.url(forResource: "TableData", withExtension: "plist")!
do {
let data = try Data(contentsOf: url)
let dataArray = try PropertyListSerialization.propertyList(from: data, format: nil) as! [[String:String]]
print(dataArray)
} catch {
print("This error must not happen", error)
}

How to detect First launch of app on every new version upgrade?

I have a requirement of detecting the first launch of app after the user upgrades the app to a newer version. I need to perform certain task only on first launch of app after the user upgrades the app to a new version. Many links available online but none answer clearly to my query. How to achieve this in Swift 2 , iOS 9.
Most of the answers available says to maintain a key in NSUserDefaults and set its value to false and after first launch make it true. But the problem is after I upgrade my app the variable still will be true and thus my scenario fails on app upgrade. Any help would be much appreciated. Thanks!
Try this:
let existingVersion = NSUserDefaults.standardUserDefaults().objectForKey("CurrentVersionNumber") as? String
let appVersionNumber = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String
if existingVersion != appVersionNumber {
NSUserDefaults.standardUserDefaults().setObject(appVersionNumber, forKey: "CurrentVersionNumber")
NSUserDefaults.standardUserDefaults().synchronize()
//You can handle your code here
}
updating Yogesh's perfect, yet simple solution to swift 4
let existingVersion = UserDefaults.standard.object(forKey: "CurrentVersionNumber") as? String
let appVersionNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
if existingVersion != appVersionNumber {
print("existingVersion = \(String(describing: existingVersion))")
UserDefaults.standard.set(appVersionNumber, forKey: "CurrentVersionNumber")
// run code here.
}

Get list of files at path swift [duplicate]

This question already has answers here:
Iterate through files in a folder and its subfolders using Swift's FileManager
(14 answers)
Closed 8 years ago.
Just trying to make a for..in loop for files in the local app folder
let filemanager:NSFileManager = NSFileManager()
let files = filemanager.enumeratorAtPath(NSHomeDirectory())
for filename in files!
{
println(filename)
}
But it says Type 'NSDirectoryEnumerator' doesn't conform to protocol SequenceType.
I think this may be possible by implementing an extension to NSFileManager that implements the SequenceType protocol. But you could easily convert your code to using a while loop:
let filemanager:FileManager = FileManager()
let files = filemanager.enumerator(atPath: NSHomeDirectory())
while let file = files?.nextObject() {
print(file)
}

How to get sharedApplication in Swift on iOS? [duplicate]

This question already has answers here:
How do I get a reference to the AppDelegate in Swift?
(18 answers)
Closed 8 years ago.
How can I get instance of current UIApplication from View Controller in Swift?
I am getting Use of unresolved identifier 'sharedApplication' error with this code:
let app = sharedApplication as UIApplication
EDIT Swift 4.x
let app = UIApplication.shared.delegate as! AppDelegate
If you're e.g. not in a ViewController, you must:
import UIKit
Perhaps try:
let app = UIApplication.sharedApplication()
EDIT (Swift 3):
It should be noted that in Swift 3, to accomplish this, you will want to access the shared property instead:
let app = UIApplication.shared
You can do it by
let app = UIApplication.sharedApplication()

Resources