In my project file, I have two seperate UITableViewCells. One containing a UITextfield and the other containing a UIDatePicker. Both of these cells are placed into a UITableViewController to contain them in a table format. I want a local notification to be sent when the user inputs text and sets a date, but so far I haven't been able to find the right tutorial on this topic using Swift programmatically.
First, create your instance of UILocalNotification: var myNotification = UILocalNotification() Next, give the notification it's text: myNotification.alertBody = "I am a notification!" Next, set a NSDate for when the notification should fire: myNotification.fireDate = NSDate() Finally, add your notification to your app: UIApplication.sharedApplication.scheduleLocalNotification(myNotification) And you should be all set! It should be noted that your UILocalNotification will only fire if your app is not in the foreground (in other words, you can't be in the app). Hope this helped!
Related
I'm making an app in swift 3
I have a TableViewController, when I click on a row it opens a ViewController in which there is a TextView. When I click on it, I am able to edit it, but the changes are not "saved". when I go back to my list and re-click on the same row, the text is back to "Default", I've been on this problem all day, and I don't know how to solve it, I've tried solution from stackoverflow but only give solution to change the text colours.
So how can I do that ?
Based on what you've said so far, I can only assume that you are not storing the new text that's been entered into your UITextView.
The reason that the changes are not being saved is that every time you "open" a new ViewController by tapping on a TableViewCell, it is a brand new instance of that ViewController. That means that it has no input as far as what should be displayed in the TextView, and therefore displays the default text.
But how do I fix it?
This is going to require that you save your TextView's text every time that you leave or dismiss the ViewController. This is because when you leave the ViewController, it is being released from memory. This means that anything that was written there no longer exists. I would suggest storing the text in an array of strings on your TableViewController, which should allow for the text to persist over the life cycle of the app.
The text disappears when I close the app!
This is something that will require data persistence, and for that I would suggest reading up on how to use Core Data to store and persist data across multiple app life cycles.
There are many ways to achieve that, the idea is to set the text of the text view in its view controller's initialization.
So, you need a place to save the data entered by the user, and then read that data again in the view controller's initialization.
One way to do that is using a singleton class to save the data, like this for example:
class SharedData {
static let instance = SharedData()
var textEnteredByUser: String?
}
Now, when the user enters text, save that text in the string we have in 'SharedData' like this:
SharedData.instance.textEnteredByUser = textView.text!
Now, we have the data entered by the user saved, we need to set the data in the text view before it appears in the screen, and this can be achieved inside viewDidLoad method of your view controller like this:
override func viewDidLoad() {
super.viewDidLoad()
textView.text = SharedData.instance.textEnteredByUser
}
You can also save your data in user defaults instead of the singleton class like this:
UserDefaults.standard.set(textView.text!, forKey: "textEnteredByUser")
And then retrieve the saved text this way:
UserDefaults.standard.string(forKey: "textEnteredByUser")
Note: The singleton solution keeps the data shared between different view controllers, but does not persist data after closing the application, and this is what user default does.
I have several notifications on my app. What i want to do is to, when a local notification arrived, it erases another previous already showed one from notification center.
I am using the the following code.
static func unscheduleNotification(userInfoValue: String){
if let notifications = UIApplication.sharedApplication().scheduledLocalNotifications{
for notification in notifications{
if let userInfo = notification.userInfo as? [String:String]{
if userInfo["info"] == userInfoValue{
UIApplication.sharedApplication().cancelLocalNotification(notification)
}
}
}
}
}
Would appreciate if someone point me out the right direction here. If that is even possible.
I found the solution. Or one solution. Adding one repetition interval to the notification allows with the method i was using and posted on the question to delete it.
I don´t know if it is the correct procedure or not but it is indeed working.
If you want to receive 1 notification when getting another 1, then best idea to keep you 1st notification object in NSUserDefault, when getting another 1 notification, fetch already stored notification from NSUserDefault then remove it.
Consider example if your device is restarted after receiving 1st notification. then it will be in notification tray but you would not get object to cancel it.
Also scheduledLocalNotifications will not provide object after restating your device. scheduledLocalNotifications doesn't work properly always with iOS 8.
Refer - iOS 8 [UIApplication sharedApplication].scheduledLocalNotifications empty
While storing in NSUserDefault you have to convert notification object to NSData
let data = NSKeyedArchiver.archivedDataWithRootObject(notificationObject)
At the time of getting from NSUserDefault it will be NSData
So again convert to object
let notificationObject : UILocalNotification = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? UILocalNotification
I am creating a local notification system that allows someone to change the time that the local notification gets to them. I have a UIDatePicker and Switch.
I can't connect either of them to #IBActions so that they can perform the notification fireDate time change. It does not show in the pop-up as an option.
How do I get the switch and UIDatePicker to show IBAction?
I see that under module it displays "none". Either you need that to be set to your app, your that class "myDatePicker" doesn't exist in your app.
So I am fairly new to objective C and iOS programming. I am learning as I program and have a question. I am trying to modify a UITextField in my FirstViewController with my Second. How do I do this?
I have a UITextField name in FirstViewController and would like to be able to do something like firstViewController.name.text = #"new name"; inside of SecondViewController with a button and have the value changed so that if I switch tabs back to the FirstViewController I see the change has been made.
Thanks in advance for any help.
1、Set the FirstViewController as the delegate of SecondViewController;
2、Use NSNotificationCenter to notify the change event.
Well to call
firstViewController.name.text = #"new name"
You just need a reference to your first view controller inside of your second view controller. There are a number of ways to pass a reference around. Could go through the app delegate.
Another option that doesn't require you to have a reference to the first view controller inside the second view controller is to use NSNotificationCenter
Then you can have your second view controller observe a notification named say #"namechange" which contains the new name string.
When the button is pressed, you post a notification to the NSNotificationCenter with the new name attached. All the info is in the link above, or just google NSNotificationCenter for more tutorials and info.
I have implemented UILocalNotification. Everything is working fine and i can see the Notification also. I can see the notification and it shows me two button "cancel" and "View" from which i do not want "view" button can i do this?
How to do this?
Hi sorry for late reply you can Have only Ok button for UilocalNotifications
by doing this
notif.hasAction=NO;
by doing this you cannot see view button in the notifications Hope it solved your Problem
No. you can not do this. because local notification generated from ios u only change the name of view button by alertAction property.
i think its not possible to show one button in nslocal notification
notification.alertAction = NSLocalizedString(#"hello", nil); // its set instead of view to hello.
notification.alertAction = NSLocalizedString(nil, nil); // default view Name is coming For action Button to application