5 #define FML_USED_ON_EMBEDDER
12 - (instancetype)initWithEnableVMServicePublication:(BOOL)enableVMServicePublication {
17 #else // FLUTTER_RELEASE
19 #import <TargetConditionals.h>
42 #include "flutter/fml/logging.h"
43 #include "flutter/fml/message_loop.h"
44 #include "flutter/fml/platform/darwin/scoped_nsobject.h"
45 #include "flutter/runtime/dart_service_isolate.h"
51 - (void)publishServiceProtocolPort:(NSURL*)uri;
56 + (NSData*)createTxtData:(NSURL*)url;
58 @property(readonly,
class) NSString* serviceName;
59 @property(readonly) NSObject<FlutterDartVMServicePublisherDelegate>* delegate;
60 @property(nonatomic, readwrite) NSURL*
url;
61 @property(readonly) BOOL enableVMServicePublication;
69 DNSServiceRef _dnsServiceRef;
74 DNSServiceRefDeallocate(_dnsServiceRef);
75 _dnsServiceRef = NULL;
79 - (void)publishServiceProtocolPort:(NSURL*)url {
81 FML_LOG(INFO) <<
"Publish Service Protocol Port";
82 DNSServiceFlags flags = kDNSServiceFlagsDefault;
83 #if TARGET_IPHONE_SIMULATOR
85 uint32_t interfaceIndex = if_nametoindex(
"lo0");
86 #else // TARGET_IPHONE_SIMULATOR
88 uint32_t interfaceIndex = 0;
89 #endif // TARGET_IPHONE_SIMULATOR
90 const char* registrationType =
"_dartVmService._tcp";
92 const char* domain =
"local.";
93 uint16_t port = [[url port] unsignedShortValue];
96 int err = DNSServiceRegister(&_dnsServiceRef, flags, interfaceIndex,
98 registrationType, domain, NULL, htons(port), txtData.length,
99 txtData.bytes, RegistrationCallback, NULL);
102 DNSServiceSetDispatchQueue(_dnsServiceRef, dispatch_get_main_queue());
106 FML_LOG(ERROR) <<
"Failed to register Dart VM Service port with mDNS with error " << err <<
".";
107 if (@available(iOS 14.0, *)) {
108 FML_LOG(ERROR) <<
"On iOS 14+, local network broadcast in apps need to be declared in "
109 <<
"the app's Info.plist. Debug and profile Flutter apps and modules host "
110 <<
"VM services on the local network to support debugging features such "
111 <<
"as hot reload and DevTools. To make your Flutter app or module "
112 <<
"attachable and debuggable, add a '" << registrationType <<
"' value "
113 <<
"to the 'NSBonjourServices' key in your Info.plist for the Debug/"
114 <<
"Profile configurations. " <<
"For more information, see "
115 <<
"https://docs.flutter.cn/development/add-to-app/ios/"
116 "project-setup#local-network-privacy-permissions";
120 static void DNSSD_API RegistrationCallback(DNSServiceRef sdRef,
121 DNSServiceFlags flags,
122 DNSServiceErrorType errorCode,
127 if (errorCode == kDNSServiceErr_NoError) {
128 FML_DLOG(INFO) <<
"FlutterDartVMServicePublisher is ready!";
129 }
else if (errorCode == kDNSServiceErr_PolicyDenied) {
131 <<
"Could not register as server for FlutterDartVMServicePublisher, permission "
132 <<
"denied. Check your 'Local Network' permissions for this app in the Privacy section of "
133 <<
"the system Settings.";
135 FML_LOG(ERROR) <<
"Could not register as server for FlutterDartVMServicePublisher. Check your "
136 "network settings and relaunch the application.";
143 flutter::DartServiceIsolate::CallbackHandle _callbackHandle;
146 - (instancetype)initWithEnableVMServicePublication:(BOOL)enableVMServicePublication {
148 NSAssert(
self,
@"Super must not return null on init.");
151 _enableVMServicePublication = enableVMServicePublication;
152 __weak __typeof(
self) weakSelf =
self;
154 fml::MessageLoop::EnsureInitializedForCurrentThread();
156 _callbackHandle = flutter::DartServiceIsolate::AddServerStatusCallback(
157 [weakSelf, runner = fml::MessageLoop::GetCurrent().GetTaskRunner()](
const std::string& uri) {
159 runner->PostTask([weakSelf, uri]() {
160 FlutterDartVMServicePublisher* strongSelf = weakSelf;
165 [[NSURL alloc] initWithString:[NSString stringWithUTF8String:uri.c_str()]];
166 strongSelf.url = url;
167 if (strongSelf.enableVMServicePublication) {
168 [[strongSelf delegate] publishServiceProtocolPort:url];
178 + (NSString*)serviceName {
179 return NSBundle.mainBundle.bundleIdentifier;
182 + (NSData*)createTxtData:(NSURL*)url {
185 NSString* path = [[url path] substringFromIndex:MIN(1, [[url path] length])];
186 NSData* pathData = [path dataUsingEncoding:NSUTF8StringEncoding];
187 NSDictionary<NSString*, NSData*>* txtDict = @{
188 @"authCode" : pathData,
190 return [NSNetService dataFromTXTRecordDictionary:txtDict];
194 [_delegate stopService];
196 flutter::DartServiceIsolate::RemoveServerStatusCallback(_callbackHandle);
200 #endif // FLUTTER_RELEASE