iOS Question wkwebview and CORS

watesoft

Active Member
Licensed User
Longtime User
Objective-C:
  WKUserContentController *userContentController = [[WKUserContentController alloc] init];
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    config.userContentController = userContentController;
    config.preferences.javaScriptEnabled = YES;
    config.preferences.javaScriptCanOpenWindowsAutomatically = YES;
    config.suppressesIncrementalRendering = YES;
   [config.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
    if (@available(iOS 10.0, *)) {
         [config setValue:@YES forKey:@"allowUniversalAccessFromFileURLs"];
    }

I encountered CORS problem when using WKwebview, I found the above solution, how can I use it in B4i?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Untested code:
B4X:
Sub SomeSub
 WebView1.InitializeWithCustomConfiguration("WebView1", Me.As(NativeObject).RunMethod("createConfig", Null))
End Sub

#if OBJC
- (NSObject*)createConfig {
  WKUserContentController *userContentController = [[WKUserContentController alloc] init];
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    config.userContentController = userContentController;
    config.preferences.javaScriptEnabled = YES;
    config.preferences.javaScriptCanOpenWindowsAutomatically = YES;
    config.suppressesIncrementalRendering = YES;
   [config.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
    if (@available(iOS 10.0, *)) {
         [config setValue:@YES forKey:@"allowUniversalAccessFromFileURLs"];
    }
    return config;
}
#End If
 
Last edited:
Upvote 0

watesoft

Active Member
Licensed User
Longtime User
Untested code:
B4X:
Sub SomeSub
 WebView1.InitializeWithCustomConfiguration("WebView1", Me.As(NativeObject).RunMethod("createConfig", Null))
End Sub

#if OBJC
- (NSObject)createConfig {
  WKUserContentController *userContentController = [[WKUserContentController alloc] init];
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    config.userContentController = userContentController;
    config.preferences.javaScriptEnabled = YES;
    config.preferences.javaScriptCanOpenWindowsAutomatically = YES;
    config.suppressesIncrementalRendering = YES;
   [config.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"];
    if (@available(iOS 10.0, *)) {
         [config setValue:@YES forKey:@"allowUniversalAccessFromFileURLs"];
    }
    return config;
}
#End If

Thanks for the solution,I get the error 'interface type 'NSObject' cannot be returned by value; did you forget * in 'NSObject'?' There will be errors whether using WKWebview or webview.
 

Attachments

  • full error.txt
    19.9 KB · Views: 76
Upvote 0

watesoft

Active Member
Licensed User
Longtime User
First line should be: - (NSObject*)createConfig {
Thank you. Now it works.
CORS problem occurs when I try to use the local httpserver. It seems that there is a problem that is difficult to solve when I use the ihttpserverhttps://www.b4x.com/android/forum/threads/b4x-xhttpserver-http-server-jquery.129190/. My requirement is very simple, that is, use xhr to request some local files in MP3 format, so I need to start the local http server.
I want to try to wrap a simple httpserver. I found a LocalHTTPServerManager.h file and LocalHTTPServerManager.m file, I wonder if it is feasible?


LocalHTTPServerManager.h:
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface LocalHTTPServerManager : NSObject

@property(nonatomic, copy)NSString *desPath;

@property (nonatomic,copy) NSString *serverPort;

+ (instancetype)sharedInstance;

- (BOOL)startServer;

- (void)stopServer;

@end

NS_ASSUME_NONNULL_END

LocalHTTPServerManager.m:
#import "LocalHTTPServerManager.h"
#import "HTTPServer.h"

@interface LocalHTTPServerManager()

@property(nonatomic,strong) HTTPServer *httpServer;

@end

@implementation LocalHTTPServerManager

+ (instancetype)sharedInstance {
  static LocalHTTPServerManager *instance = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    instance = [[LocalHTTPServerManager alloc] init];
  });
  return instance;
}

- (BOOL)startServer {
  if (!_httpServer) {
    _httpServer = [[HTTPServer alloc] init];
    [_httpServer setType: @"_http._tcp."];
    [_httpServer setPort: 8080];
    [_httpServer setDocumentRoot:_desPath];
  }
 
  if (_httpServer && ![_httpServer isRunning]) {
    NSError *error;
    if ([_httpServer start: &error]) {
      self.serverPort = [NSString stringWithFormat:@"%d",[_httpServer listeningPort]];
      NSLog(@"LocalServer succeeded");
      return YES;
    } else {
      NSLog(@"LocalServer failed:%@",[error localizedDescription]);
      return NO;
    }
  }
  return NO;
}

- (void)stopServer {
  if (_httpServer && [_httpServer isRunning]) {
    [_httpServer stop];
    NSLog(@"LocalServer stop");
  }
}

-(BOOL)serverIsRunning {
  return _httpServer && [_httpServer isRunning];
}

@end
 

Attachments

  • CCLHTTPServer.zip
    7.2 KB · Views: 71
Last edited:
Upvote 0
Top