I have a JSON API I want my app to download in background.
My Quotes app send out notifications with timeintervals and I want the quotes to be in the notification.
struct Quotetype: Codable {
let text: String?
let author: String?
}
class ViewModel: ObservableObject {
#Published var quotes: [Quotetype]?
#Published var isLoading: Bool = true
var quoteText: String = ""
var quoteAuthor: String = ""
init() {
fetchQuotes()
setUpNotificationPermission()
setUpNotificationTriggers()
}
func fetchQuotes() {
guard let url = URL(string: "https://type.fit/api/quotes") else { return }
URLSession.shared.dataTask(with: url) { [weak self] data, response, error in
if error == nil {
if let data = data {
do {
let jsonContent = try JSONDecoder().decode([Quotetype].self, from: data)
DispatchQueue.main.async { [self] in
self?.quotes = jsonContent
if self?.quotes?.count != nil {
let randomNumber = Int.random(in: 0..<self!.quotes!.count )
self?.isLoading = false
self?.quoteText = self!.quotes?[randomNumber].text ?? ""
self?.quoteAuthor = self!.quotes?[randomNumber].author ?? ""
}
}
} catch {
print("\(error)")
}
} else {
print("Data nil")
}
} else {
print("err \(String(describing: error))")
}
}.resume()
}
func setUpNotificationTriggers() {
let content = UNMutableNotificationContent()
content.title = "Quote of the Day"
content.subtitle = quoteText
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}
this is my fetch & Notification. How can I get the quoteText in background so notification can go through?
please provide with examples.
Related
I am trying to create a list of text objects out of a function that returns an array of params. Everything seems to be working fine, getting the data, console shows the correct results, except the list itself which remains empty.
The function call:
import UIKit
import SwiftUI
struct SubdomainsList: View {
#State var SubDomains = VTData().funky(XDOMAIN: "giphy.com")
var body: some View {
VStack {
List{
Text("Subdomains")
ForEach(SubDomains, id: \.self) { SuDo in
Text(SuDo)
}
}
}
}
}
struct SubdomainsList_Previews: PreviewProvider {
static var previews: some View {
SubdomainsList()
}
}
The Json handlers:
struct VTResponse: Decodable {
let data: [VT]
}
struct VT: Decodable {
var id: String
}
The class:
class VTData {
func funky (XDOMAIN: String) -> Array<String>{
var arr = [""]
getDATA(XDOMAIN: "\(XDOMAIN)", userCompletionHandler: { (SubDomain) in
print(SubDomain)
arr.append(SubDomain)
return SubDomain
})
return arr
}
func getDATA(XDOMAIN: String, userCompletionHandler: #escaping ((String) -> String)) {
let token = "<TOKEN>"
guard let url = URL(string: "https://www.lalalla.com/subdomains") else {fatalError("Invalid URL")}
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("\(token)", forHTTPHeaderField: "x-apikey")
let task = URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in
guard let data = data else { return }
let decoder = JSONDecoder()
let result = try? decoder.decode(VTResponse.self, from: data)
if let result = result {
for SubDo in result.data {
let SubDomain = SubDo.id
userCompletionHandler(SubDomain)
}
}
else {
fatalError("Could not decode")
}
})
task.resume()
}
}
I'm getting no errors whatsoever, and the console output shows the correct results:
support.giphy.com
cookies.giphy.com
media3.giphy.com
i.giphy.com
api.giphy.com
developers.giphy.com
media.giphy.com
x-qa.giphy.com
media2.giphy.com
media0.giphy.com
It is also worth mentioning that when I add print(type(of: SubDomain)) to the code I'm getting a String rather than an array.
The preview:
preview
Any ideas?
try this approach, again, to extract the list of subdomain from your API, and display them in
a List using the asynchronous function getDATA(...):
class VTData {
// `func funky` is completely useless, remove it
func getDATA(XDOMAIN: String, completion: #escaping ([String]) -> Void) { // <-- here
let token = "<TOKEN>"
guard let url = URL(string: "https://www.virustotal.com/api/v3/domains/\(XDOMAIN)/subdomains") else {
print("Invalid URL")
return
}
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("\(token)", forHTTPHeaderField: "x-apikey")
URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else { return } // todo return some error msg
do {
let results = try JSONDecoder().decode(VTResponse.self, from: data)
return completion(results.data.map{ $0.id }) // <-- here
} catch {
print(error) // <-- here important
}
}.resume()
}
}
struct SubdomainsList: View {
#State var subDomains: [String] = [] // <--- here
var body: some View {
VStack {
List{
Text("Subdomains")
ForEach(subDomains, id: \.self) { SuDo in
Text(SuDo)
}
}
}
.onAppear {
// async function
VTData().getDATA(XDOMAIN: "giphy.com") { subs in // <--- here
subDomains = subs
}
}
}
}
Hey guys so I have a problem with decoding JSON. I decoded the entire string and made a statement in the ViewModel which fetches only elements as such
#Published var posts = [Post]()
var postsid6: [Post] {
var typeid6posts = [Post]()
for post in self.posts { if post.type_id == "6" { typeid6posts.append(post) }
}
return typeid6posts
}
So if it's type_id="6" it fetches them seperetly and I call the function like this
LazyHStack(alignment: .center){
ForEach(viewModel.postsid6) { post in
NavigationLink(destination: StoryDetailsView(post)) {
SingleStoryView(post: post, dateFormatter: viewModel.dateFormatter)
.padding(.bottom, 16)
}
}
}
Problem is for some reason swift fetches only 2 of them and there are 7 in the JSON with type_id="6"
This is the entire ViewModel. Posts are being updated but the typeid6posts isn't adding new ones
//
// PostsViewModel.swift
// EMDC App (iOS)
//
// Created by admin on 19.6.21..
//
import Foundation
class PostsViewModel: ObservableObject{
lazy var dateFormatter: DateFormatter = {
let df = DateFormatter()
df.doesRelativeDateFormatting = true
df.timeStyle = .short
df.dateStyle = .short
return df
}()
struct PostsResponse: Codable{
let items: [Post]
let users: [String : UserModel]
}
#Published var posts = [Post]()
var postsid6: [Post] {
var typeid6posts = [Post]()
for post in self.posts { if post.type_id == "6" { typeid6posts.append(post) }
}
return typeid6posts
}
var videibgt: [Post] {
var videiisat = [Post]()
for post in self.posts { if (post.videourl != nil) == true { videiisat.append(post) } }
return videiisat
}
private let jsonDecoder = JSONDecoder()
func fetchPosts(){
let url = URL(string: "https://cdn.rapttor.com/influencer/data/2/posts.json")!
URLSession.shared.dataTask(with: url) { data, response, error in
guard error == nil else {
return
}
guard let data = data else {
return
}
do{
let decoded = try self.jsonDecoder.decode(PostsResponse.self, from: data)
DispatchQueue.main.async {
let posts = decoded.items
var postsWithUser = [Post]()
for var post in posts{
post.user = decoded.users[post.userId]!
postsWithUser.append(post)
}
self.posts.append(contentsOf: postsWithUser)
print(self.posts.count)
}
}
catch{
print(error)
}
}.resume()
}
func duplicate(){
posts.append(contentsOf: posts)
}
}
I created a local json. I change the value of the name key in the json and when I close and open the application, it says "Test" again. How can I save the change I made on the Json file?
Why can't I save the string value? I shared all the codes with you. If you want I can share the project.
Local JSON File
{
"person": {
"name": "Test"
}
}
Model
struct PersonContainer: Codable {
var person: Person?
}
struct Person: Codable {
var name: String?
}
JSON Provider
class JSONProvider: ObservableObject {
#Published var personContainer: PersonContainer = PersonContainer()
var fm = FileManager.default
var fresult: Bool = false
#Published var subUrl: URL? = URL(string: "")
var mainUrl: URL? = Bundle.main.url(forResource: "test", withExtension: "json")
func getData() {
do {
let documentDirectory = try fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
subUrl = documentDirectory.appendingPathComponent("test.json")
loadFile(mainPath: mainUrl!, subPath: subUrl!)
} catch {
print(error)
}
}
func loadFile(mainPath: URL, subPath: URL){
if fm.fileExists(atPath: subPath.path){
decodeData(pathName: subPath)
if ((personContainer.person) != nil) {
decodeData(pathName: mainPath)
}
}else{
decodeData(pathName: mainPath)
}
}
func decodeData(pathName: URL){
do{
let jsonData = try Data(contentsOf: pathName)
let decoder = JSONDecoder()
let personContainer = try decoder.decode(PersonContainer.self, from: jsonData)
self.personContainer = personContainer
} catch {}
}
func writeToFile(location: URL) {
do{
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let JsonData = try encoder.encode(personContainer)
try JsonData.write(to: location)
} catch {
}
}
}
ContentView
struct ContentView: View {
#State var text: String = ""
#ObservedObject var jsonProvider: JSONProvider = JSONProvider()
var body: some View {
VStack {
TextField("Placeholder", text: $text)
.padding()
.background(Color(UIColor.secondarySystemBackground))
.cornerRadius(15)
.padding(.horizontal)
Text("Hello, world! \(jsonProvider.personContainer.person?.name ?? "")")
.padding()
Button(action: {
jsonProvider.personContainer.person?.name = text
jsonProvider.writeToFile(location: jsonProvider.subUrl!)
}) {
Text("Button")
}
}
.onAppear {
jsonProvider.getData()
}
}
}
Looks like you were on the right track, but there were a few things missing.
Since the original main bundle's test.json should only be loaded if the file in the documents directory doesn't exist, a lot of the logic can be simplified. For example, you can remove the #Published subUrl, since it never gets changed and isn't observed by the View.
Make sure that you call the writeToFile when the button is pressed.
Also, it's always a good idea to do something (like printing the error) inside the catch blocks in case something has gone wrong.
class JSONProvider: ObservableObject {
#Published var personContainer: PersonContainer = PersonContainer()
private var fm = FileManager.default
private let mainUrl: URL = Bundle.main.url(forResource: "test", withExtension: "json")!
func getData() {
if fm.fileExists(atPath: documentDirectoryJSONURL().path) {
decodeData(fromURL: documentDirectoryJSONURL())
} else {
decodeData(fromURL: mainUrl)
}
}
func documentDirectoryJSONURL() -> URL {
do {
let documentDirectory = try fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
return documentDirectory.appendingPathComponent("test.json")
} catch {
fatalError("Couldn't create URL")
}
}
func decodeData(fromURL url: URL){
do{
let jsonData = try Data(contentsOf: url)
let decoder = JSONDecoder()
let personContainer = try decoder.decode(PersonContainer.self, from: jsonData)
self.personContainer = personContainer
} catch {
print(error)
assertionFailure("Error decoding JSON")
}
}
func writeToFile() {
do{
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let jsonData = try encoder.encode(personContainer)
try jsonData.write(to: documentDirectoryJSONURL())
} catch {
print(error)
}
}
}
struct ContentView: View {
#State var text: String = ""
#ObservedObject var jsonProvider: JSONProvider = JSONProvider()
var body: some View {
VStack {
TextField("Placeholder", text: $text)
.padding()
.background(Color(UIColor.secondarySystemBackground))
.cornerRadius(15)
.padding(.horizontal)
Text("Hello, world! \(jsonProvider.personContainer.person?.name ?? "")")
.padding()
Button(action: {
jsonProvider.personContainer.person?.name = text
jsonProvider.writeToFile()
}) {
Text("Write")
}
}
.onAppear {
jsonProvider.getData()
}
}
}
I want this to update every 4 seconds with fresh data form the url, but i dont know how to do this. This is what i have so far and it works fine but without the refresher! The Refresher needs to work like a youtube subscriber counter that update every 4 seconds or so. I have looked at a timer but i couldn't make it work because (i think) its a searchBarSearchButtonClicked function and the urlRequestid has to have a input! Please help! Thanks!
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
let urlRequestid = URLRequest(url: URL(string: "https://www.mylink.com/\(searchBar.text!.replacingOccurrences(of: " ", with: "%20"))/?__a=1")!)
if (interstitial.isReady){
interstitial.present(fromRootViewController: self)
interstitial = createAndLoadInterstitial()
}
let task = URLSession.shared.dataTask(with: urlRequestid) { (data, response, error) in
if error == nil {
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String : AnyObject]
if let user = json["user"] as? [String : AnyObject] {
let profile_pic_url_hd = user["profile_pic_url_hd"] as! String
let urlstr = "\(profile_pic_url_hd)"
if var comps = URLComponents(string: urlstr) {
var path = comps.path
var pathComps = path.components(separatedBy: "/")
pathComps.remove(at: 2) // this removes the s320x320
path = pathComps.joined(separator: "/")
comps.path = path
if let newStr = comps.string {
print(newStr)
self.imgURL = "\(newStr)"
}
}
if let bio = user["biography"] as? String {
self.bioS = bio
}
if let naam = user["username"] as? String {
self.naamS = naam
}
if let followed_by = user["followed_by"] as? [String : AnyObject] {
self.VolgS = followed_by["count"] as! Int
}
if let follows = user["follows"] as? [String : AnyObject] {
self.volgD = follows["count"] as! Int
}
if let media = user["media"] as? [String : AnyObject] {
self.postS = media["count"] as! Int
}
}
if let _ = json["error"] {
self.exists = false
}
DispatchQueue.main.async {
if self.exists{
self.imgView.downloadImage(from: self.imgURL!)
self.naam.text = "#\(self.naamS ?? "")"
if self.bioS == nil {
self.bio.text = "This Person has no biography!"
} else {
self.bio.text = "\(self.bioS ?? "")"
}
self.volgers.text = "\(self.VolgS!)"
self.volgend.text = "\(self.volgD!)"
self.post.text = "\(self.postS!)"
} else {
self.exists = true
}
}
} catch let jsonError {
print(jsonError.localizedDescription)
}
}
}
task.resume()
}
}
One quick but admittedly clumsy fix would be to store the latest UISearchBar instance from the searchBarSearchButtonClicked parameter in a local instance variable:
var currentSearch: UISearchBar = UISearchBar()
var timer: Timer?
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
currentSearch = searchBar
// Add the rest of the method code below...
...
}
// Call this method to begin repetition
func repeatSearch() {
self.timer = Timer.scheduledTimer(withTimeInterval: 4.0, repeats: true,
block: { (timer) in
self.searchBarSearchButtonClicked(self.currentSearch)
})
}
You can achieve it by using the Timer, schedule it for every 4 seconds.
DEMO
FOR iOS 10.0 and Above
var timer: Timer?
func callMe() {
func doSomrThing(str: String) {
print(str)
}
doSomrThing(str: "first time")
self.timer = Timer.scheduledTimer(withTimeInterval: 4.0, repeats: true, block: { (timer) in
doSomrThing(str: "after 4 second")
})
}
For below iOS 10.0
var timer: Timer?
func callMe() {
self.doSomeThing(str: "first time")
self.timer = Timer.scheduledTimer(timeInterval: 4.0, target: self, selector: #selector(AddTextVC.timerHandler), userInfo: nil, repeats: true)
}
func doSomeThing(str: String) {
print(str)
}
func timerHandler() {
self.doSomeThing(str: "after 4 seconds")
}
Just replace your code according to the demo.
And add this code to your viewController :
deinit {
self.timer?.invalidate()
self.timer = nil
}
I have a function called getEarthquake() that parses JSON using SwiftyJSON and returns all of the organized information (such as title, magnitude, and time) into an NSMutableArray called info.
var info = NSMutableArray()
func getEarthquake(completion: (results : NSMutableArray) ->Void) {
DataManager.getEarthquakeDataFromFileWithSuccess {
(data) -> Void in
let json = JSON(data: data)
if var JsonArray = json.array {
JsonArray.removeAtIndex(0)
for appDict in JsonArray {
var mag: String? = appDict["mag"].stringValue
var title: String? = appDict["title"].stringValue
var time: String? = appDict["time"].stringValue
var information = AppModel(title: title, magnitude: mag, time1: time)
info.addObject(information)
// info.removeRange(3...48)
completion(results: info)
}
}
}
}
I created another function called getEarthquake2() which calls getEarthquake() and retrieves info. In getEarthquake2() I want it to return only title1, which is a String. However my attempts result only in title1 being nil by the time is it returned.
func getEarthquake2()->String? {
var title1: String?
getEarthquake{ (info) in
var title = info[0].title
title1 = title
}
return title1
}
Can someone guide me in the right direction upon making getEarthquake2()successfully return title1 that doesn't return nil? (I'm sure it's not a matter of Info being nil, as it gets populated at the end of getEarthquake().)
My AppModel.swift file where I can easily organize my code:
import Foundation
class AppModel: NSObject, Printable {
let title: String
let magnitude: String
let time1: String
override var description: String {
return "TITLE: \(title), TIME: \(time1), MAG: \(magnitude)"
}
init(title: String?, magnitude: String?, time1: String?) {
self.title = title ?? ""
self.time1 = time1 ?? ""
self.magnitude = magnitude ?? ""
}
}
My DataManager.swift file where I call the web service:
import Foundation
let earthquakeURL = "http://www.kuakes.com/json/"
class DataManager {
class func getEarthquakeDataFromFileWithSuccess(success: ((websiteData: NSData) -> Void)) {
//1
loadDataFromURL(NSURL(string: earthquakeURL)!, completion:{(data, error) -> Void in
//2
if let urlData = data {
//3
success(websiteData: urlData)
}
else {
println("nothing")
}
})
}
class func loadDataFromURL(url: NSURL, completion:(data: NSData?, error: NSError?) -> Void) {
var session = NSURLSession.sharedSession()
// Use NSURLSession to get data from an NSURL
let loadDataTask = session.dataTaskWithURL(url, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
if let responseError = error {
completion(data: nil, error: responseError)
} else if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode != 200 {
var statusError = NSError(domain:"com.kuakes", code:httpResponse.statusCode, userInfo:[NSLocalizedDescriptionKey : "HTTP status code has unexpected value."])
completion(data: nil, error: statusError)
} else {
completion(data: data, error: nil)
}
}
})
loadDataTask.resume()
}
}
In getEarthquake2 you forget to cast your object to AppModel:
func getEarthquake2() -> String? {
var title1: String?
getEarthquake { info in
let title = (info[0] as! AppModel).title
title1 = title
}
return title1
}