Monitoring Keychain item changes in Swift - swift

The Keychain Services API allows adding/updating/deleting items from the keychain. However, it doesn't seem to include any mechanism for watching a specific keychain item for changes.
My app is storing a value in the keychain, and I want to be notified if that value gets changed by the user or another process. Is such a thing possible, or will I just have to periodically poll the keychain item and check for changes myself?

Here is API of Security framework for your purpose
/**
#function SecKeychainAddCallback
#abstract Registers your keychain event callback function
#param callbackFunction A pointer to your keychain event callback function, described in SecKeychainCallback. You indicate the type of keychain events you want to receive by passing a bit mask of the desired events in the eventMask parameter.
#param eventMask A bit mask indicating the keychain events that your application wishes to be notified of. See SecKeychainEventMask for a description of this bit mask. The Keychain Manager tests this mask to determine the keychain events that you wish to receive, and passes these events in the keychainEvent parameter of your callback function. See SecKeychainEvent for a description of these events.
#param userContext A pointer to application-defined storage that will be passed to your callback function. Your application can use this to associate any particular call of SecKeychainAddCallback with any particular call of your keychain event callback function.
#result A result code. See "Security Error Codes" (SecBase.h).
*/
public func SecKeychainAddCallback(_ callbackFunction: SecKeychainCallback,
_ eventMask: SecKeychainEventMask,
_ userContext: UnsafeMutableRawPointer?) -> OSStatus

Related

What should be used as an event parameter with FBSDKAppEventParameterNameOrderID key in Facebook Analytics SDK?

FBSDKAppEventParameterNameOrderID is name for event parameter for both FBSDKAppEventNameSubscribe and FBSDKAppEventNameStartTrial events. Official documentation contains the following description for it.
/** Parameter key used to specify the unique ID for all events within a subscription
* in an FBSDKAppEventNameSubscribe or FBSDKAppEventNameStartTrial event. */
FOUNDATION_EXPORT NSString *const FBSDKAppEventParameterNameOrderID;
As for me, there are two meaningful things, which may be put in the subscription events:
Original Transaction Identifier, which can be used by Facebook Analytics' backend to collapse all reported subscription renewals into a single real subscription.
Transaction Identifier, which is unique for each new subscription and all its renewals. That is what Facebook Analytics SDK is looking for according to documentation, but less meaningful from a marketing point of view.
So what is the right value for FBSDKAppEventParameterNameOrderID parameter in FBSDKAppEventNameSubscribe/FBSDKAppEventNameStartTrial events?

Swift Application URL Handler

I am trying to determine from which application did my application get launched. I have created the app to handle http(s) requests and registered a URL handler within the applicationWillFinishLaunching. This is all working as expected. Within the event handler the first parameter is an Apple Event Descriptor, which has 2 items. The first is the URL that was clicked on. The second item is also an Apple Event Descriptor and it contains a data structure with the name and the identifier of the previous application.
For example, if I was in Mail and clicked on a URL. The URL is in the first Event Descriptor. When converted to a string, the second descriptor contains a bundle identifier and agent name in a dictionary.
Is there a way to parse the second event data structure to retrieve the information?

How to replace auto generated easyrtc id with your applications username in easyrtc application

I am developing one application using easyrtc tool with wavemaker tool.For a new user easy rtc provides automatically created easyrtc id.
In the chat window the random id are shown..i want to replace these ids with applications username..
I have find one solution where we have to set easyrtc.setUsername("") in client js file before calling easyrtc.connect function..
But this not solves the problem...
any help would be appriciated
Now, you can do it easyer, use this function:
easyrtc.idToName(easyrtcid)
Their is no easy way to solve this. However, it is possible using a mixture of server-side and client-side events to pass/receive user metadata when connected/disconnected. Here is a simple way to achieve this:
When a client connects to the server send user metadata via sendServerMessage on the connected event listener via client-side library. The server then receives the message from the client and stores the metadata about the user with that particular easyrtcid in a central location (ex. redis). The message sent to the server can be a json object with user metadata in a structured format. See details on connecting and sending a message to the server here: easyRTC Client-Side Documentation
When a client disconnects from the server remove their information from the data store using the onDisconnect event on the server side. This event provides a connectionObj which includes the easyrtcid of the user who disconnected. Use this identifier to remove the user from the datastore. You could also call generateRoomList() on the connectionObj to remove the user by easyrtcid and room from your datastore. You can read about the connection object here: connectionObj easyRTC documentation
Here is some example code of how to do this:
// Client-Side Javascript Code (Step 1)
easyrtc.connect('easyrtc.appname', function(easyrtcid){
// When we are connected we tell the server who we are by sending a message
// with our user metadata. This way we can store it so other users can
// access it.
easyrtc.sendServerMessage('newConnection', {name: 'John Smith'},
function(type, data){
// Message Was Successfully Sent to Server and a response was received
// with a the data available in the (data) variable.
}, function(code, message) {
// Something went wrong with sending the message... To be safe you
// could disconnect the client so you don't end up with an orphaned
// user with no metadata.
}
}, function(code, message) {
// Unable to connect! Notify the user something went wrong...
}
Here is how things would work on the server-side (node.js)
// Server-Side Javascript Code (Step 2)
easyrtc.events.on('disconnect', function(connectionObj, next){
connectionObj.generateRoomList(function(err, rooms){
for (room in rooms) {
// Remove the client from any data storage by room if needed
// Use "room" for room identifier and connectionObj.getEasyrtcid() to
// get the easyrtcid for the disconnected user.
}
});
// Send all other message types to the default handler. DO NOT SKIP THIS!
// If this is not in place then no other handlers will be called for the
// event. The client-side occupancy changed event depends on this.
easyrtc.events.emitDefault("disconnect", connectionObj, next);
});
Redis is a great way to keep track of the users connected if using rooms. You can use an hash style object with the first key being the room and each sub key/value being the users easyrtcid with a JSON hash of the metadata stored as it's value. It would have to be serialized to a string FYI and de-serialized on the lookup but this is simple using Javascript using the JSON.stringify and JSON.parse methods.
To detect occupancy changes in your application you could add a event listener to the easyrtc.setRoomOccupantListener method on the client-side and then when this event is fired send another message to the server to get all the users connected to it from the datastore.You would have to listen for a separate message on the server-side and return the users in the store deserialized back to the client. However, depending on your application this may or may not be needed.

Facebook PHP SDK - getLoginUrl() - state value

I am using the PHP SDK getLoginUrl() function which works perfectly to log the user in. Once the user is redirected back to my page, the URL can come in two forms, see in the following link subsection 3: http://developers.facebook.com/docs/authentication/server-side/
Part of the return URL is a ?state= value. This value is supposed to be used to prevent Cross Site Request Forgery: http://developers.facebook.com/docs/reference/dialogs/oauth/
Though, using the getLoginUrl() method I can never set a state value as it is not one of the parameters: http://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/
So how can I utilize the state-value to log a user into facebook and prevent CSRF?
So how can I utilize the state-value to log a user into facebook and prevent CSRF?
This is being automatically handled by the Facebook PHP SDK. If you were about to write your own API calls to Facebook, you would need to submit the state manually (if desired) as per Facebook's OAuth documentation.
When you create a login url with BaseFacebook::getLoginUrl(), the first thing the function does is to establish CSRF token state1, which creates a hash using PHP's core mt_rand(), uniqid() and md5() functions and also stores the value as a session variable.
When the user gets redirected back to your page the, FBSDK checks if the submitted state matches the state value in the session. If the values indeed match, the state is cleared from the Facebook object and from the session, so all subsequent getLoginUrl() requests would get a new state variable.2
Theoretically you could use your own state value with FBSDK by writing it to fb_<your_app_id>_state session variable before constructing the Facebook-object, as the BaseFacebook's constructor and establishCSRFTokenState() both check if the state already exists in the session.
But that would probably introduce more complexity than is necessary.
see BaseFacebook::establishCSRFTokenState()
see BaseFacebook::getCode()

How to receive UIAccessibilityNotifications in iPhone App

I'm interested in capturing UI changes in my application programmatically and thought that the UIAccessibility protocol may help. I've found how to post UIAccessibilityLayoutChangedNotification and UIAccessibilityScreenChangedNotification but I'm not sure how to register to receive these notifications.
I've tried using NSNotificationCenter, but the name param expects a string, while the two notifications above are of the type UIAccesibilityNotifications which is an int.
Any idea how to register for these notifications?
Thanks!
That's a great question! Unfortunately you cannot receive these "notifications" without affecting normal behavior. (i.e. "no you can't")
If you disassemble UIKit, you'll find UIAccessibilityPostNotification is implemented like this:
static void (*__UIAccessibilityBroadcastCallback)(UIAccessibilityNotifications notification, id argument);
void UIAccessibilityPostNotification(UIAccessibilityNotifications notification, id argument) {
__UIAccessibilityBroadcastCallback (notification, argument);
}
That means these accessibility "notifications" aren't any normal notifications. Rather, they are just parameters to an internal callback function. How the callback function is implemented depends on the accessibility bundle you're using.
You can replace the callback function with your own using the undocumented API _UIAccessibilitySetBroadcastCallback:
void _UIAccessibilitySetBroadcastCallback(void (*replacement)(UIAccessibilityNotifications notification, id argument)) {
__UIAccessibilityBroadcastCallback = replacement;
}
However, there isn't a corresponding "get" function (not even private), so once you set it, the original listeners cannot be notified again.