Store an array on the app group for WidgetKit - ios-app-group

I'm trying to store an struct array into the app group container to use it later on the widget.
let's assume, I have an array of string
let = array = ["object1", "object2", "object3"]
and I saw here I can access to the app group container url
let applicationGroupId = "group.com.development.widget"
guard let groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: applicationGroupId) else {
fatalError("could not get shared app group directory.")
}
Now, I need to know how I can store this array on app group and how I can read from it on the Widgetkit SwiftUI class
Your help will be appreciated.

Signing, Capabilities, Entitlements
Right now it should be possible to do all of this from within Xcode (12+). In the app's project settings go to the Signing & Capabilities tab and make sure Automatically manage signing is enabled and the Team has a valid development team (either company or personal account).
Tap on the + Capability button on the top left to enter the App Group Capability (this will also add an entitlements file). Now you will find an App Group section. Hit the + button for App Groups and enter a globally unique identifier (something like group.<appid>). This will create the app group on the developer platform and add it to your app's provisioning profile and entitlements.
You must repeat the same steps and use the same app group for all targets (app target, widget extension target and (optionally) intent targets). If you hit the reload button for the other targets' app group it should automatically show the app group you previously registered and you can check the checkbox. If not, hitting + and using the same id should work as well.
Finally, check all entitlements files and ensure that they have the same String in App Groups (copy and paste just to make sure).
Code
In code you now just have to access the user defaults using the chosen app group as suitName. To test whether everything is set up correctly, I would put the following line in application(_:didFinishLaunchingWithOptions):
UserDefaults(suiteName: "group.id.test")!.setValue(["object1", "object2", "object3"], forKey: "test")
In the Widget just try to put it in a Text view:
struct WidgetTestEntryView : View {
var entry: Provider.Entry
var text: String {
(UserDefaults(suiteName: "group.id.test")!.value(forKey: "test") as? [String])?.joined(separator: " ") ?? "Nothing stored yet"
}
var body: some View {
Text(text)
}
}
Note: I force unwrap the user defaults on purpose to fail early and check whether the setup of capabilities/signing worked. In production you should guard against a failure.
Alternative Storage
As you pointed out it's also possible to persist data in a shared container by writing to url provided by FileManager.default.containerURL(forSecurityApplicationGroupIdentifier:) but I don't think you need that for your simple example. If you want to share data that is more complex than String, Int, Array ... make sure to convert it to Data since user defaults cannot handle arbitrary data (even if it's Codable).
Manual Signing
If you use manual signing you just have to do the same step in the developer console starting with registering an app group. After that, add the group to all identifiers' capabilities and recreate all provisioning profiles. From there you should be use similar steps to the ones described above.

Related

Does the "/Volumes" name changes based on system language, if yes how to adapt this?

I am using this code to check if the client runs from DMG /Volume, and show an alert and quit.
private void IsRunningFromDMG()
{
var currentPath = NSBundle.MainBundle.BundlePath;
if (currentPath.StartsWith("/Volumes", StringComparison.Ordinal))
{
using (var alert = new NSAlert
{
MessageText = "Warning",
InformativeText = "You cannot run this application from DMG, drag move to the " +
"\"/Applications\" folder"
})
{
alert.RunModal();
NSApplication.SharedApplication.Terminate(null);
}
}
}
This is fine if the system language is set to English. I doubt if the /Volumes reacts to localization in that case how to do it?
There should be a system defined constant that would take care of it?
If it is, please help me to know what is it.
Filesystem paths do not change based on localization, they are localized for "display" though (i.e. Pictures, Music, etc... would be localized in Finder for display to the user but the filesystem path is still /User/xxx/Pictures, etc...)
Localized names. The system provides localized names for many system directories, such as Applications, Library, Music, Movies. An app may similarly provide localized names for itself and for any directories it creates.
Display names do not affect the actual name of the file in the file system. Code that accesses a file or directory programmatically must specify the item’s actual name when opening or manipulating the item using the file system interfaces. The only time your app should ever use display names is when displaying the name of a file or directory to the user. You can get the display name for any file or directory using the displayNameAtPath: method of NSFileManager.
i.e.
var urls = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.ApplicationsDirectory, NSSearchPathDomain.System);
var applicationDisplayName = NSFileManager.DefaultManager.DisplayName(urls[0].AbsoluteString);
Note: You should use NSSearchPathDirectory.AllApplicationsDirectory within the NSSearchPathDomain.All domain to obtain a valid list of "Application" urls where your application might/should be located at, instead of hard coding it to the "System" application location.

Custom Grid App

So I am trying to make modifications to the custom grid app that rally has already created. I found the source code at https://github.com/RallyApps/app-catalog/tree/master/src/apps/grid. However, I cannot get that code to work. I have added it to a js and then ran build (I also changed the json). However, when I then add the app to rally it doesn't give me the settings options (object, query, pagesize, etc) and just generates a table. This table generates 4 rows (the number of user stories I have), but the rows are completely blank except for gears at the beginning of each row. I was wondering if I was building this app incorrectly or if I had grabbed the wrong code. If not, is there a place where can I get the complete code or a way to modify the already existing code?
Thanks
You may add a fetch to the config as below:
config: {
defaultSettings: {
types: 'hierarchicalrequirement',
fetch: 'FormattedID,Name'
}
}
and it will display a grid.
It will not display settings dialog however. The source code in this github repo is not exactly the CustomGrid app available from the AppCatalog in the UI. It does not have the full functionality and not ready yet. I submitted a defect.

User Specific App Settings

I have started working with server side app settings and it would appear the default mechanics for a shared app is to save the app settings across the entire project scope; so if an individual makes changes to the app settings, those changes are reflected for all future users. This is not the ideal use in my specific case. I would like it if I could have different settings for each user, without them each adding separate instances of my custom HTML. Is this possible using server side settings, or will I need to look into using cookies to save the settings on each user?
Note: I've read the documentation on app setting scope (https://help.rallydev.com/apps/2.0rc1/doc/#!/guide/settings-section-define-scope) but it doesn't appear as though "user" is an option.
Thanks!
Conner, you're making a good argument for User-level settings for an app.
Unfortunately, we don't support that currently. The settingsScope option in AppSettings only supports the values app, project, or workspace.
Creating an instance of the app for each user (such as on their individual dashboard) is the best alternative I can think of. But as you mentioned, this is not ideal for you.
I have solved this issue by prepending the user ObjectID to each setting value before I save or load it from the Rally servers. Here is the code for my user settings set/get functions:
setUserSetting : function(settingName, settingValue, callback) {
var settings = {};
settings[App.getContext().getUser().ObjectID + settingName] = settingValue;
App.updateSettingsValues({
settings : settings,
success : function() {
App.setSettings(Ext.Object.merge(App.getSettings(), settings));
callback();
}
});
},
getUserSetting : function(settingName) {
return App.getSetting(App.getContext().getUser().ObjectID + settingName);
}
BTW, it seems kind of strange that I have to save the settings in the way I have. The "updateSettingsValues" function sets the settings on the next load of the app and the "setSettings" function sets it for the current runtime of the app. It's a strange way to have to do it, but it works.

Detecting if a running application is sandboxed

Given an application's pid, is there any way, programatically, of detecting if that application is running in an OSX sandbox environment?
Ideally, I'd like to know if there's an API call somewhere, preferably in C, rather than objective-C (for a daemon, so not using Cocoa), but if not, is there any other way of checking?
#Linuxios was right partly right about there being a CoreFoundation call. In fact, there are a few that when combined, can be used to solve this and it's based on the call to SecStaticCodeCheckValidityWithErrors
For anyone that may want, or need to programmatically test for an app being sandboxed can follow this blog.
Also, the full code to the article has been added to Github here.
First you must get the path of the application from the pid, and then you can use the command codesign --display --entitlements - app_path to view all the entitlements. If the app has the entitlements com.apple.security.app-sandbox set to true then it is sandboxed.
You can take a look here.
For detecting the sandbox in Flex/AIR/AS3 you can use the following kludge. The same approach should also work in objc. The only condition under which this would not work would be if the Documents folder were entirely empty. Or you could use any other folder that is off-limits to the sandbox.
var file:File = File.userDirectory;
var a:Array = file.nativePath.split("/");
var userName:String = a[2];
var docFolder:File = new File("/Users/" + userName + "/Documents/");
var dirList:Array = docFolder.getDirectoryListing();
if (dirList.length>0) {
docDirectoryDisplay.text = "App is NOT sandboxed.";
} else {
docDirectoryDisplay.text = "App is sandboxed.";
}

Kaltura - Force player to stop with API only?

Is there any way to force a Kaltura videoplayer to stop ONLY using code and the Kaltura API?
Currently I have solved it by adding a Access Control Profile named "Free preview" under Settings > Access Control in KMC and then added this profile to the Entries I've choosen.
I then add the session to the players flashvars to restrict non-members to only watch the preview, not the whole clip.
But I would like to restrict ALL, or even better selected Categories of clips by using only code, so I don't need to involve KMC.
Is that possible?
Alt) Can you create a new player in KMC and restrict it to viewing only X seconds, no matter what length of Entry? Then I can do the check if user is valid or not and get the category via API and show it in the "preview-player" och the "default player".
If I use the mediaProxy.mediaPlayTo attribute the clip stops, but is easily started again by presing play.
Would greatly appreciate an answer
I got this answer from a guy named oferc in a different forum:
You can listen to the head move event and pause the clip it goes beyond a certain time (then if someone pressed play, you can stop it again)
function jsCallbackReady(player_id) {
my_kdp = $("#"+player_id).get(0); // document.getElementById(player_id) if you do not use jquery/ prefer pure js
my_kdp.addJsListener("kdpReady", "kdpReady"); // when you load the player with an entry (and the player is ready to begin playing it using doPlay for instance)
}
function kdpReady() {
my_kdp.addJsListener("playerUpdatePlayhead","headMove");
}
function headMove(position) {
if (position > "30") { // Your Time, example 30 seconds
my_kdp.sendNotification('doStop')
}
}
Works like a charm!
fredrik_w - neither of the ways you chosen here are a good option to restrict access.
in both cases, your videos are made public, and can be easily accessible by anyone.
The best way to limit access to a video is by defining an Access Control, and like everything in Kaltura, you can define an ACL using API as well.
Check this out as a reference sample-
http://blog.kaltura.org/turning-profit-online-video-made-easy-using-paypal-html5-digital-goods