[iOS] Fixes state restoration not work (flutter/engine#31511)

This commit is contained in:
Wu Zhong
2022-03-18 12:55:07 +08:00
committed by GitHub
parent 2117f15ec8
commit 6b27162f6a
2 changed files with 37 additions and 2 deletions

View File

@@ -9,6 +9,8 @@
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h"
FLUTTER_ASSERT_ARC
@@ -36,6 +38,32 @@ FLUTTER_ASSERT_ARC
#pragma mark - Tests
- (void)testRestoratonViewControllerEncodeAndDecode {
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test"
project:nil
allowHeadlessExecution:YES
restorationEnabled:YES];
[engine run];
FlutterViewController* flutterViewController =
[[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
FlutterRestorationPlugin* restorationPlugin = flutterViewController.restorationPlugin;
NSData* data = [@"testrestortiondata" dataUsingEncoding:NSUTF8StringEncoding];
[restorationPlugin setRestorationData:data];
NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initRequiringSecureCoding:YES];
[flutterViewController encodeRestorableStateWithCoder:archiver];
[restorationPlugin setRestorationData:nil];
NSKeyedUnarchiver* unarchiver =
[[NSKeyedUnarchiver alloc] initForReadingWithData:archiver.encodedData];
[flutterViewController decodeRestorableStateWithCoder:unarchiver];
XCTAssert([[restorationPlugin restorationData] isEqualToData:data],
"Restoration state data must be equal");
}
- (void)testRestorationEnabledWaitsForData {
FlutterRestorationPlugin* restorationPlugin =
[[FlutterRestorationPlugin alloc] initWithChannel:restorationChannel restorationEnabled:YES];

View File

@@ -32,6 +32,8 @@
static constexpr int kMicrosecondsPerSecond = 1000 * 1000;
static constexpr CGFloat kScrollViewContentSize = 2.0;
static NSString* const kFlutterRestorationStateAppData = @"FlutterRestorationStateAppData";
NSNotificationName const FlutterSemanticsUpdateNotification = @"FlutterSemanticsUpdate";
NSNotificationName const FlutterViewControllerWillDealloc = @"FlutterViewControllerWillDealloc";
NSNotificationName const FlutterViewControllerHideHomeIndicator =
@@ -1811,12 +1813,17 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch)
- (void)encodeRestorableStateWithCoder:(NSCoder*)coder {
NSData* restorationData = [[_engine.get() restorationPlugin] restorationData];
[coder encodeDataObject:restorationData];
[coder encodeBytes:(const unsigned char*)restorationData.bytes
length:restorationData.length
forKey:kFlutterRestorationStateAppData];
[super encodeRestorableStateWithCoder:coder];
}
- (void)decodeRestorableStateWithCoder:(NSCoder*)coder {
NSData* restorationData = [coder decodeDataObject];
NSUInteger restorationDataLength;
const unsigned char* restorationBytes = [coder decodeBytesForKey:kFlutterRestorationStateAppData
returnedLength:&restorationDataLength];
NSData* restorationData = [NSData dataWithBytes:restorationBytes length:restorationDataLength];
[[_engine.get() restorationPlugin] setRestorationData:restorationData];
}