private ProtoPadServer(UIApplicationDelegate appDelegate, UIWindow window, int? overrideListeningPort = null, string overrideBroadcastedAppName = null) { _appDelegate = appDelegate; _window = window; BroadcastedAppName = overrideBroadcastedAppName ?? String.Format("ProtoPad Service on iOS Device {0}", UIDevice.CurrentDevice.Name); ListeningPort = overrideListeningPort ?? 8080; LocalIPAddress = Helpers.GetCurrentIPAddress(); var mainMonotouchAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name.ToLower() == "monotouch"); var requestHandlers = new Dictionary<string, Func<byte[], string>> { {"GetMainXamarinAssembly", requestData => mainMonotouchAssembly.FullName}, {"WhoAreYou", requestData => "iOS"}, {"GetPixateCssFiles", requestData => JsonEncode(_pixateCssPaths.ToArray())}, {"ExecuteAssembly", requestData => { var response = "{}"; var remoteCommandDoneEvent = new AutoResetEvent(false); _appDelegate.InvokeOnMainThread(() => ExecuteAssemblyAndCreateResponse(requestData, remoteCommandDoneEvent, ref response)); remoteCommandDoneEvent.WaitOne(); return response; } }, {"UpdatePixateCSS", requestData => { var response = "{}"; var remoteCommandDoneEvent = new AutoResetEvent(false); var filePathDataLength = requestData[1] + (requestData[0] << 8); var filePathData = new byte[filePathDataLength]; Array.Copy(requestData, 2, filePathData, 0, filePathDataLength); var filePath = Encoding.UTF8.GetString(filePathData); var cssFileDataLength = requestData.Length - (2 + filePathDataLength); var cssFileData = new byte[cssFileDataLength]; Array.Copy(requestData, 2 + filePathDataLength, cssFileData, 0, cssFileDataLength); _appDelegate.InvokeOnMainThread(() => UpdatePixateCssFile(filePath, cssFileData, remoteCommandDoneEvent, ref response)); remoteCommandDoneEvent.WaitOne(); return response; } }, {"GetFileContents", requestData => { var response = ""; var filePath = Encoding.UTF8.GetString(requestData); var remoteCommandDoneEvent = new AutoResetEvent(false); _appDelegate.InvokeOnMainThread(() => GetFileContents(filePath, remoteCommandDoneEvent, ref response)); remoteCommandDoneEvent.WaitOne(); return response; } } }; _httpServer = new SimpleHttpServer(ListeningPort, requestHandlers); _udpServer = new UdpDiscoveryServer(BroadcastedAppName, String.Format("http://{0}:{1}/", LocalIPAddress, ListeningPort)); }
private ProtoPadServer(UIApplicationDelegate appDelegate, UIWindow window, int?overrideListeningPort = null, string overrideBroadcastedAppName = null) { _appDelegate = appDelegate; _window = window; BroadcastedAppName = overrideBroadcastedAppName ?? String.Format("ProtoPad Service on iOS Device {0}", UIDevice.CurrentDevice.Name); ListeningPort = overrideListeningPort ?? 8080; LocalIPAddress = Helpers.GetCurrentIPAddress(); var mainMonotouchAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name.ToLower() == "monotouch"); var requestHandlers = new Dictionary <string, Func <byte[], string> > { { "GetMainXamarinAssembly", requestData => mainMonotouchAssembly.FullName }, { "WhoAreYou", requestData => "iOS" }, { "GetPixateCssFiles", requestData => JsonEncode(_pixateCssPaths.ToArray()) }, { "ExecuteAssembly", requestData => { var response = "{}"; var remoteCommandDoneEvent = new AutoResetEvent(false); _appDelegate.InvokeOnMainThread(() => ExecuteAssemblyAndCreateResponse(requestData, remoteCommandDoneEvent, ref response)); remoteCommandDoneEvent.WaitOne(); return(response); } }, { "UpdatePixateCSS", requestData => { var response = "{}"; var remoteCommandDoneEvent = new AutoResetEvent(false); var filePathDataLength = requestData[1] + (requestData[0] << 8); var filePathData = new byte[filePathDataLength]; Array.Copy(requestData, 2, filePathData, 0, filePathDataLength); var filePath = Encoding.UTF8.GetString(filePathData); var cssFileDataLength = requestData.Length - (2 + filePathDataLength); var cssFileData = new byte[cssFileDataLength]; Array.Copy(requestData, 2 + filePathDataLength, cssFileData, 0, cssFileDataLength); _appDelegate.InvokeOnMainThread(() => UpdatePixateCssFile(filePath, cssFileData, remoteCommandDoneEvent, ref response)); remoteCommandDoneEvent.WaitOne(); return(response); } }, { "GetFileContents", requestData => { var response = ""; var filePath = Encoding.UTF8.GetString(requestData); var remoteCommandDoneEvent = new AutoResetEvent(false); _appDelegate.InvokeOnMainThread(() => GetFileContents(filePath, remoteCommandDoneEvent, ref response)); remoteCommandDoneEvent.WaitOne(); return(response); } } }; _httpServer = new SimpleHttpServer(ListeningPort, requestHandlers); _udpServer = new UdpDiscoveryServer(BroadcastedAppName, String.Format("http://{0}:{1}/", LocalIPAddress, ListeningPort)); }
public static void Invoke(NSAction action) { if (_app != null) { _app.InvokeOnMainThread(action); } }
/// <summary> /// Runs a method on the application's UI thread /// </summary> /// <param name="a"></param> public void RunOnUIThread(Action a) { #if ANDROID activity.RunOnUiThread(a); #elif IOS del.InvokeOnMainThread(a); #endif }
protected override void OnControllerLoadComplete(IMXView fromView, IMXController controller, MXViewPerspective viewPerspective) { Console.WriteLine("Controller Load End"); _appDelegate.InvokeOnMainThread(delegate { LoadViewForController(fromView, controller, viewPerspective); if (ControllerLoadComplete != null) { ControllerLoadComplete(controller); } }); }
public async Task InitializeAsync(UIApplicationDelegate appDelegate, NSDictionary options) { using (var _ = _analyticsService.StartTrace(this, "iOs Push Notifications Initialization")) { if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) { UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound, (granted, error) => { if (granted) { _analyticsService.TraceInformation(this, "Permission granted for push notifications"); appDelegate.InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications); } else { if (error?.Code != null) { _analyticsService.TraceError(this, "Failed to get permission for push notifications", "ErrorCode", error.Code.ToString()); } else { _analyticsService.TraceWarning(this, "Permission for push notifications not granted"); } } }); } else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) { var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, new NSSet()); UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings); UIApplication.SharedApplication.RegisterForRemoteNotifications(); } else { const UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound; UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes); } if (options != null) { if (options.TryGetValue(new NSString("UIApplicationLaunchOptionsRemoteNotificationKey"), out var tapped)) { _analyticsService.TraceVerbose(this, "Notification payload available for new app session"); await HandleMessageReceivedAsync(UIApplicationState.Inactive, (NSDictionary)tapped); } } } }