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.
Related
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.
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.
I am trying to integrate Parse into a Swift app. I downloaded the SDK, set the app id and added the dependencies, but when I try to import Parse, it says 'No such module - Parse'.
Check that the Parse framework has been copied to your project folder to wherever you keep your 3rd party dependencies (e.g. Vendor).
Then, add the path to the Parse framework to the Framework Search Paths (FRAMEWORK_SEARCH_PATHS) for your build target.
It should look something like this:
$(inherited)
$(PROJECT_DIR)/Vendor/Parse
I'd clean up the DerivedData folder and rebuild.
I think this link should be solve your Problem:
Set up new Parseproject
or here ist explained it for a existing project please check this
Edit after i saw the Code. At first please do not post api keys here this are your private api keys.
second i think the code should look like:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Parse.enableLocalDatastore()
// Initialize Parse.
Parse.setApplicationId("appID",
clientKey: "Key")
// [Optional] Track statistics around application opens.
PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
//end parse
// Override point for customization after application launch.
let splitViewController = self.window!.rootViewController as! UISplitViewController
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
splitViewController.delegate = self
let masterNavigationController = splitViewController.viewControllers[0] as! UINavigationController
let controller = masterNavigationController.topViewController as! MasterViewController
controller.managedObjectContext = self.managedObjectContext
return true
}
you have to put in your Key i corrected the method for you
My problem appears to have been in the naming of the application: I included a number.
Once I corrected this, the error disappeared. Perhaps the name prevents the import of certain frameworks.
Just after implementing a method for saving a users email and password from login into NSUserDefaults. My app crashes when running on iPhone 5s, but not in simulator (works as supposed).
Error message is:
dyld: Symbol not found: __TWPVSs26AutoreleasingUnsafePointerSs8_Pointer
Referenced from: /private/var/mobile/Containers/Bundle/Application/76A645B8-3428-452F-AEA4-60BAF6C28819/AppName.app/AppName
Expected in: /private/var/mobile/Containers/Bundle/Application/76A645B8-3428-452F-AEA4-60BAF6C28819/AppName.app/Frameworks/libswift_stdlib_core.dylib
in /private/var/mobile/Containers/Bundle/Application/76A645B8-3428-452F-AEA4-60BAF6C28819/AppName.app/AppName
My NSUserDefaults code is this (don't think the rest matters):
let loggedIn = "yes"
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setObject(email, forKey:"Email")
userDefaults.setObject(pw, forKey:"Password")
userDefaults.setObject(loggedIn, forKey: "LoggedIn")
userDefaults.synchronize()
This is set after user has entered email and password and the server has validated these and returned "ok".
In prepareForSegue I have this code:
let navigationController = segue.destinationViewController as UINavigationController
let vc = navigationController.viewControllers[0] as LoggedInViewController
// Gets email from saved data
let userDefaults = NSUserDefaults.standardUserDefaults()
let sendEmail: String = userDefaults.stringForKey("Email")
userDefaults.synchronize()
vc.email = sendEmail
Update:
I have gone through the crash logs and noticed this:
I dont really know how to read it but I noticed that exception code 0x0000000196442124 is at the bottom of my included image.
_dispatch_mach_msg_send + 1624
What does that mean? Does it help?
I have also marked all methods and stuff that has with NSUserDefaults (as seen above) as comments to see if they caused the problem. But the app crashes anyway..
Thanks in advance!
The error message says something about autoreleasing memory.
Do you have any ...Release() function calls within your swift code?
If so, comment them out and try again.
let sendEmail: String = userDefaults.stringForKey("Email")
for me swift is confused by lines like these and fails to correctly infer the type.
It performs ..... I don't what but it can't really get from anyobject! to string correctly
it crashes when it tries to work with sendEmail
see:
Get from AnyObject(NSString) to String
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()