gpt4 book ai didi

ios - 使用自定义 URL Scheme 将 NSArray 传递给其他应用程序的语法

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:08:12 25 4
gpt4 key购买 nike

这几乎是一个概念验证应用程序。当我使用以下命令启动另一个应用程序时,我试图简单地传递一个 NSArray 值:

UIApplication *test = [ UIApplication sharedApplication ];
BOOL found =
[ test openURL:[ NSURL URLWithString:@"myCalculator://data" ] ];

我想用包含三个整数的 NSArray 替换“数据”。在 myCalculator 中,我在委托(delegate)中实现了这些方法:

- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {

NSLog(@"Open application called in calculator");
return [ self application:application handleOpenURL:url ] ;
}

// Depracted
- (BOOL)application:(UIApplication *)application
handleOpenURL:(NSURL *)url {
NSLog(@"%@",application.debugDescription);
return YES;
}
@end

当我从其他应用程序打开 URL 时确实调用了它。所以,我不得不提问。如何对 NSArray 进行编码并在打开自定义 URL 时将其传入?而且,我如何在上面的 appDelegate 方法中解码它?

任何帮助将不胜感激。我在文档中找不到任何关于如何对 NSArray 进行编码以传递到自定义 URL Scheme 或在接收端对其进行解码的内容。

另外,我是初学者,所以越详细越好。

最佳答案

一个选项是构建一个查询字符串:

NSMutableString *query = [NSMutableString string];
for (id val in myArray) {
if (query.length) {
[query appendString:@"&"];
[query appendFormat:@"v=%@", val];
}

NSString *urlStr = [@"myCalculator://data?" stringByAppendingString:query];
NSURL *url = [NSURL URLWithString:urlStr];

URL 将如下所示:

myCalculator://data?v=1&v=2&v=3

其中 1、2、3 将是数组中的实际值。

handleOpenURL 方法中,您需要解析此 URL 以获取值。

NSString *query = [url query];
NSArray *parts = [query componentsSeparatedByString:@"&"];
NSMutableArray *values = [NSMutableArray array];
for (NSString *part in parts) {
NSRange equal = [part rangeOfString:@"="];
NSString *value = [part substringFromIndex:equal.location + equal.length];
[values addObject:value]; // If you want the value as a string
[values addObject:[NSNumber numberWithInt:[value intValue]]]; // If you want the value as a number
}

现在 values 数组中有您从其他应用程序传递的值。

关于ios - 使用自定义 URL Scheme 将 NSArray 传递给其他应用程序的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19322813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com