【问题标题】:how to transfer text or pdf file with URL Scheme如何使用 URL Scheme 传输文本或 pdf 文件
【发布时间】: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


【解决方案1】:

您在 .plist 文件中添加代码:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>Scheme_Name</string>
        </array>
    </dict>
</array>

编写代码调用事件:

// use url

[NSURL URLWithString:@"Scheme_Name://"] 

仅见第 1 项:

我也在一个项目中实现了这个代码,它正在使用今天的小部件。 注意:步骤替换图像中的 Scheme_Name。

【讨论】:

  • 你能分享一下 plist 的示例屏幕截图吗?我在 plist 中做了一些事情,但它不起作用。
  • 我正在做同样的事情,但没有运气
猜你喜欢
  • 2013-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多