黄学彪
2019-11-25 5727cf0b9b54da0a191dd1e23cb5abf21320fbff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System;
using Foundation;
using GateWay.Ios;
using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
using Shared;
using Shared.Common;
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)
        {
            //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));
            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(Home.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;
        }
    }
}