using System; using Foundation; using Microsoft.AppCenter; using Microsoft.AppCenter.Analytics; using Microsoft.AppCenter.Crashes; using Shared; using Shared.Common; using Shared.IOS.TBL; using UIKit; namespace Home.IOS { [Register("AppDelegate")] public class AppDelegate : BaseApplicationDelegate { public static bool haveToSignOut = false; // class-level declarations public override UIWindow Window { get; set; } internal static UINavigationController rootViewController; public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) { Shared.Application.IsGpsEnable = false; Shared.Application.IsMusicEnable = false; base.FinishedLaunching(application, launchOptions); Shared.Application.FontSize = 12; Shared.Application.IsUsePingFang = true; Window = new UIWindow(UIScreen.MainScreen.Bounds); rootViewController = new UINavigationController(new GateWay.Ios.MainViewController()) { NavigationBarHidden = true }; Window.RootViewController = rootViewController; Window.MakeKeyAndVisible(); Shared.IOS.TBL.WXApi.RegisterApp("wx2ec8f53f6fa36e82", "https://hdlcontrol.com/ZigbeeApp/"); DeviceTokenAction += (deviceToken) => { Shared.Common.Config.Instance.RegistrationID = deviceToken; }; haveToSignOut = false; if (launchOptions != null) { // check for a remote notification if (launchOptions.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey)) { NSDictionary userInfo = launchOptions[UIApplication.LaunchOptionsRemoteNotificationKey] as NSDictionary; if (userInfo != null) { NSString key_hiddenJson = new NSString("HiddenJson"); if (userInfo.ContainsKey(key_hiddenJson)) { NSDictionary hiddenJsonDic = userInfo["HiddenJson"] as NSDictionary; if (hiddenJsonDic != null) { NSString key_Offline = new NSString("Offline"); if (hiddenJsonDic.ContainsKey(key_Offline)) { string signOut = hiddenJsonDic["Offline"].ToString(); if (signOut == "0") { //不下线 } else { //标记下线 haveToSignOut = true; } } } } } } } AppCenter.Start("0ca801a2-742b-42fb-ba10-7ba4391371aa", typeof(Analytics), typeof(Crashes)); GDMapKit.setGDApiKey("866fae25f336bc8d9a79b1d19267fffb");//高德地图key return true; } public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification) { } public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action completionHandler) { if (userInfo == null) { return; } Console.WriteLine(userInfo); if (application.ApplicationState == UIApplicationState.Active || application.ApplicationState == UIApplicationState.Background) { NSString key_hiddenJson = new NSString("HiddenJson"); if (userInfo.ContainsKey(key_hiddenJson)) { NSDictionary hiddenJsonDic = userInfo["HiddenJson"] as NSDictionary; if (hiddenJsonDic == null) { return; } NSString key_Offline = new NSString("Offline"); if (hiddenJsonDic.ContainsKey(key_Offline)) { string signOut = hiddenJsonDic["Offline"].ToString(); if (signOut == "0") { //不下线 } else { //强制下线 CommonPage.Instance.SingOut(); CommonPage.Instance.ShowErrorInfoAlert(Shared.R.MyInternationalizationString.uHadBeenLoginAndOffLine); } } } } else { NSString key_hiddenJson = new NSString("HiddenJson"); if (userInfo.ContainsKey(key_hiddenJson)) { NSDictionary hiddenJsonDic = userInfo["HiddenJson"] as NSDictionary; if (hiddenJsonDic == null) { return; } NSString key_Offline = new NSString("Offline"); if (hiddenJsonDic.ContainsKey(key_Offline)) { string signOut = hiddenJsonDic["Offline"].ToString(); if (signOut == "0") { //不下线 } else { //标记下线 haveToSignOut = true; } } } } completionHandler(UIBackgroundFetchResult.NewData); } public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo) { Console.WriteLine(userInfo); if (application.ApplicationState == UIApplicationState.Active) { System.Console.WriteLine("ReceivedRemoteNotification1"); } else if (application.ApplicationState == UIApplicationState.Background) { System.Console.WriteLine("ReceivedRemoteNotification2"); } else if (application.ApplicationState == UIApplicationState.Inactive) { System.Console.WriteLine("ReceivedRemoteNotification3"); } } public override void OnResignActivation(UIApplication application) { // Invoked when the application is about to move from active to inactive state. // This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) // or when the user quits the application and it begins the transition to the background state. // Games should use this method to pause the game. base.OnResignActivation(application); } public override void DidEnterBackground(UIApplication application) { // Use this method to release shared resources, save user data, invalidate timers and store the application state. // If your application supports background exection this method is called instead of WillTerminate when the user quits. //Shared.BusSocket.Stop ();On在这里停止BusSocket ZigBee.Common.Application.FindGateWaySocket.Stop(); base.DidEnterBackground(application); } public override void WillEnterForeground(UIApplication application) { // Called as part of the transiton from background to active state. // Here you can undo many of the changes made on entering the background. System.Console.WriteLine("WillEnterForeground"); UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0; base.WillEnterForeground(application); } public override void OnActivated(UIApplication application) { // Restart any tasks that were paused (or not yet started) while the application was inactive. // If the application was previously in the background, optionally refresh the user interface. base.OnActivated(application); if (haveToSignOut == true) { //强制下线 CommonPage.Instance.SingOut(); CommonPage.Instance.ShowErrorInfoAlert(Shared.R.MyInternationalizationString.uHadBeenLoginAndOffLine); } //检查连接远程 //代码 } public override void WillTerminate(UIApplication application) { // Called when the application is about to terminate. Save data, if needed. See also DidEnterBackground. base.WillTerminate(application); } public static Action RespAction; public static void WXLogin() { var req = new SendAuthReq { Scope = "snsapi_userinfo", State = "ZigbeeApp", OpenID = "0c806938e2413ce73eef92cc3" }; WXApi.SendAuthReq(req, rootViewController, null, null); } MyWXApiDelegate myWXApiDelegate = new MyWXApiDelegate { }; class MyWXApiDelegate : WXApiDelegate { public override void OnReq(BaseReq req) { } public override void OnResp(BaseResp resp) { if (resp is SendAuthResp) { var sendAuthResp = resp as SendAuthResp; switch (sendAuthResp.ErrCode) { case 0: try { var result = new System.Net.WebClient { }.DownloadString($"https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx2ec8f53f6fa36e82&secret=a08585cd6ff2ce64570b9e7e6525dd8e&code={((SendAuthResp)resp).Code}&grant_type=authorization_code"); RespAction?.Invoke(result); } catch { RespAction?.Invoke(null); } break; default: RespAction?.Invoke(null); break; } } } } public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler) { return WXApi.HandleOpenUniversalLink(userActivity, myWXApiDelegate); } } } namespace Shared { public static class QRCode { static ZXing.Mobile.MobileBarcodeScanner scanner; public static async System.Threading.Tasks.Task ScanQRcode(Action action, string cancel = "取消", string flashText = "闪光灯", string titleText = "二维码扫描") { if (scanner == null) { var mZXingOverlayView = new Home.IOS.ZXingOverlayView(cancel, flashText, titleText); scanner = new ZXing.Mobile.MobileBarcodeScanner(Home.IOS.AppDelegate.rootViewController) { FlashButtonText = flashText, TopText = titleText, BottomText = "", CancelButtonText = cancel }; scanner.UseCustomOverlay = true; scanner.CustomOverlay = mZXingOverlayView; var bOn = false; mZXingOverlayView.OnCancel += () => { scanner?.Cancel(); }; mZXingOverlayView.OnTorch += () => { bOn = !bOn; scanner?.Torch(bOn); }; } var result = await scanner.Scan(); if (result != null) action?.Invoke(result.Text); else action?.Invoke(null); } public static byte[] BytesFromText(string text, int width = 300, int height = 300) { var barcodeWriter = new ZXing.Mobile.BarcodeWriter { Format = ZXing.BarcodeFormat.QR_CODE, Options = new ZXing.Common.EncodingOptions { Width = width, Height = height, Margin = 0 } }; barcodeWriter.Renderer = new ZXing.Mobile.BitmapRenderer(); var uiImage = barcodeWriter.Write(text); var data = uiImage.AsJPEG(); var resultBytes = new byte[data.Length]; System.Runtime.InteropServices.Marshal.Copy(data.Bytes, resultBytes, 0, resultBytes.Length); return resultBytes; } } }