ios APP uses WKWebView to open local h5 files (cross-domain, Undefined symbols for architecture x86_64 “_OBJC_CLASS_$_WKWebView“..)

The webpage developed by H5 wants to be made into an ios APP, so WKWebView opens the html page in the local folder.

In the following example, there are addresses with parameters and without parameters.

Encounter problems:

1. The less file used in the h5 project adopts the method of sending requests, including the sending of ajax requests, and there are cross-domain problems:

[self.webView.configuration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];

2、报错:Undefined symbols for architecture x86_64 "_OBJC_CLASS_$_WKWebView",referenced from...

Click Project - General - Frameworks, Libraries, and Embedded Content Click +

Select WebKit.framework

#import "ViewController.h"

#define SCREENWIDTH  [UIScreen mainScreen].bounds.size.width
#define SCREENHEIGHT  [UIScreen mainScreen].bounds.size.height

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSString *bundlePath = [[NSBundle mainBundle]bundlePath];
    NSLog(@"bundlePath== : %@", bundlePath);//app打包后的路径 ,文件在app 内部
    //    /private/var/containers/Bundle/Application/645AC02B-F69F-4C87-B6AE-F404268A207D/webViewDemo.app

    
    //load login.html
    self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
    //    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    //    [config setValue:@(true) forKey:@"allowUniversalAccessFromFileURLs"];
    
    [self.webView.configuration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
    //    self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.webView];
    
    
    NSString *basePath = [NSString stringWithFormat:@"%@/www",bundlePath];
    NSURL *baseUrl = [NSURL fileURLWithPath:basePath isDirectory:YES];
    
    
    //    加载html地址带参数 方法一 loadFileURL
    // 示例 portStr 固定8000 
    NSString *portStr = @"8000";//@(portstr.c_str()); 多个参数也可以直接拼在一起
    NSString *filePath = [NSString stringWithFormat:@"file://%@/login.html?httpPort=%@", basePath, portStr];
    NSURL *fileUrl = [NSURL URLWithString:filePath];
    NSLog(@"fileUrl== : %@", fileUrl);
    //file:///private/var/containers/Bundle/Application/645AC02B-F69F-4C87-B6AE-F404268A207D/webViewDemo.app/www/login.html?httpPort=8000
    [self.webView loadFileURL:fileUrl allowingReadAccessToURL:baseUrl];
    
    //    加载html地址带参数 方法二 loadRequest
    //    NSString *portStr = @(portstr.c_str());
    //    NSString *basePath2 = [NSString stringWithFormat:@"%@/www/login.html",bundlePath];
    //    NSString *urlString2 = [NSString stringWithFormat:@"?%@",portStr];
    //    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString2 relativeToURL:[NSURL fileURLWithPath:basePath2]]]];//带参数
    
    
    //    // 加载html地址不带参数 loadHTMLString
    //    NSString *htmlUrl = [NSString stringWithFormat:@"%@/login.html",basePath];
    //    NSLog(@"dayinzifuchuanmainbasePath: %@", htmlUrl);
    //    NSString *indexContent = [NSString stringWithContentsOfFile:htmlUrl encoding:NSUTF8StringEncoding error:nil];
    //    [self.webView loadHTMLString:indexContent baseURL:baseUrl];
    
    
    
    //    // 实例化 WKWebView 对象
    //    self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    //
    //    //创建请求
    //    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
    //    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //    //加载网页
    //    [self.webView loadRequest:request];
    //    [self.view addSubview:self.webView];
    
    
        ------UIWebView
    //    CGFloat width = self.view.frame.size.width;
    //    CGFloat height = self.view.frame.size.height - 20;
    //    UIWebView *_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, width, height)];
    //
    //    NSString *urlStr = @"http://www.baidu.com";
    //    NSURL *url = [NSURL URLWithString:urlStr];
    //    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //    [_webView loadRequest:request];
    
}
#import <UIKit/UIKit.h>

#import <WebKit/WebKit.h>

@interface ViewController : UIViewController
@property (strong, nonatomic) WKWebView *webView;
//@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

Just using mac is not used to:

ctrl + space switch input method

command + c to copy command + v to paste

Guess you like

Origin blog.csdn.net/qq_40015157/article/details/127365917