У меня есть вопрос о сообщении «.html хотел бы использовать ваше текущее местоположение».
Итак, я следую этому ответу в стеке
Но я могуничего не получаю в userContentController(_:didReceive:)
И я также устанавливаю Privacy - Location When In Use Usage Description
в .plist.
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
WKUserContentController *userContent = [[WKUserContentController alloc] init];
NSString *scriptString = @"navigator.geolocation.getCurrentPosition = function(success, error, options) { ... }; navigator.geolocation.watchPosition = function(success, error, options) { ... }; navigator.geolocation.clearWatch = function(id) { ... };";
WKUserScript *script = [[WKUserScript alloc] initWithSource: scriptString injectionTime: WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES];
[userContent addUserScript:script];
config.userContentController = userContent;
self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration: config];
- (void)userContentController:(WKUserContentController*)userContentController didReceiveScriptMessage:(WKScriptMessage*)message {
NSString *name = message.name;
NSLog(@"===) message name = %@",name);
}
js пример кода:
var onSuccess = function(position) {
alert('Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n' +
'Altitude: ' + position.coords.altitude + '\n' +
'Accuracy: ' + position.coords.accuracy + '\n' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
'Heading: ' + position.coords.heading + '\n' +
'Speed: ' + position.coords.speed + '\n' +
'Timestamp: ' + position.timestamp + '\n');
};
// onError Callback receives a PositionError object
//
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);