Search code examples
iossdkudid

iOS: Detect whether my SDK is installed on another apps on the device


I am developing a location based Q&A SDK for mobile devices.

When a question is asked about a specific location, the server side targets the most relevant user and sends the question to that user. If the user fails to answer, the question is sent to the second best user, and so on.

The problem is that my SDK might be installed on more than one application on the device, meaning that the user can get a question more than once.

Is there a way to detect whether my SDK is installed on more than one app? I thought that sending the UDID to the server might work, but iOS UDIDs differ between applications.


Solution

  • You can use UIPasteboard to share data between applications on the device.

    The UIPasteboard class enables an app to share data within the app and with another app. To share data with any other app, you can use system-wide pasteboards; to share data with another app that has the same team ID as your app, you can use app-specific pasteboards.

    In your SDK, do something like this:

    @interface SDKDetector : NSObject
    
    @end
    
    @implementation SDKDetector
    
    + (void)load
    {
        int numberOfApps = (int)[self numberOfAppsInDeviceUsingSDK];
        NSLog(@"Number of apps using sdk:%d", numberOfApps);
    }
    
    + (NSInteger)numberOfAppsInDeviceUsingSDK
    {
        static NSString *pasteboardType = @"mySDKPasteboardUniqueKey";
    
        NSData *value = [[UIPasteboard generalPasteboard] valueForPasteboardType:pasteboardType];
        NSMutableArray *storedData = [[NSKeyedUnarchiver unarchiveObjectWithData:value] mutableCopy];
    
        if (!storedData) {
            storedData = [NSMutableArray new];
        }
    
        NSString *bundleId = [[NSBundle mainBundle] bundleIdentifier];
        if (![storedData containsObject:bundleId]) {
            [storedData addObject:[[NSBundle mainBundle] bundleIdentifier]];
        }
    
        value = [NSKeyedArchiver archivedDataWithRootObject:storedData];
        [[UIPasteboard generalPasteboard] setData:value forPasteboardType:pasteboardType];
    
        return [storedData count];
    }
    
    @end