我正在使用以下行从其他ios应用程序传输和打开ios应用程序。
NSURL *url = [[NSURL alloc]initWithString:@"test2://"];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc]init];
[tempDict setObject:@"companyURL" forKey:@"company"];
[tempDict setObject:@"https://companyURL" forKey:@"cburl"];
[tempDict setObject:@"AccessToken" forKey:@"AccessToken"];
NSDictionary *options = @{UIApplicationOpenURLOptionsAnnotationKey : tempDict};
[[UIApplication sharedApplication] openURL:url options:options completionHandler:^(BOOL success) {
if (success) {
NSLog(@"Opened url");
}}
];
"Test2"应用程序已成功打开,但无法检索字典(选项(值。请指导我。
我已经参考了其他一些博客,他们正在URL本身中传递数据。但是如何在选项中传递数据?
在 Test2 应用程序中,我使用以下代码来检索数据,
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
NSLog(@"URL %@",url);
NSLog(@"Option %@",options);
return YES;
}
它只显示选项中的这两个值
Option {
UIApplicationOpenURLOptionsOpenInPlaceKey = 0;
UIApplicationOpenURLOptionsSourceApplicationKey = "com.test.testApplicaton";
}
将所需的数据作为参数传递。
通话用
NSString *urlS = @"customUrlScheme://name=shehan&age=27";
NSURL *url = [[NSURL alloc]initWithString:urlS];
UIApplication *applicaton = [UIApplication sharedApplication];
[applicaton openURL:url options:options completionHandler:^(BOOL success) {
NSLog(@"Success");
}];
接收
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
NSString *vc = [url description];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:6];
NSArray *pairs = [[url debugDescription] componentsSeparatedByString:@"://"];
pairs = [pairs[1] componentsSeparatedByString:@"&"];
for (NSString *pair in pairs) {
NSArray *elements = [pair componentsSeparatedByString:@"="];
NSString *key = [[elements objectAtIndex:0] stringByRemovingPercentEncoding];
NSString *val = [[elements objectAtIndex:1] stringByRemovingPercentEncoding];
[dict setObject:val forKey:key];
}
NSString *name = [dict valueForKey:@"name"];
NSString *age = [dict valueForKey:@"age"];
NSLog(name);
NSLog(age);
return true;
}