我使用一個名爲basicmap.html本地HTML文件,其中包含無法加載谷歌地圖
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="map"></div>
<script>
function initMap() {
// Create a map object and specify the DOM element for display.
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
scrollwheel: false,
zoom: 8
});
console.log(map);
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCvjeJZDdkpxCLasVMvTX2raxKkVGUULP8&callback=initMap"
async defer></script>
</body>
</html>
在viewcontroller.m負荷本地basicmap.html文件到已下面的代碼的WebView
#import "ViewController.h"
@interface ViewController()
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"basicmap" withExtension:@"html"];
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
[self.view addSubview:_webView];
}
@end
輸出顯示webview中的空白頁面。
我還加 NSAppTransportSecurity \t \t \t NSAllowsArbitraryLoads \t \t \t 在Info.plist文件 –
使用這種方法 - (空)加載HTMLString:(NSString *)string baseURL:(NSURL *)baseURL – iOS
@Jigar 我使用這個 NSString * htmlFile = [[NSBundle mainBundle] pathForResource:@「basic」ofType:@「html」inDirectory:nil]; NSString * htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; [_webView loadHTMLString:htmlString baseURL:[[NSBundle mainBundle] bundleURL]]; 但這也不行/ –