0
我有陣營本土原生module,打開Safari瀏覽器視圖控制器:如何打「集合被列舉時發生了變異」錯誤?
RCTSFSafariViewController.m:
#import "RCTSFSafariViewController.h"
@implementation RCTSFSafariViewController
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE();
- (void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
[self.bridge.eventDispatcher sendAppEventWithName:@"SFSafariViewControllerDismissed" body:nil];
}
RCT_EXPORT_METHOD(openURL:(NSString *)urlString params:(NSDictionary *)params) {
NSURL *url = [[NSURL alloc] initWithString:urlString];
SFSafariViewController *safariViewController = [[SFSafariViewController alloc] initWithURL:url];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:safariViewController];
[navigationController setNavigationBarHidden:YES animated:NO];
safariViewController.delegate = self;
if ([params objectForKey:@"tintColor"]) {
UIColor *tintColor = [RCTConvert UIColor:params[@"tintColor"]];
if([safariViewController respondsToSelector:@selector(setPreferredControlTintColor:)]) {
safariViewController.preferredControlTintColor = tintColor;
} else {
safariViewController.view.tintColor = tintColor;
}
}
dispatch_sync(dispatch_get_main_queue(), ^{
[rootViewController.rootViewController.presentedViewController presentViewController:navigationController animated:YES completion:^{
[self.bridge.eventDispatcher sendDeviceEventWithName:@"SFSafariViewControllerDidLoad" body:nil];
}];
});
}
RCT_EXPORT_METHOD(close) {
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *rootViewController = [[[UIApplication sharedApplication] delegate] window].rootViewController;
[rootViewController dismissViewControllerAnimated:YES completion:nil];
});
}
@end
RCTSFSafariViewController.h:
#import <React/RCTBridgeModule.h>
#import <React/RCTConvert.h>
#import <React/RCTEventDispatcher.h>
#import <UIKit/UIKit.h>
@import SafariServices;
@interface RCTSFSafariViewController : NSObject <RCTBridgeModule, SFSafariViewControllerDelegate>
@end
它運作良好,在模擬器和我的iPhone ,但很多用戶都面臨這樣的崩潰(根據Crashlytics):
Collection <__NSArrayM: 0x14e3bd20> was mutated while being enumerated.' was thrown while invoking openURL on target SFSafariViewController with params ("https://example.com", { })
問題是在此代碼中沒有數組或枚舉結構。我有這個想法,這可能是由dispatch_async引起的,因爲當我刪除它時,應用程序停止崩潰,但在調用SVC後工作速度慢得多。
我在做什麼錯?
我使用dispatch_async和dispatch_sync是我絕望的實驗。但最後一段似乎是可能的解決方案。我會試試看。無論如何,非常感謝您的回答。 – devtwo
不知何故,當調用SFSafariViewController時,將整個方法封裝到'dispatch_async'會導致死鎖:( – devtwo
包裝整個方法並刪除'dispatch_sync',或者離開'dispatch_sync'?如果不刪除'dispatch_sync'調用,完全死鎖(這將是死鎖的典型例子) –