WJC
2019-12-30 f1c3921b08bb22ac6f5db22d620e01d7e8e5c49f
ZigbeeApp/GateWay.Ios/AppDelegate.cs
New file
@@ -0,0 +1,263 @@
using Foundation;
using UIKit;
using Shared;
using System;
using ObjCRuntime;
using UserNotifications;
//using Shared.Common;
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
namespace GateWay.Ios
{
    // The UIApplicationDelegate for the application. This class is responsible for launching the
    // User Interface of the application, as well as listening (and optionally responding) to application events from 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)
        {
            //base.FinishedLaunching(application, launchOptions);
            Shared.Application.FontSize = 12;
            Window = new UIWindow(UIScreen.MainScreen.Bounds);
            rootViewController = new UINavigationController(new MainViewController()) { NavigationBarHidden = true };
            Window.RootViewController = rootViewController;
            Window.MakeKeyAndVisible();
            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<UIBackgroundFetchResult> 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.YouHaveBeenSignOut);
                        }
                    }
                }
            }
            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.YouHaveBeenSignOut);
            }
            //检查连接远程
            //代码
        }
        public override void WillTerminate(UIApplication application)
        {
            // Called when the application is about to terminate. Save data, if needed. See also DidEnterBackground.
            base.WillTerminate(application);
        }
    }
}
namespace Shared
{
    public static class QRCode
    {
        static ZXing.Mobile.MobileBarcodeScanner scanner;
        public static async System.Threading.Tasks.Task ScanQRcode(Action<string> action, string topText = "", string bottomText = "", string cancel = "Cancel")
        {
            if (scanner == null)
            {
                scanner = new ZXing.Mobile.MobileBarcodeScanner(GateWay.Ios.AppDelegate.rootViewController) { FlashButtonText = "", TopText = topText, BottomText = bottomText, CancelButtonText = cancel };
            }
            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 = 10
                }
            };
            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;
        }
    }
}