Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a crash when load local HTML resources on IOS when localUrlScop… #906

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions ios/Classes/FlutterWebviewPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,10 @@ - (void)navigate:(FlutterMethodCall*)call {
NSString *url = call.arguments[@"url"];
NSNumber *withLocalUrl = call.arguments[@"withLocalUrl"];
if ( [withLocalUrl boolValue]) {
NSURL *htmlUrl = [NSURL fileURLWithPath:url isDirectory:false];
NSString *localUrlScope = call.arguments[@"localUrlScope"];
if (@available(iOS 9.0, *)) {
if(localUrlScope == nil) {
[self.webview loadFileURL:htmlUrl allowingReadAccessToURL:htmlUrl];
}
else {
NSURL *scopeUrl = [NSURL fileURLWithPath:localUrlScope];
[self.webview loadFileURL:htmlUrl allowingReadAccessToURL:scopeUrl];
}
NSURL *scopeUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSURL *fileUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:url ofType:nil]];
[self.webview loadFileURL:fileUrl allowingReadAccessToURL:scopeUrl];
} else {
@throw @"not available on version earlier than ios 9.0";
}
Expand Down
20 changes: 12 additions & 8 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const _kChannel = 'flutter_webview_plugin';
// TODO: more general state for iOS/android
enum WebViewState { shouldStart, startLoad, finishLoad, abortLoad }

/// the transition animation type of page on/off screen
enum TransitionType {
Non,
Slide
}


// TODO: use an id by webview to be able to manage multiple webview

/// Singleton class that communicate with a Webview Instance
Expand Down Expand Up @@ -44,7 +51,7 @@ class FlutterWebviewPlugin {
final _onPostMessage = StreamController<JavascriptMessage>.broadcast();

final Map<String, JavascriptChannel> _javascriptChannels =
<String, JavascriptChannel>{};
<String, JavascriptChannel>{};

Future<Null> _handleMessages(MethodCall call) async {
switch (call.method) {
Expand Down Expand Up @@ -139,8 +146,7 @@ class FlutterWebviewPlugin {
/// - [withOverviewMode]: enable overview mode for Android webview ( setLoadWithOverviewMode )
/// - [useWideViewPort]: use wide viewport for Android webview ( setUseWideViewPort )
/// - [ignoreSSLErrors]: use to bypass Android/iOS SSL checks e.g. for self-signed certificates
Future<void> launch(
String url, {
Future<void> launch(String url, {
Map<String, String>? headers,
Set<JavascriptChannel> javascriptChannels = const <JavascriptChannel>{},
bool withJavascript = true,
Expand Down Expand Up @@ -317,21 +323,19 @@ class FlutterWebviewPlugin {

Set<String> _extractJavascriptChannelNames(Set<JavascriptChannel> channels) {
final Set<String> channelNames =
channels.map((JavascriptChannel channel) => channel.name).toSet();
channels.map((JavascriptChannel channel) => channel.name).toSet();
return channelNames;
}

void _handleJavascriptChannelMessage(
final String channelName, final String message) {
void _handleJavascriptChannelMessage(final String channelName, final String message) {
if (_javascriptChannels.containsKey(channelName))
_javascriptChannels[channelName]!
.onMessageReceived(JavascriptMessage(message));
else
print('Channel "$channelName" is not exstis');
}

void _assertJavascriptChannelNamesAreUnique(
final Set<JavascriptChannel>? channels) {
void _assertJavascriptChannelNamesAreUnique(final Set<JavascriptChannel>? channels) {
if (channels == null || channels.isEmpty) {
return;
}
Expand Down
63 changes: 55 additions & 8 deletions lib/src/webview_scaffold.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:ui';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand All @@ -10,6 +11,8 @@ import 'base.dart';
class WebviewScaffold extends StatefulWidget {
const WebviewScaffold({
Key? key,
required this.rrok,
required this.margin,
this.appBar,
required this.url,
this.headers,
Expand Down Expand Up @@ -41,7 +44,22 @@ class WebviewScaffold extends StatefulWidget {
this.geolocationEnabled = false,
this.debuggingEnabled = false,
this.ignoreSSLErrors = false,
}) : super(key: key);
this.transitionType = TransitionType.Non
})
:
assert(transitionType != null, 'TransitionType must not null !'),
super(key: key);

// The global key of the referenced render object
final GlobalKey rrok;

final EdgeInsets margin;

/// in Debug mode,the first init webview page at slide/scale transition,will display misalignment
/// after that will be display properly.
///
/// in Profile/Release mode, will always display properly.
final TransitionType transitionType;

final PreferredSizeWidget? appBar;
final String url;
Expand Down Expand Up @@ -92,6 +110,8 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
super.initState();
webviewReference.close();

perceptionPageTransition();

_onBack = webviewReference.onBack.listen((_) async {
if (!mounted) {
return;
Expand All @@ -113,13 +133,40 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
if (widget.hidden) {
_onStateChanged =
webviewReference.onStateChanged.listen((WebViewStateChanged state) {
if (state.type == WebViewState.finishLoad) {
webviewReference.show();
}
if (state.type == WebViewState.finishLoad) {
webviewReference.show();
}
});
}
}

/// coordinate the webview rect whit page's transition
void perceptionPageTransition() {
if (widget.transitionType != TransitionType.Non) {
WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
//avoid to concurrent modification exception
WidgetsBinding.instance?.addPersistentFrameCallback((timeStamp) {
if (mounted) {
driveWebView();
}
});
});
}
}

void driveWebView() {
switch (widget.transitionType) {
case TransitionType.Slide:
final RenderBox box = widget.rrok.currentContext?.findRenderObject()! as RenderBox;
final Offset offset = box.localToGlobal(Offset.zero) + Offset(widget.margin.left != null ? widget.margin.left : 0.0, widget.margin.top != null ? widget.margin.top : 0.0);
webviewReference.resize(_rect!.shift(offset));
break;
case TransitionType.Non:
// TODO: Handle this case.
break;
}
}

/// Equivalent to [Navigator.of(context)._history.last].
Route<dynamic> get _topMostRoute {
Route? topMost;
Expand Down Expand Up @@ -161,7 +208,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
clearCache: widget.clearCache,
clearCookies: widget.clearCookies,
mediaPlaybackRequiresUserGesture:
widget.mediaPlaybackRequiresUserGesture,
widget.mediaPlaybackRequiresUserGesture,
hidden: widget.hidden,
enableAppScheme: widget.enableAppScheme,
userAgent: widget.userAgent,
Expand Down Expand Up @@ -217,8 +264,7 @@ class _WebviewPlaceholder extends SingleChildRenderObjectWidget {
}

@override
void updateRenderObject(
BuildContext context, _WebviewPlaceholderRender renderObject) {
void updateRenderObject(BuildContext context, _WebviewPlaceholderRender renderObject) {
renderObject..onRectChanged = onRectChanged;
}
}
Expand All @@ -227,7 +273,8 @@ class _WebviewPlaceholderRender extends RenderProxyBox {
_WebviewPlaceholderRender({
RenderBox? child,
ValueChanged<Rect>? onRectChanged,
}) : _callback = onRectChanged,
})
: _callback = onRectChanged,
super(child);

ValueChanged<Rect>? _callback;
Expand Down