您可以为您的应用的免费版和专业版实施单独的custom URL scheme,以允许它们通过 URL 交换有限数量的数据。因此,专业版应用程序可能会在第一次运行时检查是否安装了精简版应用程序,如果是,则向其请求数据:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL runAlready = [userDefaults boolForKey:kRunAlready];
if (!runAlready)
{
[userDefaults setBool:YES forKey:kRunAlready];
[userDefaults synchronize];
[self importDataFromLite];
}
}
- (void)importDataFromLite
{
NSURL *liteURL = [NSURL URLWithString:@"testapp-lite://getdata"];
// if we can open test app, then test app installed, so launch it, providing "getdata" request
if ([[UIApplication sharedApplication] canOpenURL:liteURL])
{
// Getting data from lite app
[[UIApplication sharedApplication] openURL:liteURL];
}
}
精简版显然必须设置它的 Info.plist 来注册这个自定义 URL 方案,并且它的应用程序委托需要响应这个数据请求,进而调用专业应用程序的自定义 URL 方案来发送返回数据:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
[[[UIAlertView alloc] initWithTitle:nil message:[url absoluteString] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
if ([[url absoluteString] isEqualToString:@"testapp-lite://getdata"])
{
// I'm going to create URL from local NSDictionary, but you would presumably retrieve data from your model/database
NSDictionary *dictionary = @{@"name" : @"Rob", @"age" : @"29"};
NSMutableArray *array = [NSMutableArray array];
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, NSString *obj, BOOL *stop) {
[array addObject:[NSString stringWithFormat:@"%@=%@", key, [obj stringByAddingPercentEscapesForURLParameterUsingEncoding:NSUTF8StringEncoding]]];
}];
// now create the URL
NSURL *proURL = [NSURL URLWithString:[NSString stringWithFormat:@"testapp-pro://data?%@", [array componentsJoinedByString:@"&"]]];
// if we can open pro app, then then do so, providing "getdata" request
if ([[UIApplication sharedApplication] canOpenURL:proURL])
{
NSLog(@"Opening pro app");
[[UIApplication sharedApplication] openURL:proURL];
}
}
return YES;
}
反过来,专业版可以有自定义 URL 方案来接收来自应用的精简版的数据。因此,pro 应用程序处理来自 lite 应用程序的数据的处理程序可能如下所示:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSArray *absoluteStringComponents = [[url absoluteString] componentsSeparatedByString:@"?"];
NSArray *parameters = [absoluteStringComponents[1] componentsSeparatedByString:@"&"];
for (NSString *parameter in parameters)
{
NSArray *parameterComponents = [parameter componentsSeparatedByString:@"="];
// I'm just logging the results, but you'd presumably integrate the results into your model
NSLog(@"%@ is equal to %@", parameterComponents[0], [parameterComponents[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]);
}
return YES;
}
这适用于少量的基本数据,但我不认为它非常适合交换图像文件。如果有兴趣,我可以给你举个例子。
1234563专业版应用中的数据文件(看起来不优雅)。