macOS WKwebview的簡單實現


1.最簡單的,只顯示內容沒有交互。

 

 復制代碼即可運行,可拖拽窗口。

#import "ViewController.h"
#import <WebKit/WebKit.h>
@interface ViewController ()
@property (nonatomic,strong)WKWebView * webView;
@end

@implementation ViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    //觀察窗口拉伸
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(screenResize)
                                                name:NSWindowDidResizeNotification
                                              object:nil];

    //初始化
    _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    //加載出來
    [self.view addSubview:_webView];

    //1.網絡
    _webView.allowsBackForwardNavigationGestures = YES;
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.hao123.com/"]];

    [_webView loadRequest:request];
}

-(void)screenResize{
    
    NSLog(@"觀察窗口拉伸");
    NSLog(@"%.2f===%.2f",self.view.bounds.size.width,self.view.bounds.size.height);

    _webView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}


@end

注意⚠️如果發現界面是黑屏或者是白屏那就是有兩處地方你還沒有做配置

此處也可以參考:https://www.jianshu.com/p/d7e96597bd1f

1.是網絡權限的配置

參考我的上一篇:https://www.cnblogs.com/gaozhang12345/p/15979220.html

2.是沙盒模式下的網絡權限配置

參考這張截圖:

 

最近在做項目的時候遇到一個奇怪的問題,WKWebView加載一些頁面的時候會出現頁面內無法跳轉鏈接,但是有些網頁是可以的,最后查閱了很多資料才找到最終解決辦法。

此處感謝 

2.可進行簡單的交互增加代碼如下

(1)引進代理

@interface ViewController ()<WKNavigationDelegate>

(2)增加代理引用的地方

//1.網絡
    self.webView.navigationDelegate = self;

(3)實現下面的代理方法

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

    if (navigationAction.targetFrame == nil) {

        [webView loadRequest:navigationAction.request];

    }

    decisionHandler(WKNavigationActionPolicyAllow);

}

運行之后,代碼就可以實現簡單的交互了。

3.增加前進后退頁面

待續

4.遇到需要上傳圖片的網頁,比如說

https://www.gaitubao.com/tupian-wenzi

那么我們就要調用MAC的文件選擇管理器的代理了
在WKWebView的UIDelegate事件中,調起文件選擇對話框,並將選取的文件回調回去。

self.webView.UIDelegate = self;
- (void)webView:(WKWebView*)webView runOpenPanelWithParameters:(WKOpenPanelParameters*)parameters initiatedByFrame:(WKFrameInfo*)frame completionHandler:(nonnull void (^)(NSArray<NSURL *> * _Nullable))completionHandler {
    NSOpenPanel* panel = [NSOpenPanel openPanel];
    panel.allowsMultipleSelection = parameters.allowsMultipleSelection;
    panel.canChooseDirectories = parameters.allowsDirectories;
    panel.allowedFileTypes = @[@"png", @"jpg", @"BMP", @"TIFF"];//圖片類型
    NSModalResponse result = [panel runModal];
    if (result == NSModalResponseOK) {
        completionHandler([panel URLs]);
    }
    else {
        completionHandler(nil);
    }
}

鳴謝:

就可以拉起文件或者圖片選擇彈框了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM