如何将我的应用程序添加到共享表操作

nah*_*hum 9 objective-c ios

我正在尝试将我的应用添加到共享表.如果你看一张照片并按下分享按钮,你会看到Facebook twitter ...

最近我从商店下载了一些应用程序,它们也出现在共享表中,所以我认为它可能在某种程度上.

可能吗?如果是这样的话

这个问题不是什么

  1. 这不仅仅是在您创建的应用之间共享数据.通常,自定义URL方案是出于此目的,但仅当您的数据源知道如何使用自定义方案时.但是这个问题是关于如何让你的应用程序为第三方应用程序(即照片)做好准备,以便与你分享标准内容(照片,电影等).

  2. 这不是关于如何准备您的内容以分享在Facebook或Twitter上.相反,它询问如何编写自己类似Facebook的应用程序,以便它可以接受共享

Sar*_*ith 12

更新了XCode 10,swift 4.2

是的,这是可能的.


创建扩展

在你的XCode中转到File -> New -> Target然后选择Share Extension.

将在项目目录中创建一个文件夹和扩展名为.appex的文件.

文件夹将包含

Extension Name  --------- ShareViewController.swift
               |
                -------- Maininterface.storyboard 
               |
                -------- Info.plist
Run Code Online (Sandbox Code Playgroud)

添加对项目类型的支持

在此plist中提供您的应用可以共享的适当扩展*.

<key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionActivationRule</key>
        <dict>
            <key>NSExtensionActivationSupportsImageWithMaxCount</key>
            <integer>10</integer>
            <key>NSExtensionActivationSupportsMovieWithMaxCount</key>
            <integer>1</integer>
            <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
            <integer>1</integer>
        </dict>
    </dict>
Run Code Online (Sandbox Code Playgroud)

委托方法

ShareViewController文件内容

    override func isContentValid() -> Bool {
            // Do validation of contentText and/or NSExtensionContext attachments here
            return true
    }

    override func didSelectPost() {
             // This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.

            // Inform the host that we're done, so it un-blocks its UI. Note: Alternatively you could call super's -didSelectPost, which will similarly complete the extension context.
                self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
    }

    override func configurationItems() -> [Any]! {
            // To add configuration options via table cells at the bottom of the sheet, return an array of SLComposeSheetConfigurationItem here.
                return []
    }
Run Code Online (Sandbox Code Playgroud)

分享你身边的帖子

你快到了.将所选项目从disSelectPost传递到您的应用程序.您可以使用下面的外部代码

- ( void ) passSelectedItemsToApp
{
    NSExtensionItem * item = self.extensionContext.inputItems.firstObject;

    // Reset the counter and the argument list for invoking the app:
    m_invokeArgs = NULL;
    m_inputItemCount = item.attachments.count;

    // Iterate through the attached files
    for ( NSItemProvider * itemProvider in item.attachments )
    {
        // Check if we are sharing a JPEG
        if ( [ itemProvider hasItemConformingToTypeIdentifier: ( NSString * ) kUTTypeJPEG ] )
        {
            // Load it, so we can get the path to it
            [ itemProvider loadItemForTypeIdentifier: ( NSString * ) kUTTypeJPEG
                                             options: NULL
                                   completionHandler: ^ ( UIImage * image, NSError * error )
             {
                 static int itemIdx = 0;

                 if ( NULL != error )
                 {
                     NSLog( @"There was an error retrieving the attachments: %@", error );
                     return;
                 }

                 // The app won't be able to access the images by path directly in the Camera Roll folder,
                 // so we temporary copy them to a folder which both the extension and the app can access:
                 NSString * filePath = [ self saveImageToAppGroupFolder: image imageIndex: itemIdx ];

                 // Now add the path to the list of arguments we'll pass to the app:
                 [ self addImagePathToArgumentList: filePath ];

                 // If we have reached the last attachment, it's time to hand control to the app:
                 if ( ++itemIdx >= m_inputItemCount )
                 {
                     [ self invokeApp: m_invokeArgs ];
                 }
             } ];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,请确保您的.ipa文件中包含扩展程序.通过使用.zip技巧打开它.


调试您的扩展程序

如您所述使用照片应用调试您的扩展程序.您可以使用附加到处理方法.*脚步

1)打开照片应用程序

2)使用PID或进程名称将扩展附加到xcode

3)与您的应用共享图片或视频.

4)应用程序将在didSelectPost方法中命中.

*Apple文档