简体   繁体   English

RSS Widget For iOS 14 显示为空

[英]RSS Widget For iOS 14 Shows Empty

I use an XMLParser for my app written in Swift, and am trying to get a Widget set up for iOS 14 in which it will show the most recent article on the RSS.我在 Swift 中为我的应用程序使用了一个 XMLParser,并且正在尝试为 iOS 14 设置一个小部件,它将在其中显示 RSS 上的最新文章。 I see the placeholder just fine when I add it, but it stays empty with just a line where text should be and never seems to get the information from the timeline.添加时我看到占位符很好,但它仍然是空的,只有一行文本应该是文本,而且似乎永远不会从时间轴中获取信息。

struct Provider: TimelineProvider {
    @State private var rssItems:[RSSItem]?
    let feedParser = FeedParser()
    func placeholder(in context: Context) -> SimpleEntry {
        SimpleEntry(date: Date(), title:"News", description: "News article here", link: "Http://link", pubDate: "The day it posted")
    }

    func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
        let entry = SimpleEntry(date: Date(), title:"News", description: "News Article Here", link: "Http://link", pubDate: "The day it posted")
        completion(entry)
    }

    func getTimeline(in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> ()) {
        var entries: [SimpleEntry] = []
        feedParser.parseFeed(url: "http://fritchcoc.wordpress.com/feed") {(rssItems) in
            self.rssItems = rssItems
            let currentDate = Date()
                let entry = SimpleEntry(date: currentDate, title:rssItems[0].title, description: rssItems[0].description, link: rssItems[0].link, pubDate: rssItems[0].pubDate)
                entries.append(entry)
            
        
      
        }

        let timeline = Timeline(entries: entries, policy: .atEnd)
        completion(timeline)
    }
}

struct SimpleEntry: TimelineEntry {
    let date: Date
    let title: String
    let description: String
    let link: String
    let pubDate: String
}

struct FritchNewsEntryView : View {
    var entry: SimpleEntry
    var body: some View {
            VStack(alignment: .leading, spacing: 4) {
                
                Text(entry.title)
               
                   
                
              
            }.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .leading)
                .padding()
                .background(LinearGradient(gradient: Gradient(colors: [.orange, .yellow]), startPoint: .top, endPoint: .bottom))
        }
    
}

@main
struct FritchNews: Widget {
    let kind: String = "FritchNews"

    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: Provider()) { entry in
            FritchNewsEntryView(entry: entry)
        }
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
    }
}

UPDATE BY THE OP: It was indeed a combination of the timeline being set in the wrong spot, along with a couple of typos within the parser that caused this particular issue. OP 更新:确实是时间轴设置在错误位置的组合,以及导致此特定问题的解析器中的几个拼写错误。

Apple does not allow HTTP connections by default, the easiest solution is to change the URL to https://fritchcoc.wordpress.com/feed Apple 默认不允许 HTTP 连接,最简单的解决方案是将 URL 更改为https://fritchcoc.wordpress.com/feed

If you want to allow HTTP connections in your app (or widget) you can add exceptions (to one domain or all) in the Info.plist for the target, see NSAppTransportSecurity at Apple Docs如果您想在您的应用程序(或小部件)中允许 HTTP 连接,您可以在目标的 Info.plist 中添加例外(到一个域或所有域),请参阅Apple Docs上的 NSAppTransportSecurity

Edit:编辑:
After a better look and finding for the implementation of FeedParser online, I noticed that parseFeed() is asynchronous.在仔细查看并在线查找FeedParser的实现后,我注意到parseFeed()是异步的。

Thus the completion is called with an empty array, it should be called after the parsing is done:因此用一个空数组调用完成,它应该在解析完成后调用:

struct Provider: TimelineProvider {
    ...

    func getTimeline(in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> ()) {
        var entries: [SimpleEntry] = []
        feedParser.parseFeed(url: "http://fritchcoc.wordpress.com/feed") {(rssItems) in
            self.rssItems = rssItems
            let currentDate = Date()
            let entry = SimpleEntry(date: currentDate, title:rssItems[0].title, description: rssItems[0].description, link: rssItems[0].link, pubDate: rssItems[0].pubDate)
            entries.append(entry)
            
        
            let timeline = Timeline(entries: entries, policy: .atEnd)
            completion(timeline)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM