forked from flutter/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow specifying a navigation delegate (iOS implementation). (flutter…
…#1323) This is the iOS implementation of the navigation delegate method channel. The Dart and Android implementations are in flutter#1236 Splitting off the iOS to keep a reasonable change size. This PR will be merged first. flutter/flutter#25329
- Loading branch information
Showing
5 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
...ages/webview_flutter/example/ios/Runner.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
8 changes: 8 additions & 0 deletions
8
.../webview_flutter/example/ios/Runner.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>BuildSystemType</key> | ||
<string>Original</string> | ||
</dict> | ||
</plist> |
21 changes: 21 additions & 0 deletions
21
packages/webview_flutter/ios/Classes/FLTWKNavigationDelegate.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2019 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#import <Flutter/Flutter.h> | ||
#import <WebKit/WebKit.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface FLTWKNavigationDelegate : NSObject <WKNavigationDelegate> | ||
|
||
- (instancetype)initWithChannel:(FlutterMethodChannel*)channel; | ||
|
||
/** | ||
* Whether to delegate navigation decisions over the method channel. | ||
*/ | ||
@property(nonatomic, assign) BOOL hasDartNavigationDelegate; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
59 changes: 59 additions & 0 deletions
59
packages/webview_flutter/ios/Classes/FLTWKNavigationDelegate.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2019 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#import "FLTWKNavigationDelegate.h" | ||
|
||
@implementation FLTWKNavigationDelegate { | ||
FlutterMethodChannel* _methodChannel; | ||
} | ||
|
||
- (instancetype)initWithChannel:(FlutterMethodChannel*)channel { | ||
self = [super init]; | ||
if (self) { | ||
_methodChannel = channel; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)webView:(WKWebView*)webView | ||
decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction | ||
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { | ||
if (!self.hasDartNavigationDelegate) { | ||
decisionHandler(WKNavigationActionPolicyAllow); | ||
return; | ||
} | ||
NSDictionary* arguments = @{ | ||
@"url" : navigationAction.request.URL.absoluteString, | ||
@"isForMainFrame" : @(navigationAction.targetFrame.isMainFrame) | ||
}; | ||
[_methodChannel invokeMethod:@"navigationRequest" | ||
arguments:arguments | ||
result:^(id _Nullable result) { | ||
if ([result isKindOfClass:[FlutterError class]]) { | ||
NSLog(@"navigationRequest has unexpectedly completed with an error, " | ||
@"allowing navigation."); | ||
decisionHandler(WKNavigationActionPolicyAllow); | ||
return; | ||
} | ||
if (result == FlutterMethodNotImplemented) { | ||
NSLog(@"navigationRequest was unexepectedly not implemented: %@, " | ||
@"allowing navigation.", | ||
result); | ||
decisionHandler(WKNavigationActionPolicyAllow); | ||
return; | ||
} | ||
if (![result isKindOfClass:[NSNumber class]]) { | ||
NSLog(@"navigationRequest unexpectedly returned a non boolean value: " | ||
@"%@, allowing navigation.", | ||
result); | ||
decisionHandler(WKNavigationActionPolicyAllow); | ||
return; | ||
} | ||
NSNumber* typedResult = result; | ||
decisionHandler([typedResult boolValue] ? WKNavigationActionPolicyAllow | ||
: WKNavigationActionPolicyCancel); | ||
}]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters