Multiple Feeds in a rss reader (swift) - xcode

I am creating a rss reader in xcode using swift. I am follwing this tut https://www.youtube.com/watch?v=jootsUaCvAU but in the tut he doesnt go over adding multiple feeds of your own for the user. Their is a slide out menu, but their is only a "add feed" button that the user can add their own feed.I have taken that out, and have put other feeds names on the slide out menu using "feedNames.append()", but all i need now is that the feed will open up after the user hits one of the feed names on the slide out menu. I think i know where the code goes, but i dont know what to put. Here is the code, for the slide out menu with the items. The code still has the "Add feed" implemented but just ignore that, cuz i am going to take that out.
func sideBarDidSelectMenuButtonAtIndex(index: Int) {
if index == 0{ // ADD FEED BUTTON
let alert = UIAlertController(title: "Add new feed", message: "Enter feed name and URL", preferredStyle: UIAlertControllerStyle.Alert)
alert.addTextFieldWithConfigurationHandler({ (textField:UITextField!) -> Void in
textField.placeholder = "Feed name"
})
alert.addTextFieldWithConfigurationHandler({ (textField:UITextField!) -> Void in
textField.placeholder = "Feed URL"
})
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: { (alertAction:UIAlertAction!) -> Void in
let textFields = alert.textFields
let feedNameTextField = textFields?.first as UITextField
let feedURLTextField = textFields?.last as UITextField
if feedNameTextField.text != "" && feedURLTextField.text != "" {
let moc = SwiftCoreDataHelper.managedObjectContext()
let feed = SwiftCoreDataHelper.insertManagedObject(NSStringFromClass(Feed), managedObjectConect: moc) as Feed
feed.name = feedNameTextField.text
feed.url = feedURLTextField.text
SwiftCoreDataHelper.saveManagedObjectContext(moc)
self.loadSavedFeeds()
}
}))
So i'm sure, the code goes here, but i am not for sure what to put. Thanks for your help!

func sideBarDidSelectMenuButtonAtIndex(index: Int)
if index == 0{
let url = NSURL(string: "http://www.widadclub.tk/feed")
let feedParser = MWFeedParser(feedURL: url)
feedParser.delegate = self
feedParser.parse()
}

Related

Refresh/Reload Eureka PushRow's pushed ViewController options with button?

I am using Eureka forms in my project.
I have a PushRow that presents the default SelectorViewController with a list of options. In the pushed view, I have added a rightBarButtonItem that points to locationSelectorAddButton ... on click, this brings up a UIAlertController that should allow users to add options to this pushed controller.
Is it possible, without creating my own custom selector controller, to refresh the current controller with the newly saved options from UserDefaults?
let defaults = UserDefaults.standard
func setupForm() {
form
+++ PushRow<String>(K.SESSIONFIELD.location) {
$0.title = K.SESSIONFIELD.location
$0.options = defaults.array(forKey: K.SESSIONFIELD.location) as? [String]
$0.value = sessionResult?.sessionLocation ?? $0.options?.first
}
.onPresent { from, to in
to.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named:"add_20pt"), style: UIBarButtonItemStyle.plain, target: from, action: #selector(self.locationSelectorAddButton(_:)))
}
}
the locationSelectorAddButton is implemented as follows:
#objc func locationSelectorAddButton(_ sender: UIBarButtonItem) {
var textField = UITextField()
let alert = UIAlertController(title: "Add New Location", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "Add", style: .default) { (action) in
var locArray = self.defaults.array(forKey: K.SESSIONFIELD.location) as? [String]
locArray?.append(textField.text!)
self.defaults.set(locArray, forKey: K.SESSIONFIELD.location)
///TODO: somehow refresh the pushed view controller here!!
// self.form.rowBy(tag: K.SESSIONFIELD.location)?.reload()
// print("this is the list of locations currently ...\(locArray)")
// self.tableView.reloadData()
///
}
alert.addTextField { (alertTextField) in
alertTextField.placeholder = "Location name ..."
textField = alertTextField
}
alert.addAction(action)
present(alert, animated:true, completion:nil)
}
Push row is not reloaded by using tag property. You need to implement the method
cellUpdate { cell, row in
row.options = (assign value here for updated options)
}

How to import ViewController data into email in Swift 3?

I am VERY new to Swift/xcode and I've built my first app but I'm stuck on one part. I have a button that opens an email and I would like to take the selections from a picker, two date pickers and a text input and import them into the email. I want the user to make their choices and just click send when the email opens. Everything works fine but I can't figure out how to import the data from the app into the email despite searching all over. Any suggestions?
}
func configuredMailComposeViewController() -> MFMailComposeViewController {
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["myemail#myemail.com"])
mailComposerVC.setSubject("EQUIPMENT RESERVATION REQUEST")
mailComposerVC.setMessageBody("test email message", isHTML: false)
return mailComposerVC
}
Basically, I want to replace the "test email message" string with the selections from my date pickers, picker and text input.
I was able to figure this out using the link from Kevin above and the following links:
How to store data from a picker view in a variable when a button is pressed?
Get just the date (no time) from UIDatePicker
The revised code is below and works exactly as I wanted it to:
#IBOutlet weak var StartDate: UIDatePicker!
#IBOutlet weak var EndDate: UIDatePicker!
#IBOutlet weak var HowMany: UITextField!
func configuredMailComposeViewController() -> MFMailComposeViewController {
let chosen = Picker1.selectedRow(inComponent: 0)
let chosenString = words[chosen]
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd"
let stringDate = dateFormatter.string(from: StartDate.date)
let Start = stringDate
let number = HowMany.text
let stringendDate = dateFormatter.string(from: EndDate.date)
let End = stringendDate
let mailComposerVC = MFMailComposeViewController()
mailComposerVC.mailComposeDelegate = self
mailComposerVC.setToRecipients(["myemail#example.com"])
mailComposerVC.setSubject("Email Subject")
mailComposerVC.setMessageBody ("Please reserve \(number ?? "") of
the following **\(chosenString)** from \(Start) to \(End).
Thanks.", isHTML: false)
return mailComposerVC
}

Send email In-game using sprite kit in xcode 7 beta3?

I am making an iPad game in sprite kit using swift in xcode 7beta3 and I want the results of the game to be send to the users email after the game is completed. The user should press a button called send and redirect to where they can type in their email-address and send the message. But I have no idea how to make and send an email.
I have been searching all around the internet for an answer to this question, but all are older version answers. I hope you can help.
Thanks in advance
EDIT:
I have been searching some more and i found a solution (here: http://kellyegan.net/sending-files-using-swift/), but I still have a problem. In my GameViewController i have added:
override func viewDidLoad() {
super.viewDidLoad()
let scene = StartGameScene(size: view.bounds.size)
let skView = view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
skView.presentScene(scene)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
internal func sendEmail() {
//Check to see the device can send email.
if( MFMailComposeViewController.canSendMail() ) {
print("Can send email.")
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
//Set the subject and message of the email
mailComposer.setSubject("Have you heard a swift?")
mailComposer.setMessageBody("This is what they sound like.", isHTML: false)
if let filePath = NSBundle.mainBundle().pathForResource("Math", ofType: "txt") {
print("File path loaded.")
if let fileData = NSData(contentsOfFile: filePath) {
print("File data loaded.")
mailComposer.addAttachmentData(fileData, mimeType: "text/plain", fileName: "Math")
}
}
self.presentViewController(mailComposer, animated: true, completion: nil)
}
}
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
self.dismissViewControllerAnimated(true, completion: nil)
}
The sendMail() is called in one of my gameScenes when you press a button.
The problem is that I get an error when I press that button. It prints out
Can send email.
File path loaded.
File data loaded.
as it should, but then it gives an error:
Could not cast value of type 'UIView' (0x1964ea508) to 'SKView' (0x19624f560).
I think the problem is the self.presentViewController(), but I have no idea how to fix it.

Swift NSUserNotificationCenter didActivateNotification not working

I'm working on a swift os x application and I'm having trouble understanding the userNoticiation / didActivateNotification structure. I've reviewed the documentation and searched SO but am still stuck. Any guidance on the following code would be much appreciated:
func notify(message: String, callBack: String) {
println(message)
println(callBack)
var notification:NSUserNotification = NSUserNotification()
notification.title = "New Phone Call"
notification.informativeText = message
notification.actionButtonTitle = "Lookup"
notification.hasActionButton = true
var center:NSUserNotificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter()
center.delegate = self
center.scheduleNotification(notification)
}
func notify (center: NSUserNotificationCenter, didActivateNotification notification: NSUserNotification){
center.delegate = self
println("clicked") //this does not print
}
The notification displays exactly as I'd like it to. Clicking the "Lookup" button that I've defined will bring my app to the foreground the first time it is clicked, but the code I'd expect to handle that action does not fire.
Thanks in advance.
Change your second 'func notify' declaration to 'optional func userNotificationCenter'

Swift UIAlertController Getting Text Field Text

I need to get the text from the text fields in my alert view when the Input button is pressed.
func inputMsg() {
var points = ""
var outOf = ""
var alertController = UIAlertController(title: "Input View", message: "Please Input Your Grade", preferredStyle: UIAlertControllerStyle.Alert)
let actionCancle = UIAlertAction(title: "Cancle", style: UIAlertActionStyle.Cancel) { ACTION in
println("Cacle")
}
let actionInput = UIAlertAction(title: "Input", style: UIAlertActionStyle.Default) { ACTION in
println("Input")
println(points)
println(outOf)
}
alertController.addAction(actionCancle)
alertController.addAction(actionInput)
alertController.addTextFieldWithConfigurationHandler({(txtField: UITextField!) in
txtField.placeholder = "I got"
txtField.keyboardType = UIKeyboardType.NumberPad
points = txtField.text
})
alertController.addTextFieldWithConfigurationHandler({(txtField: UITextField!) in
txtField.placeholder = "Out Of"
txtField.keyboardType = UIKeyboardType.NumberPad
outOf = txtField.text
})
presentViewController(alertController, animated: true, completion: nil)
}
As requested here is an implementation solution.
alertController.addAction(UIAlertAction(title: "Submit", style: UIAlertActionStyle.Default,handler: {
(alert: UIAlertAction!) in
if let textField = alertController.textFields?.first as? UITextField{
println(textField.text)
}
}))
As stated above, the alertController has a property called textFields. You can conditionally unwrap that property to safely access a text field if you have added one. In this case since there is only one text field I just did the unwrap using the first property. Hope it helps.
The UIAlertController has a textFields property. That's its text fields. Any of your handlers can examine it and thus can get the text from any of the text fields.

Resources