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/runtime/dart_service_isolate.h"
50 - (void)publishServiceProtocolPort:(NSURL*)uri;
55 + (NSData*)createTxtData:(NSURL*)url;
57 @property(readonly,
class) NSString* serviceName;
58 @property(readonly) NSObject<FlutterDartVMServicePublisherDelegate>* delegate;
59 @property(nonatomic, readwrite) NSURL*
url;
60 @property(readonly) BOOL enableVMServicePublication;
68 DNSServiceRef _dnsServiceRef;
73 DNSServiceRefDeallocate(_dnsServiceRef);
74 _dnsServiceRef = NULL;
78 - (void)publishServiceProtocolPort:(NSURL*)url {
80 FML_LOG(INFO) <<
"Publish Service Protocol Port";
81 DNSServiceFlags flags = kDNSServiceFlagsDefault;
82 #if TARGET_IPHONE_SIMULATOR
84 uint32_t interfaceIndex = if_nametoindex(
"lo0");
85 #else // TARGET_IPHONE_SIMULATOR
87 uint32_t interfaceIndex = 0;
88 #endif // TARGET_IPHONE_SIMULATOR
89 const char* registrationType =
"_dartVmService._tcp";
91 const char* domain =
"local.";
92 uint16_t port = [[url port] unsignedShortValue];
95 int err = DNSServiceRegister(&_dnsServiceRef, flags, interfaceIndex,
97 registrationType, domain, NULL, htons(port), txtData.length,
98 txtData.bytes, RegistrationCallback, NULL);
101 DNSServiceSetDispatchQueue(_dnsServiceRef, dispatch_get_main_queue());
105 FML_LOG(ERROR) <<
"Failed to register Dart VM Service port with mDNS with error " << err <<
".";
106 if (@available(iOS 14.0, *)) {
107 FML_LOG(ERROR) <<
"On iOS 14+, local network broadcast in apps need to be declared in "
108 <<
"the app's Info.plist. Debug and profile Flutter apps and modules host "
109 <<
"VM services on the local network to support debugging features such "
110 <<
"as hot reload and DevTools. To make your Flutter app or module "
111 <<
"attachable and debuggable, add a '" << registrationType <<
"' value "
112 <<
"to the 'NSBonjourServices' key in your Info.plist for the Debug/"
113 <<
"Profile configurations. " <<
"For more information, see "
114 <<
"https://flutter.dev/docs/development/add-to-app/ios/"
115 "project-setup#local-network-privacy-permissions";
119 static void DNSSD_API RegistrationCallback(DNSServiceRef sdRef,
120 DNSServiceFlags flags,
121 DNSServiceErrorType errorCode,
126 if (errorCode == kDNSServiceErr_NoError) {
127 FML_DLOG(INFO) <<
"FlutterDartVMServicePublisher is ready!";
128 }
else if (errorCode == kDNSServiceErr_PolicyDenied) {
130 <<
"Could not register as server for FlutterDartVMServicePublisher, permission "
131 <<
"denied. Check your 'Local Network' permissions for this app in the Privacy section of "
132 <<
"the system Settings.";
134 FML_LOG(ERROR) <<
"Could not register as server for FlutterDartVMServicePublisher. Check your "
135 "network settings and relaunch the application.";
142 flutter::DartServiceIsolate::CallbackHandle _callbackHandle;
145 - (instancetype)initWithEnableVMServicePublication:(BOOL)enableVMServicePublication {
147 NSAssert(
self,
@"Super must not return null on init.");
150 _enableVMServicePublication = enableVMServicePublication;
151 __weak __typeof(
self) weakSelf =
self;
153 fml::MessageLoop::EnsureInitializedForCurrentThread();
155 _callbackHandle = flutter::DartServiceIsolate::AddServerStatusCallback(
156 [weakSelf, runner = fml::MessageLoop::GetCurrent().GetTaskRunner()](
const std::string& uri) {
158 runner->PostTask([weakSelf, uri]() {
159 FlutterDartVMServicePublisher* strongSelf = weakSelf;
164 [[NSURL alloc] initWithString:[NSString stringWithUTF8String:uri.c_str()]];
165 strongSelf.url = url;
166 if (strongSelf.enableVMServicePublication) {
167 [[strongSelf delegate] publishServiceProtocolPort:url];
177 + (NSString*)serviceName {
178 return NSBundle.mainBundle.bundleIdentifier;
181 + (NSData*)createTxtData:(NSURL*)url {
184 NSString* path = [[url path] substringFromIndex:MIN(1, [[url path] length])];
185 NSData* pathData = [path dataUsingEncoding:NSUTF8StringEncoding];
186 NSDictionary<NSString*, NSData*>* txtDict = @{
187 @"authCode" : pathData,
189 return [NSNetService dataFromTXTRecordDictionary:txtDict];
193 [_delegate stopService];
195 flutter::DartServiceIsolate::RemoveServerStatusCallback(_callbackHandle);
199 #endif // FLUTTER_RELEASE