【问题标题】:How to pass the dictionary to other iOS app using openURL:options?如何使用 openURL:options 将字典传递给其他 iOS 应用程序?
【发布时间】:2019-01-17 21:20:23
【问题描述】:

我正在使用以下行从其他 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";
}

【问题讨论】:

    标签: ios ios9 ios10 openurl


    【解决方案1】:

    将所需的数据作为参数传递。

    //用于调用

    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;
    }
    

    【讨论】:

    • 是的。我们可以使用这种方式来传递数据。但我的问题是,如何将数据作为字典传递(在选项中)而不在 url 中附加值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    相关资源
    最近更新 更多