簡體   English   中英

為什么在 MacOS 上使用 WebKit 構建時會出現“預期平台名稱”錯誤?

[英]Why do I get an “Expected a Platform Name” error when building with WebKit on MacOS?

我最近將我的構建系統更新為 macOS 10.15 和 Xcode 11.3.1,但是當我嘗試編譯我的一個程序時,我收到“預期的平台名稱,例如'macos'”錯誤並且構建停止。 我已經確定只有在使用 WebKit 構建時才會出現此問題 - 如果我注釋掉該代碼,那么軟件將正確構建並運行(當然,除了能夠顯示 HTML 之外)。 我想知道問題是否可能出在我使用的 WebViews 上——它已被棄用——所以我將代碼更新為 WKWebView,但問題仍然存在。 當然,由於我收到此錯誤,我不知道我是否已正確更新到 WKWebView - 它不會運行,並且我的這部分代碼沒有顯示任何其他錯誤。

HTMLViewController.h:

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#import "ReadEML.h"

@interface HTMLViewController : NSViewController //<WebPolicyDelegate>
{
    NSSize dpi; 
}
@property (assign) IBOutlet WKWebView *mailContentView;

- (WKWebView*)contentView;
- (void)displayContentFromReader:(id)emlReader;
- (void)setFormattingOn:(bool)formatting;

- (void)webView:(WKWebView *)webView
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;

@end

HTMLViewController.m

#import "HTMLViewController.h"
#import "Webtools.h"
#import "Stringtools.h"

@interface HTMLViewController ()

@end

@implementation HTMLViewController

- (void)awakeFromNib {
    CGDirectDisplayID displayID = [_mailContentView.window.screen.deviceDescription[@"NSScreenNumber"] unsignedIntValue];
    CGSize size = CGDisplayScreenSize(displayID);
    CGDisplayModeRef mode = CGDisplayCopyDisplayMode(displayID);

    dpi.width = CGDisplayModeGetWidth(mode) * 25.4 / size.width;
    dpi.height = CGDisplayModeGetHeight(mode) * 25.4 / size.height;
    CGDisplayModeRelease(mode);

    float scaling = [AppDelegate.sharedAppdelegate getPreferenceForKey:@"DefaultZoom"]?[[AppDelegate.sharedAppdelegate getPreferenceForKey:@"DefaultZoom"] intValue]:100;

    [_mailContentView.webFrame.frameView.documentView scaleUnitSquareToSize:NSMakeSize(dpi.width / (dpi.width / (scaling / 100.0)), dpi.height / (dpi.height / (scaling / 100.0)))];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    return self;
}

- (WKWebView*)contentView {
    return _mailContentView;
}

- (void)webView:(WKWebView *)webView
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    if (navigationAction.request.URL) {
        decisionHandler(WKNavigationActionPolicyCancel);
        [NSWorkspace.sharedWorkspace openURL:navigationAction.request.URL];
    } else {
        decisionHandler(WKNavigationActionPolicyAllow);
    }
}

- (void)displayContentFromReader:(id)dataCollector {
    @autoreleasepool {        
        NSString* pathStem = getTempCacheDirectory();
        NSURL* rootURL = [NSURL URLWithString:[pathStem stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSURL* ReadRootURL = [NSURL fileURLWithPath:[pathStem stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

        [_mailContentView.webFrame loadHTMLString:[dataCollector getContent:@"RawHTML"] baseURL:ReadRootURL];
    }
}

@end

我的直覺告訴我,這個特定問題不在我的代碼中 - 任何人都可以提出其他可能發生的事情嗎? 正如我所說,如果我不導入 .h 文件並刪除代碼中對其用法的所有引用,那么代碼編譯不會出現問題。

它在 macOS 10.14 上也可以毫無問題地編譯。

我很困惑!

這可能會幫助一些人解決這個特定問題——我找到了一個適合我的解決方案,但我對此並不滿意,因為它感覺很老套。

只需將以下定義添加到您的 header 中,您的代碼應該可以毫無困難地編譯。 我仍然有興趣了解為什么 Apple 會改變這一點,無論是故意的還是錯誤的,以及“正確”的解決方案應該是什么。

#define TARGET_OS_MACCATALYST 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM