问题
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