【发布时间】:2016-05-25 06:12:03
【问题描述】:
我想使用 URL Scheme 在两个应用程序之间进行应用程序间通信。我已经浏览了 Apple 文档,但不会得到太多帮助。
我做了两个项目,在发件人中我添加了一种方法
{
UIApplication *ourApplication = [UIApplication sharedApplication];
NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet];
NSString *URLEncodedText = [@"TEST" stringByAddingPercentEncodingWithAllowedCharacters:set];
NSString *ourPath = [@"readtext://" stringByAppendingString:URLEncodedText];
NSURL *ourURL = [NSURL URLWithString:ourPath];
if ([ourApplication canOpenURL:ourURL]) {
[ourApplication openURL:ourURL];
}
else {
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Receiver Not Found"
message:@"The Receiver App is not installed"
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
NSLog(@"RECEIVER NOT FOUND");
}
}
在 Receiver 应用程序中,将以下代码添加到应用程序委托
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet];
NSString *text = [[url host] stringByAddingPercentEncodingWithAllowedCharacters:set];
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Title"
message:text
preferredStyle:UIAlertControllerStyleAlert];
[self.window.rootViewController presentViewController:alert animated:YES completion:nil];
return YES;
}
还有在 info.plist 和 Info 下的 URL 类型中添加什么。
【问题讨论】:
-
请添加您自己尝试过的源示例。
-
我尝试了下面的链接,对新的 ios code.tutsplus.com/tutorials/…进行了一些更改
-
将此信息添加到您的问题(通过编辑它)并显示您所做的更改。您可以轻松地将代码复制并粘贴到问题编辑器中,突出显示它并按 { } 图标将其显示为代码。
-
做了一些更改,请检查。
标签: ios iphone ios9 url-scheme