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

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()

Related

Instance member 'jpegData' cannot be used on type 'UIImage' [duplicate]

This question already has answers here:
UIImageJPEGRepresentation has been replaced by instance method UIImage.jpegData(compressionQuality:)
(3 answers)
Closed 4 years ago.
// compressing image(avaImg)before sending it to the server(Parse)
let avaData = UIImageJPEGRepresentation(avaImg.image!, 0.5)
let avaFile = PFFile(name: "ava.jpg", data: avaData!)
user["ava"] = avaFile
When I replace it to the following line as recommended by the notifications I get the error below
let avaData = UIImage.jpegData(avaImg.image!, 0.5)
ERROR: Instance member 'jpegData' cannot be used on type 'UIImage'; did you mean to use a value of this type instead?
I'm a newbie and I followed that on an online course so please baby steps!
Do you mean swift 4.2?
Call it like this yourImageObject.jpegData(compressionQuality: 0.5) because function changed to public func jpegData(compressionQuality: CGFloat) -> Data? For more syntax I have a repo you can take a reference with changes from swift 4. https://github.com/alexliubj/Swift-Migration-4.2
Updated:
This API change is from iOS 12, not Swift 4.2. Thanks #rmaddy for your correction.

Xcode; How to call function in AppDelegate from another ViewController [duplicate]

This question already has answers here:
How do I get a reference to the AppDelegate in Swift?
(18 answers)
Closed 4 years ago.
I'm using Swift and the newest Xcode version.
I have this code in AppDelegate.swift:
func testPrint(string: String) {
print(string)
}
I can successfully call the function from the file ViewController.swift like this:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.testPrint(string: "Hello!")
But if I'm trying to call the function from TodayViewController.swift, which is for Apples Today Extension, I'm getting the following error:
Use of undeclared type 'AppDelegate'
Why that?
Try something along those lines:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.testPrint(string: "Hello!")
EDIT:
'Today Extension' and App live in a different contexts so it is not possible.

Get AppName in sdk [duplicate]

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 ?? ""

CoreData , executeFetchRequest, Extra Argument "Error" in Call [duplicate]

This question already has answers here:
Swift 2 ( executeFetchRequest ) : error handling
(5 answers)
Closed 7 years ago.
https://www.dropbox.com/s/vyzdrjlpvdcuill/managedContext.png?dl=0
I had this working January and when I ran it today, I was presented with this error.
I have tried to understand the new method of error handling, sadly I am not grasping it.
first time poster.
Sincerely,
rob
You have extra argument, which is the error,
Simple remove it.
Also this is a working code:
let fetchRequest = NSFetchRequest(entityName: "movieQuoteEntityName")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "lastTouchDate", ascending: false)]
var error :NSError? = nil
let appDel = UIApplication.sharedApplication().delegate as! AppDelegate
var context: NSManagedObjectContext? = nil
let movieQuotes = context!.executeFetchRequest(fetchRequest)
Well, with the Swift 2.0, they changed the whole error handling stuff so you are not able to declare that way.
Swift has try-catch now for the error handling, so you may change all of the old error handling methods.

Request Data from CoreData in Today Extension

i have an app with ios 8, swift and CoreData.
all works fine :)
now i would like to add an today extion - this is my first time to work with today extions. i very proud to say, that i already can set in my app an NSUserdafault value and show it on my today extension :)
but now i would like to go a step forward.
in my app i have a CoreData Entiny LM_ITEMS with an Attribute "Hersteller"
how can i list up all data of LM_ITEMS in my today extension?
Thank you :)
in my app i do it like this way:
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var LM = [LM_ITEMS]()
func DatenAbrufen() {
let fetchRequest = NSFetchRequest(entityName: "LM_ITEMS")
if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [LM_ITEMS] {
LM = fetchResults
}
}
But this doesnt work in today extension ...
You should better create a core data stack singleton object and include it in both targets, your app's target and extension target.
What #Lucian said is correct and can become very useful if you want to add other targets.
However if you want access the same database of your main app you have to make both the app and the widget use the same App Group in the target capabilities and place your core data database in the shared container.

Resources