Switf: Cocoa - How to handle notification from webview to display it into local notification

心已入冬 提交于 2020-01-25 08:50:25

问题


I am creating an application with a webview. I want to display the browser notification on the desktop in local notification. I found a way to do it but I do not like it too much. This project https://github.com/jnordberg/irccloudapp does this, it has a NotificationProvider it's a private Webview protocol.

What I did was I created an abase application of this project. The notificationProvider is written in Objc and the rest of my application I passed it in swift, so in my project I combine the 2 language with a bridging-Header

So I managed to display the notification browsers locally.

I am facing 2 problems.

The first is that I do not like to combine Objc and swift I would like to spend all the project in swift but I can not translate it because it is a private protocol and there is some object that I do not can not use for example WebSecurityOrigin or WebNotificationPrivate. In Objc it's easy because the objects are defined like this:

@class WebNotificationPrivate;
@class WebSecurityOrigin;

Here are the protocols of the NotificatrionProvider:

@protocol WebNotificationProvider
- (void)registerWebView:(WebView *)webView;
- (void)unregisterWebView:(WebView *)webView;
- (void)showNotification:(WebNotification *)notification fromWebView:(WebView *)webView;
- (void)cancelNotification:(WebNotification *)notification;
- (void)notificationDestroyed:(WebNotification *)notification;
- (void)clearNotifications:(NSArray *)notificationIDs;
- (WebNotificationPermission)policyForOrigin:(WebSecurityOrigin *)origin;

- (void)webView:(WebView *)webView didShowNotification:(uint64_t)notificationID;
- (void)webView:(WebView *)webView didClickNotification:(uint64_t)notificationID;
- (void)webView:(WebView *)webView didCloseNotifications:(NSArray *)notificationIDs;
@end

@interface WebView (WebViewNotification)
- (void)_setNotificationProvider:(id<WebNotificationProvider>)notificationProvider;
- (id<WebNotificationProvider>)_notificationProvider;
- (void)_notificationControllerDestroyed;

- (void)_notificationDidShow:(uint64_t)notificationID;
- (void)_notificationDidClick:(uint64_t)notificationID;
- (void)_notificationsDidClose:(NSArray *)notificationIDs;
@end

We can find the protocol ir https://opensource.apple.com/source/WebKit/WebKit-7536.30.1/mac/WebView/WebViewPrivate.h.auto.html

The second thing is that WebView is deprecated, is there the equivalent for WKWebView? And how to implement it?

来源:https://stackoverflow.com/questions/58216918/switf-cocoa-how-to-handle-notification-from-webview-to-display-it-into-local

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!