在iOS8测试版中共享扩展

前端之家收集整理的这篇文章主要介绍了在iOS8测试版中共享扩展前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用新的iOS 8应用扩展程序创建一个共享扩展.我试图获取Safari站点的当前URL在UILabel中显示.足够简单

我正在从苹果官方推荐指南https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/ExtensibilityPG/ShareSheet.html#//apple_ref/doc/uid/TP40014214-CH12-SW1工作,但有些事情没有按预期工作.我知道这只是在测试版,但也许我只是做错了事情.

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

-(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文件内容

<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支持页面并尝试将其分享到我的扩展名时,我获得以下值,但没有URL:

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

字典对象内的数组始终为空.

当我与系统邮件应用程序共享苹果网站时,URL会发布到邮件中.那么为什么我的扩展程序中没有URL?

解决方法

以下是如何获取网址.注意类型标识符是kUTTypeURL,块参数是NSURL.此外,plist也需要像我一样正确.该文档缺乏,并从 number4 on the Apple dev forums获得帮助(您需要注册登录才能看到它).

码:

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

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

猜你在找的iOS相关文章