gefunden die Lösung über WKWebView
. Ich habe es auf die gleiche Weise wie UIWebView
in meinem Projekt gearbeitet.
Und dann benutze ich eigentlich zwei Methoden aus WKNavigationDelegate
Protokoll:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
und
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler
Beispiel Code:
erste Methode ist, aktuelle Anforderung zu prüfen, ob tatsächlich ist unsere redirect_uri
dass wir damit umgehen sollten:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSLog(@"request: %@", navigationAction.request);
// check whether this request should be stopped - if this is redirect_uri (like user is already authorized)
if (...) {
self.webView.navigationDelegate = nil;
// do what is needed to send authorization data back
self.completionBlock(...);
// close current view controller
[self dismissViewControllerAnimated:YES completion:nil];
// stop executing current request
decisionHandler(WKNavigationActionPolicyCancel);
} else {
// otherwise allow current request
decisionHandler(WKNavigationActionPolicyAllow);
}
}
zweite Methode ist Antwort vom Server zu analysieren, und hier können wir den Statuscode, um alle Fehler
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
NSHTTPURLResponse *response = (NSHTTPURLResponse *)navigationResponse.response;
// filter the responses, in order to verify only the response from specific domain
if ([[NSString stringWithFormat:[[response URL] absoluteString]] containsString:@"some.domain.com/"]) {
NSInteger statusCode = [response statusCode];
// check what is the status code
if (statusCode == 200 ||
statusCode == 301 ||
statusCode == 302) {
// allow response as everything is ok
decisionHandler(WKNavigationResponsePolicyAllow);
} else {
// handle the error (e.g. any of 4xx or any others)
self.webView.navigationDelegate = nil;
// send needed info back
self.completionBlock(...);
// close current view controller
[self dismissViewControllerAnimated:YES completion:nil];
// stop current response
decisionHandler(WKNavigationResponsePolicyCancel);
}
} else {
// current response is not from our domain, so allow it
decisionHandler(WKNavigationResponsePolicyAllow);
}
}
Sind Sie sicher, dass diese Methode aufgerufen wird, wenn der Antwortstatuscode 301 oder 302 ist? Ich versuche das und erhalte nie diese Codes – FelixSFD
Diese HTTP-Statuscodes bedeuten, dass Anfrage an eine andere URL umgeleitet wurde. Sie erhalten sie in seltenen Fällen (es hängt vom Server selbst ab). In den meisten Fällen erhalten Sie den Statuscode 200 –
für mich war es sehr wichtig, 4xx oder 5xx Fehler zu fangen als Autorisierungsserver kann sehr oft bei meinem Projekt sein)) –