/// <summary> /// Checks whether the app was launched from the share extension. If it is, the CropViewController is launched without user input. /// </summary> private void CheckLaunchFromShareExtension() { if (((AppDelegate)UIApplication.SharedApplication.Delegate).LaunchedFromShareExtension) { // reset to false to prevent this from executing when the user returns to home page ((AppDelegate)UIApplication.SharedApplication.Delegate).LaunchedFromShareExtension = false; NSUserDefaults sharedDefaults = new NSUserDefaults(Constants.AppGroupIdentifier, NSUserDefaultsType.SuiteName); NSData imageData = sharedDefaults.DataForKey(Constants.ShareExtensionImageKey); sharedDefaults.SetValueForKey(new NSData(), new NSString(Constants.ShareExtensionImageKey)); try { UIImage image = new UIImage(imageData); //NOTE EXIF data is not retrieved NSUrl url = null; LaunchCropViewController(image, url, this.NavigationController); } catch (Exception e) { // If data was not able to be retrieved var alert = UIAlertController.Create("Error Launching from Share Extension", "An unexpected error occured.", UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null)); PresentViewController(alert, true, null); } } }
/// <summary> /// Retrieves image(s) from share extension and launches the analysis page. /// </summary> private void LaunchFromShareExtension() { if (!AppDelegate.LaunchedFromShareExtension) { return; } AppDelegate.LaunchedFromShareExtension = false; var sharedDefaults = new NSUserDefaults(SharedConstants.AppGroupID, NSUserDefaultsType.SuiteName); var imageData = sharedDefaults.DataForKey(SharedConstants.ImageKey); sharedDefaults[SharedConstants.ImageKey] = new NSData(); sharedDefaults.Dispose(); try { var image = UIImage.LoadFromData(imageData); var imageName = string.Empty; ImageEntries.Add(new ImageEntry(image.ToSKImage(), imageName, DateTime.UtcNow, null)); LaunchAnalysisScreen(ImageEntries); } catch (ArgumentNullException) { var alert = UIAlertController.Create(SharedConstants.ShareExtensionErrorAlertTitle, SharedConstants.ShareExtensionErrorAlertMessage, UIAlertControllerStyle.Alert); alert.AddAction(UIAlertAction.Create(SharedConstants.ShareExtensionErrorAlertOkAction, UIAlertActionStyle.Default, null)); PresentViewController(alert, true, null); } }
// Loads the data from `NSUserDefaults`. void LoadData() { UserDefaultsAccessQueue.DispatchSync(() => { NSData archivedData = UserDefaults.DataForKey(StorageDescriptor.Key); try { // Let the derived classes handle the specifics of // putting the unarchived data in the correct format. // This is necessary because the derived classes // (SoupMenuManager, SoupOrderMenuManager) are using // generic data formats (NSMutableSet<T> or NSMutableArray<T>) // and these types cannot be casted directly from the // deserialized data. NSObject unarchivedData = NSKeyedUnarchiver.UnarchiveObject(archivedData); FinishUnarchiving(unarchivedData); } catch (Exception e) { if (!(e is null)) { Console.WriteLine($"Error: {e.Message}"); } } }); }
void SetElementValueFromDefaults(CheckboxElement e, NSUserDefaults defs, string key, bool defaultVal) { var value = defs.DataForKey (key); if (value == null) e.Value = defaultVal; else e.Value = defs.BoolForKey(key); }