How to pass the dictionary to other iOS app using openURL:options?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to pass the dictionary to other iOS app using openURL:options?



I am using the below lines to transfer and open the ios app from other ios application.


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");

];



The "Test2" application has opened successfully but cannot retrieve the dictionary(options) value. Please guide me.



I have referred some other blogs, They are passing the data in URL itself. But How to pass the data in options?



In the Test2 application, I am using below code to retrieve the data,


- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
NSLog(@"URL %@",url);
NSLog(@"Option %@",options);

return YES;



It shows the only these two values in the option


Option
UIApplicationOpenURLOptionsOpenInPlaceKey = 0;
UIApplicationOpenURLOptionsSourceApplicationKey = "com.test.testApplicaton";




2 Answers
2



I hope below code will work for you.


NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"contact_name", @"kumar",@"designantion",@"sales", nil];
NSDictionary *finalDict = [[NSDictionary alloc] initWithObjectsAndKeys:dict, @"OfficeContactPerson", nil];

NSURL *url=[NSURL URLWithString:@"http://139.59.252.34:1337/office"];
NSData *postData=[[[NSString alloc]initWithData:[NSJSONSerialization dataWithJSONObject:finalDict options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding] dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postlength=[NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request= [[NSMutableURLRequest alloc]init];

[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postlength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSError *error=[[NSError alloc]init];
NSHTTPURLResponse *response=nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];


if (!urlData)


UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Error"message:[NSString stringWithFormat:@"%ld",(long)(error.code)]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];


else

NSString *responseData=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response data ----->%@",responseData);
NSError *error=nil;
NSDictionary *jsonData=[NSJSONSerialization JSONObjectWithData:urlData options:NSJSONReadingMutableContainers error:&error];





pass what ever the needed data as parameters.



//For Calling


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");
];



//For receiving


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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard