Search code examples
jsonswiftmobile-safari

How to read from blockerList.json


Hi I wanted to write a simple application that blocks websites in safari. I created a new target( Content Blocker extension) and so it created this blockerList.json file in new blocker folder. So I wanted to read sites from that file into an array and show them in a tableview and to add new sites or delete sites from blockerList.json. Here is my code

import UIKit
import Cartography
import SwiftyJSON
import SafariServices



class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView: UITableView = UITableView()
var arr = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    super.view.backgroundColor = UIColor.red;
    //let blockerIdentifier = "com.appsfoundation.ContentBlocker.Blocker"
    //SFContentBlockerManager.
    print("--------------------")
    //print(FileManager.default.c)
    //Bundle(for: ContentBlockerRequestHandler)

    print("--------------------")
    //Bundle.main.url
    //Bundle.main.pa
    //FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: <#T##String#>)
   // print(Bundle.main.url(forResource: "blocker/blockerList", withExtension: "json"))
   // Bundle.init(for: ContentBlockerRequestHandler)
   // print(Bundle.main.paths(forResourcesOfType: "blockerList.json", inDirectory: "blocker"))
    if let path = Bundle.main.path(forResource: "blockerList", ofType: "json", inDirectory: "./../blocker"){
        guard let data = NSData(contentsOf: URL(fileURLWithPath: path)) else{
            return
        }
         let jsonObj = JSON(data: data as Data)
        print(jsonObj)
        if jsonObj != JSON.null {
                        print("jsonData:\(jsonObj)")
                    } else {
                        print("Could not get json from file, make sure that file contains valid json.")
                    }
        //


    }
    arr.append("Hello")
    arr.append("my name is ")
    let button = UIButton();
    button.setTitle("+", for: .normal)
    button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
    // button.titleLabel?.text = "+"

    self.view.addSubview(tableView)
    self.view.addSubview(button)
    tableView.delegate = self
    tableView.dataSource = self
    constrain(tableView, view, button){
        tableView, view, button in
        tableView.width == view.width
        tableView.height == view.height - 50
        tableView.top == view.top
        tableView.right == view.right
        button.height == 50
        button.width == view.width
        button.top == view.bottom - 50
        button.right == view.right

    }
    //arr = Array()
    // Do any additional setup after loading the view, typically from a nib.
    // self.view.backgroundColor = UIColor.redColor();
}
func buttonPressed(){
    // print("I was pressed")
    //var alert = UIAlertView();
    //alert.title = "Enter a website";
    // alert.alertViewStyle = UIAlertViewStyle.plainTextInput
    // alert.addB
    //alert.addButton(withTitle: "Done")
    //alert.addButton(withTitle: "Cancel")
    var alert = UIAlertController(title: "Alert", message: "Enter website that you would like to block", preferredStyle: UIAlertControllerStyle.alert)
    alert.addTextField { (text) in
        text.placeholder = "http://";
    }
    let action = UIAlertAction(title: "Done", style: UIAlertActionStyle.default) { (action: UIAlertAction) in
        print("I am working")
    }
    let action2 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { (UIAlertAction) in
        self.dismiss(animated: true, completion: nil)
    }
    alert.addAction(action)
    alert.addAction(action2)
    self.present(alert, animated: true, completion: nil)
    //self.viewC
    //presentedViewController(alert)
    //alert.addA
    // alert.show()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

    // Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return arr.count

}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
    return 50
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    // Code here
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
    //cell.t
    //let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell")
    //cell.textLabel?.text = "Hello"
    cell.textLabel?.text = arr[(indexPath as NSIndexPath).item]
    return cell;
}


}

But the problem is I cannot access blockerList.json file. Is it possible at all? Or am I doing something completely wrong. I am searching this for 3 days and I have no ideas how to solve this problem. Thanks for any help in advance.


Solution

  • I solved my problem if you have this problem it turns out that this json is not added to Build Phase. So you need to go to your project-> Build phase->Copy files->add blockerList.json here. Hope this helps if you have the same problem :)