Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS YouTube共享扩展未提供URL_Ios_Swift_Youtube_Ios8 Share Extension - Fatal编程技术网

iOS YouTube共享扩展未提供URL

iOS YouTube共享扩展未提供URL,ios,swift,youtube,ios8-share-extension,Ios,Swift,Youtube,Ios8 Share Extension,我的共享扩展中有以下代码来获取共享的URL if let item = extensionContext?.inputItems.first as? NSExtensionItem { for (index, _) in (item.attachments?.enumerated())! { if let itemProvider = item.attachments?[index] as? NSItemProvider {

我的共享扩展中有以下代码来获取共享的URL

    if let item = extensionContext?.inputItems.first as? NSExtensionItem {
        for (index, _) in (item.attachments?.enumerated())! {
            if let itemProvider = item.attachments?[index] as? NSItemProvider {
                if itemProvider.hasItemConformingToTypeIdentifier("public.url") {
                    itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil, completionHandler: { (url, error) -> Void in
                        if let shareURL = url as? NSURL {
                            // send url to server to share the link
                            print (shareURL.absoluteString!)
出于某种原因,iOS YouTube应用程序为
itemProvider.hasItemConformingToTypeIdentifier(“public.url”)
返回false

下面是共享扩展的Info.plist

<key>NSExtension</key>
<dict>
    <key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionActivationRule</key>
        <string>
            SUBQUERY (
                extensionItems,
                $extensionItem,
                SUBQUERY (
                    $extensionItem.attachments,
                    $attachment,
                    ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url"
                ).@count == 1
            ).@count == 1
        </string>
    </dict>
    <key>NSExtensionMainStoryboard</key>
    <string>MainInterface</string>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.share-services</string>
</dict>
NSExtension
N扩展属性
NSExtensionActivationRule
子查询(
伸展岩,
多边主义,
子查询(
$m.attachments,
$附件,
任何$attachment.registeredTypeIdentifiers UTI-Compliance-TO“public.url”
)@count==1
)@count==1
NSextensionMainstryBoard
主界面
NSExtensionPointIdentifier
com.apple.share-services

我如何获取共享扩展中共享的YouTube视频的URL?

我的第一个建议是找出
itemProvider

if let item = extensionContext?.inputItems.first as? NSExtensionItem {
    for (index, _) in (item.attachments?.enumerated())! {
        if let itemProvider = item.attachments?[index] as? NSItemProvider {

            // print out the registered type identifiers so we can see what's there
            itemProvider.registeredTypeIdentifiers.forEach { print String(describing: $0) }
在这种情况下,根据您的评论,您可以获得纯文本或
kuttypeplantext
,并且该文本包含一个URL,因此:

if let item = extensionContext?.inputItems.first as? NSExtensionItem {
for (index, _) in (item.attachments?.enumerated())! {
    if let itemProvider = item.attachments?[index] as? NSItemProvider {
        if itemProvider.hasItemConformingToTypeIdentifier(kUTTypePlainText) {
            itemProvider.loadItem(forTypeIdentifier: kUTTypePlainText, options: nil, completionHandler: { (string, error) -> Void in
                if let string = (string as? String), let shareURL = URL(string) {
                    // send url to server to share the link
                    print (shareURL.absoluteString!)

我的第二个建议是始终使用kUTType常量而不是原始字符串:-)

Youtube将提供kUTTypePlainText。 要获取URL,必须将NSSecureCoding转换为字符串,然后使用该字符串构建URL

func getUrl(){
如果让content=extensionContext!.inputItems[0]作为?NSExtensionItem{
让contentTypePlain=kUTTypePlainText作为字符串
如果let contents=content.attachments{
附件的内容{
如果附件.hasItemConformingToTypeIdentifier(contentTypePlain){
loadItem(forTypeIdentifier:contentTypePlain,选项:nil,completionHandler:{data,错误在
设urlString=数据为!String
让url=url(string:urlString)!
//使用URL
})
}
}
}
}
} 

如果
ItemProvider
无法为您提供URL,它可以提供什么类型的URL?如果您向项目提供程序询问它的
registeredTypeIdentifiers
提供了什么UTI?
p itemProvider。registeredTypeIdentifiers[0]为!String(String)$R4=“public.plain text”
@scotthompson打印
itemProvider。将类型标识符[0]注册为!字符串
返回“public.plain text”确定。这是第一个。还有其他的吗?如果你真的要纯文本,它包含什么?@scotthompson
item.attachments?.count
返回
1
。正在查看它现在包含的内容。
参数标签“(:)”与
上的任何可用重载都不匹配,如果让shareURL=URL(字符串){
我调整了几次代码。您可能需要
URL(字符串:字符串)
。我试图在操场上尽可能地接近,但没有完整的代码要编译,因此您可能需要不断调整。好的,将
kUTTypePlainText作为字符串,而不是
kUTTypePlainText
?UTTYPE是CFStrings…但显然您必须将其键入:-(非常感谢。经过一点编辑和使用,我让它开始工作了!