我正在通過react-native. 因此,我使用react-native-webview圖書館。但是,“window.open”和“window.close”在react-native-webview.
我需要將那部分代碼應用于social login. 于是我找到了swift代碼檔案。但是,我不知道如何將此檔案更改為objective-c代碼。
react-native-webview的object-c部分代碼
- 快速檔案
// webView list management
var webViews = [WKWebView]()
...
func webView(_ webView: WKWebView,
createWebViewWith configuration: WKWebViewConfiguration,
for navigationAction: WKNavigationAction,
windowFeatures: WKWindowFeatures
) -> WKWebView? {
guard let frame = self.webViews.last?.frame else {
return nil
}
//Creating and returning a web view creates a parent relationship with the current web view.
return createWebView(frame: frame, configuration: configuration)
}
/// ---------- popup close ----------
func webViewDidClose(_ webView: WKWebView) {
destroyCurrentWebView()
}
// Examples of Web View Generation Methods
func createWebView(frame: CGRect, configuration: WKWebViewConfiguration) -> WKWebView {
let webView = WKWebView(frame: frame, configuration: configuration)
// set delegate
webView.uiDelegate = self
webView.navigationDelegate = self
// add view
self.view.addSubview(webView)
self.webViews.append(webView)
return webView
}
// Examples of webview deletion methods
func destroyCurrentWebView() {
// remove from webview lists and screens
self.webViews.popLast()?.removeFromSuperview()
}
我如何應用此代碼以適應react-native-webview?
編輯
- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];
}
WKWebView *popUpWebView = [[WKWebView alloc] initWithFrame: navigationAction.targetFrame configuration: configuration];
popUpWebView.uiDelegate = self;
popUpWebView.navigationDelegate = self;
[_webView addSubview:popUpWebView];
return nil;
}
- (void)webViewDidClose:(WKWebView *)webView {
[_webView removeFromSuperview];
}
我查看了檔案并將其更改如下。但是,在構建時會發生錯誤。我不知道問題是什么。
uj5u.com熱心網友回復:
正如我在評論中提到的,我不確定這是否適用于 React Native,但這個 Obj-C 代碼與您的 swift 代碼相同并且應該編譯
在您的 .h 檔案中
您的.h檔案可能需要與RNCWebView.h相同,并且您可能需要洗掉任何不需要/未使用的內容
在您的 .m 檔案中
同樣,您.m將類似于RNCWebView.m并洗掉您不使用的內容。
然后根據您的 swift 代碼,這些是這些函式的更新 Obj C 版本
- (WKWebView *)webView:(WKWebView *)webView
createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration
forNavigationAction:(WKNavigationAction *)navigationAction
windowFeatures:(WKWindowFeatures *)windowFeatures
{
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];
}
if ([webViews count] == 0) {
return nil;
}
WKWebView *currentWebView = [webViews lastObject];
WKWebView *popUpWebView = [[WKWebView alloc] initWithFrame: currentWebView.frame
configuration: configuration];
popUpWebView.UIDelegate = self;
popUpWebView.navigationDelegate = self;
[webView addSubview:popUpWebView];
return popUpWebView;
}
- (void)webViewDidClose:(WKWebView *)webView
{
[webView removeFromSuperview];
}
更新
如果webViews原始 swift 代碼中的變數未使用/不需要,您可能需要更新webView createWebViewWithConfiguration如下:
- (WKWebView *)webView:(WKWebView *)webView
createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration
forNavigationAction:(WKNavigationAction *)navigationAction
windowFeatures:(WKWindowFeatures *)windowFeatures
{
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];
}
WKWebView *popUpWebView = [[WKWebView alloc] initWithFrame: webView.bounds
configuration: configuration];
popUpWebView.UIDelegate = self;
popUpWebView.navigationDelegate = self;
[webView addSubview:popUpWebView];
return popUpWebView;
}
最后,澄清一下:
The header does not need to be the same as mine, I just gave you an example if you were subclassing a UIViewController. You probably need to follow the header and implementation file defined here
My goal was to convert your swift code into Obj C code that would compile, I cannot say if it is right for React Native however.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/445613.html
