您好我想写一个简单的应用程序来阻止Safari中的网站。我创建了一个新目标(Content Blocker扩展),因此它在新的阻止文件夹中创建了这个blockerList.json文件。因此,我想将该文件中的网站读入数组,并在tableview中显示它们,并添加新网站或从blockerList.json中删除网站。这是我的代码
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;
}
}
但问题是我无法访问blockerList.json文件。有可能吗?或者我做错了什么。我正在搜索这个3天,我不知道如何解决这个问题。感谢您提前提供任何帮助。
答案 0 :(得分:1)
如果你遇到这个问题我解决了我的问题,事实证明这个json没有添加到Build Phase。所以你需要去你的项目 - &gt;构建阶段 - &gt;复制文件 - &gt;在此处添加blockerList.json。希望如果您遇到同样的问题,这会有所帮助:)