UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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,

3      

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.

3      

@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!

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.