在IOS8测试版中共享扩展 [英] Sharing Extension in IOS8 beta

查看:132
本文介绍了在IOS8测试版中共享扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用新的iOS 8应用扩展程序创建共享扩展程序。我试图获取Safari网站的当前URL以在UILabel中显示它。很简单。

I'm trying to create a sharing extension using the new iOS 8 app extensions. I tried to get the current URL of a Safari site to show it in a UILabel. Simple enough.

我正在通过Apple的官方扩展指南工作 https://developer.apple.com/library/content/documentation/General/Conceptual /ExtensibilityPG/Share.html#//apple_ref/doc/uid/TP40014214-CH12-SW1 但有些事情没有按预期工作。我知道它只是在测试版中,但也许我只是做错了。

I was working trough the official extension guide from apple here https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/Share.html#//apple_ref/doc/uid/TP40014214-CH12-SW1 but some things are not working as expected. I know it is only in beta but maybe I'm just doing something wrong.

这是我在扩展名ViewController中从safari获取URL的代码:

Here is my code to get the URL from safari inside the extensions ViewController:

-(void)viewDidAppear:(BOOL)animated{
NSExtensionContext *myExtensionContext = [self extensionContext];
NSArray *inputItems = [myExtensionContext inputItems];
NSMutableString* mutableString = [[NSMutableString alloc]init];
for(NSExtensionItem* item in inputItems){
    NSMutableString* temp = [NSMutableString stringWithFormat:@"%@, %@, %lu, 
           %lu - ",item.attributedTitle,[item.attributedContentText string],
           (unsigned long)[item.userInfo count],[item.attachments count]];

    for(NSString* key in [item.userInfo allKeys]){
        NSArray* array = [item.userInfo objectForKey:@"NSExtensionItemAttachmentsKey"];
        [temp appendString:[NSString stringWithFormat:@" in array:%lu@",[array count]]];   
    }
    [mutableString appendString:temp];
}
self.myLabel.text = mutableString;
}

这是我的扩展程序的Info.plist文件的内容:

And this is the content of my Info.plist file of my Extension:

<dict>
    <key>NSExtensionMainStoryboard</key>
    <string>MainInterface</string>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.share-services</string>
    <key>NSExtensionActivationRule</key>
    <dict>
        <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
        <integer>200</integer>
    </dict>
</dict>

当我访问Safari中的苹果iPod支持页面并尝试将其分享到我的扩展程序时,我得到以下值但没有网址:

When I visit apples iPod support page in Safari and try to share it to my extension, I get following values but no URL:

item.attributedTitle = (null)
item.attributedContentText = "iPod - Apple Support"
item.userInfo.count = 2 (two keys: NSExtensionAttributedContentTextKey and
    NSExtensionItemAttachmentsKey)
item.attachments.count = 0

字典对象内的数组总是空的。

The arrays inside the objects of the dictionary are always empty.

当我用系统邮件分享苹果网站时app将URL发布到消息中。那么为什么我的扩展中没有URL?

When I share the apple site with the system mail app the URL is posted to the message. So why is there no URL in my extension?

推荐答案

以下是获取网址的方法。请注意,类型标识符是kUTTypeURL,块参数是NSURL。此外,plist也需要像我的一样正确。缺少文档,并获得了 Apple dev论坛上的number4 的帮助。 (您需要注册并登录才能看到它)。

Below is how you can get the url. Notice the type identifier is kUTTypeURL and the block argument is NSURL. Also, the plist needs to be correct like mine also. The documentation was lacking and got help from number4 on the Apple dev forums. (you'll need to be registered and logged in to see it).

代码:

NSExtensionItem *item = self.extensionContext.inputItems.firstObject;
NSItemProvider *itemProvider = item.attachments.firstObject;
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL]) {
    [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeURL options:nil completionHandler:^(NSURL *url, NSError *error) {
        self.urlString = url.absoluteString;
    }];
}

Info.plist

Info.plist

<key>NSExtension</key>
<dict>
    <key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionActivationRule</key>
        <dict>
            <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
            <integer>1</integer>
        </dict>
        <key>NSExtensionPointName</key>
        <string>com.apple.share-services</string>
        <key>NSExtensionPointVersion</key>
        <string>1.0</string>
    </dict>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.share-services</string>
    <key>NSExtensionMainStoryboard</key>
    <string>MainInterface</string>
</dict>

这篇关于在IOS8测试版中共享扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆