public Task UpdateMultipleEntities(IEnumerable <Session> data) { return(Task.Run(() => { var fileManager = new NSFileManager(); var appGroupContainer = fileManager.GetContainerUrl($"group.{AboutThisApp.PackageName}"); var appGroupContainerPath = appGroupContainer.Path; var sessionsFilePath = Path.Combine(appGroupContainerPath, "sessions.json"); if (fileManager.FileExists(sessionsFilePath)) { NSError error; fileManager.Remove(sessionsFilePath, out error); } var attributes = new NSFileAttributes { Type = NSFileType.Regular }; var json = JsonConvert.SerializeObject(data.Where(s => s.IsFavorite).ToList()); var fileData = NSData.FromString(json); fileManager.CreateFile(sessionsFilePath, fileData, attributes); var url = NSUrl.FromFilename(sessionsFilePath); url.SetResource(NSUrl.IsExcludedFromBackupKey, new NSNumber(true)); var settings = new NSUserDefaults($"group.{AboutThisApp.PackageName}", NSUserDefaultsType.SuiteName); settings.SetBool(true, "FavoritesInitialized"); settings.Synchronize(); UpdateWidget(); })); }
public override void DidSelectPost() { if (ExtensionContext.InputItems != null) { NSExtensionItem item = ExtensionContext.InputItems[0]; if (item.Attachments != null && item.Attachments.Length > 0) { var fileManager = new NSFileManager(); var appGroupContainer = fileManager.GetContainerUrl("group.com.shareapp.ios"); var appGroupContainerPath = appGroupContainer.Path; string directoryPath = Path.Combine(appGroupContainerPath, "DocPro"); Directory.Delete(directoryPath, true); fileManager.CreateDirectory(directoryPath, true, null); int count = 0; foreach (NSItemProvider itemProvider in item.Attachments) { if (itemProvider.HasItemConformingTo(UTType.Image)) { string tempPath = string.Empty; try { itemProvider.LoadItem(UTType.Image, null, (NSObject itemObject, NSError error) => { NSData imgData = null; if (itemObject is UIImage) { using (imgData = ((UIImage)itemObject).AsPNG()) { byte[] myByteArray = new byte[imgData.Length]; Marshal.Copy(imgData.Bytes, myByteArray, 0, Convert.ToInt32(imgData.Length)); var fileIOS = new FileIOS(); fileIOS.WriteAllBytes("Teste123.png", myByteArray); tempPath = fileIOS.GetFilePath("Teste123.png"); } } else if (itemObject is NSUrl) { NSUrl nsurl = (NSUrl)itemObject; var nsdata = NSData.FromUrl(nsurl); if (nsdata != null) { var data = UIImage.LoadFromData(nsdata); string pngFilename = Path.Combine(directoryPath, $"Img{count}.png"); count++; using (imgData = data.AsPNG()) { //NSError err = null; //bool isSaved = imgData.Save(pngFilename, false, out err); bool isFileCreated = fileManager.CreateFile(pngFilename, imgData, (NSDictionary)null); } } //using (imgData = (Data).AsPNG()) //{ // byte[] myByteArray = new byte[imgData.Length]; // Marshal.Copy(imgData.Bytes, myByteArray, 0, Convert.ToInt32(imgData.Length)); // var fileIOS = new FileIOS(); // fileIOS.WriteAllBytes("Teste123.png", myByteArray); //} } }); //var alert2 = UIAlertController.Create("Funfou", tempPath, UIAlertControllerStyle.Alert); //PresentViewController(alert2, true, () => //{ // DispatchQueue.MainQueue.DispatchAfter(new DispatchTime(DispatchTime.Now, 5000000000), () => // { // // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context. // }); //}); UIApplication.SharedApplication.OpenUrl(new NSUrl("com.shareapp.test://")); } catch (Exception) { } } } } ExtensionContext.CompleteRequest(ExtensionContext.InputItems, null); } }