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

风格不统一 提交于 2019-12-10 13:47:04

问题


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.


回答1:


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



回答2:


If you only want to provide an SDK, it is not possible. Apple has added security steps to prevent that for user privacy. Keychain sharing will not work, because apps must share the same bundle seed ID (see here for more info).

If you want to provide an app along with your SDK, then you could do something like Facebook does, where app sends a "helo" message, Facebook asks user and finally Facebook sends "ehlo" message.

Your App -> "I would like to use the SDK; please give me token" -> SDK Controller App -> (Remember which apps have requested use) -> "OK, you can use the SDK; here is a token: #123" -> Your App

The SDK controller app can now send the server the list of apps.




回答3:


I think you can group the apps on the same device by IP address as they will use the same address to connect to your server. So the IP address will represent the device and the API key will represent the app that uses the SDK.




回答4:


Can you try using

advertisingIdentifier

Not sure whether it serves your purpose. It is explained in here ASIdentifierManager class reference : Apple doc




回答5:


I think its possible using keychain, you can have an unique keychain key in which you can save anything you want, and can be accessed by other apps if available. So for your SDK, lets say if there is one app, it will register some value in keychain with a unique key which is private to your SDK only if the key doesn't exist, and if it exist you get to know, since you can save any value in keychain, you can try multiple options and combinations which suits you.

You can use KeychainItemWrapper for the implementations.

Elaboration

Lets say we have an method.

[MySDK register];

Which can be used anywhere, say in AppDelegate. The register method will generate a token for the app, for the device, which we will save in the keychain using an unique key we have defined in the SDK, say in com.mysdk.key. And while saving in keychain the SDK can actually do a registration.

We consider the above method is implemented in multiple apps.

Now we have scenario.

  1. User installs an App-A which uses the SDK, the register method will call and create a token and will save in keychain for the first time.

  2. Now user installs another App-B which also uses the SDK, the same register method will call, but now it will check for the key com.mysdk.key in keychain, if exist it will just update the count for the token, which meant for the device.

Note

Keychain not meant to save only unique identifier, you can save other informations too.

Update

Check demo projects https://db.tt/7xpKrgMp

The wrapper I have used in the projects is same as SDK in your case which is same in both the projects.

Cheers.



来源:https://stackoverflow.com/questions/24252954/ios-detect-whether-my-sdk-is-installed-on-another-apps-on-the-device

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!