NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

SOLVED: 100 days of Swift - Day 69 - Project 19 - Sarafi Extension - Challenge 3

Forums > 100 Days of Swift

@teine  

  1. For something bigger, let users name their scripts, then select one to load using a UITableView.

Ok, how can i get the name/selected script back to main ActionViewController?

A) create a property for the ActionViewController in my TableViewController, and have the script textview accesible? (or perhaps a variable I can set from the TVC, then check/load/empty when the parent actionView willAppear...?

B) make my ActionViewController a tableViewController, and ... hmm i dunno this seems to lead to an overly big view controller?

C) Write the return value to user default, and load in ActionView viewWillAppear?

I can send data to the tableView when i instantiate it, but how should i send data back?

but is there a nice way to send back data?

thanks,

   

Hope that I don't spoil you fun of figuring it out yourself, I'm not even sure if my approach was the right one, but here it goes:

First of all, you DON'T have to go back to ActionViewController. I just pasted the lines that are needed to execute the script, straight to TableViewController:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = NSExtensionItem()
        let argument: NSDictionary = ["customJavaScript": tableScripts[indexPath.row].tableScript]
        let webDictionary: NSDictionary = [NSExtensionJavaScriptFinalizeArgumentKey: argument]
        let customJavaScript = NSItemProvider(item: webDictionary, typeIdentifier: kUTTypePropertyList as String)
        item.attachments = [customJavaScript]
        extensionContext?.completeRequest(returningItems: [item])
}

You might think what tableScripts[indexPath.row].tableScript in the line 3 is. Well, it's from the custom class I made to store the written scripts along with their names:

class scriptSavedForTableView: NSObject, Codable {
    var tableName: String?
    var tableScript: String?

    init (tableName: String, tableScript: String) {
        self.tableName = tableName
        self.tableScript = tableScript
    }
}

tableScripts then is just a var of [scriptSavedForTableView].

So, having a script saved to a table view, when you click a row, the view controller is automatically dismissed and the script is executed.

   

@teine  

Good hint! I got it from the first line.

I'm quite good at pausing, trying things out then continuing to what was actually recomended :D

Being a bit wary of duplicating code, I pulled out the code into a new swift file, but the extensionContext became an unresolved identifier, so.. I just passed as a function parameter... hope that doesn't lead to madness.. but cool.

Thanks for your time!

   

Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.