简体   繁体   English

App Extension iOS9:如何从共享视图向我的应用程序发送字符串/ URL?

[英]App Extension iOS9: How can I send a string / URL from the share view to my the app?

Unfortunately, there's currently a lack of sample code oder proper information on App Extension / Share Sheets on iOS 9. (Even Apples Dev Support is very disappointing in this regard. Just today I got the answer "We don't have an action extension sample either unfortunately". And they won't send me. 不幸的是,当前在iOS 9上的App Extension / Share Sheets上缺少示例代码或适当的信息。(即使Apples Dev Support在这方面也很令人失望。今天,我得到了答案:“我们没有动作扩展示例要么不幸”。他们不会送我。

Anyway, maybe someone is willing to share his knowledge on this topic. 无论如何,也许有人愿意分享他在这个话题上的知识。 So, adding a Share Extension in xcode is the easy part, but I struggle with 因此,在xcode中添加共享扩展名是最简单的部分,但是我很难

  1. sending and receiving strings: I want the Share Sheet to open in Safari with the current URL (maybe even with the Character Counter like in the WWDC 2015 Video "App Extension Best Practices") and send it to the app by tapping "Post". 发送和接收字符串:我希望使用当前URL在Safari中打开共享表(甚至可以使用字符计数器,例如WWDC 2015视频“应用程序扩展最佳实践”中的字符计数器),然后点击“发布”将其发送到应用程序。 Is it really necessary to open an NSURLSession? 是否真的有必要打开NSURLSession?

  2. adding an Share-icon under / General / "App Icons and Launch Images" / App Icons Source is not possible. 无法在“常规” /“应用程序图标和启动图像” /“应用程序图标源”下添加共享图标。 Some Sources suggest to use Asset Catalog - but before I do, is there no other way to simply add ONE image? 一些消息来源建议使用资产目录-但是在我这样做之前,没有其他方法可以简单地添加一个图像吗?

Other Sources I found include: 我发现的其他来源包括:

Today Extension: raywenderlich.com/83809/ios-8-today-extension-tutorial 今日扩展:raywenderlich.com/83809/ios-8-today-extension-tutorial

How to Build a Simple Action Extension: http://code.tutsplus.com/tutorials/ios-8-how-to-build-a-simple-action-extension--cms-22794 如何构建简单操作扩展: http : //code.tutsplus.com/tutorials/ios-8-how-to-build-a-simple-action-extension--cms-22794

Basic Share Extensions with Data Sharing on iOS 8: http://www.andypierz.com/blog/2014/9/19/basic-share-extensions-with-data-sharing-on-ios-8 iOS 8上带有数据共享的基本共享扩展: http : //www.andypierz.com/blog/2014/9/19/basic-share-extensions-with-data-sharing-on-ios-8

What I was able to accomplish so far is to add a title to the Share Sheet on shareViewController.m: 到目前为止,我能完成的工作是在shareViewController.m的共享表中添加标题:

- (void)loadView
{
    [super loadView];
    self.title = @"Title of the Share Sheet";
}

I want the Share Sheet to open in Safari with the current URL (maybe even with the Character Counter like in the WWDC 2015 Video "App Extension Best Practices") 我希望使用当前URL在Safari中打开共享表(甚至可以使用字符计数器,例如WWDC 2015视频“应用程序扩展最佳实践”中的字符计数器)

It is not up to you what app the user uses your share extension in, and it is not up to you what data that app supplies to the share extension. 用户使用共享扩展名的应用程序不由您决定,应用程序提供给共享扩展名的数据也不取决于您。 The app will share what the app wants to share. 该应用程序将共享该应用程序要共享的内容。 All you can do is define what kind of data you are willing to accept. 所能做的就是定义您愿意接受的数据类型。

and send it to the app by tapping "Post" 并点击“发布”将其发送到应用

When the user taps "Post", your didSelectPost override is called. 当用户点击“发布”时,将调用您的didSelectPost覆盖。 What you do is up to you. 您的工作取决于您。 What do you mean by "the app" here? 您在这里所说的“应用程序”是什么意思? Do you mean, your app that provides the share extension? 您的意思是您的应用程序提供了共享扩展名吗? Then devise a custom URL scheme for communication from the extension to the app, or use one of the other data sharing methods described in the docs: 然后设计一个自定义URL方案,用于从扩展名到应用程序的通信,或使用docs中描述的其他数据共享方法之一:

https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html

adding an Share-icon under / General / "App Icons and Launch Images" / App Icons Source is not possible. 无法在“常规” /“应用程序图标和启动图像” /“应用程序图标源”下添加共享图标。 Some Sources suggest to use Asset Catalog - but before I do, is there no other way to simply add ONE image? 一些消息来源建议使用资产目录-但是在我这样做之前,没有其他方法可以简单地添加一个图像吗?

The icon for a share extension is automatically the icon of the app that supplies the share extension. 共享扩展名的图标自动是提供共享扩展名的应用程序的图标。

to extract the URL, you will have to enable the ExtensionPreprocessingJS part first 要提取URL,必须首先启用ExtensionPreprocessingJS部分

the following is slightly modified from AppCode iOS8 action extension tutorial 以下是从AppCode iOS8操作扩展教程中进行的略微修改

Add a new file GetURL.js to your share extension folder 将新文件GetURL.js添加到共享扩展文件夹中

var GetURL = function() {};

GetURL.prototype = {

   run: function(arguments) {
       arguments.completionFunction({ "currentUrl" : document.URL });
   }    
};

var ExtensionPreprocessingJS = new GetURL;

Next, you will have to edit your Info.plist so that the host app will call the JS. 接下来,您将必须编辑Info.plist,以便主机应用程序将调用JS。 Refer to AppCode tutorial on how to do so. 请参阅AppCode教程以了解操作方法。 在此处输入图片说明

Then include the following function in your ShareViewController viewDidLoad 然后在您的ShareViewController viewDidLoad中包含以下函数

    //swift code
    let extensionItem = extensionContext?.inputItems.first as! NSExtensionItem
    let itemProvider = extensionItem.attachments?.first as! NSItemProvider

    let propertyList = String(kUTTypePropertyList)
    if itemProvider.hasItemConformingToTypeIdentifier(propertyList) {
        itemProvider.loadItemForTypeIdentifier(propertyList, options: nil, completionHandler: { (item, error) -> Void in
            let dictionary = item as! NSDictionary
            NSOperationQueue.mainQueue().addOperationWithBlock {
                let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as! NSDictionary
                let url = NSURL(string: (results["currentURL"] as! String))                                        
                //now you can do what you like with this url
            }
        })
    } else {
        print("error")
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 url 从浏览器共享到 iOS 中的应用程序? - How can I share a url from a browser to my app in iOS? 如何与我的主应用程序和 iOS 应用程序的应用程序扩展正确共享 Amplify 框架? - How can I properly share the Amplify framework with my Main App and my App Extension for an iOS app? 如何通过iOS应用程序通过电子邮件和消息共享字符串? - How can I share a string through email and imessage from my iOS app? 如何使用应用程序扩展中的 IPC 将信息异步发送到 iOS 上的包含应用程序? - How can I use IPC from an app extension to asynchronously send information to the containing app on iOS? 如何与我的Cordova / PhoneGap应用(ios)共享Safari URL? - How can I share Safari URL to my Cordova/PhoneGap App (ios)? 当我尝试从React Native iOS应用程序共享我的应用程序URL时,应用程序变得无响应 - App becomes unresponsive when I tried to share my app url from react native iOS app 如何允许用户从iOS相册应用中选择照片并在应用中打开? 而且我不想使用SHARE EXTENSION - How can I allow user to select photo from iOS Photos app and open in my application? And I don't want to use SHARE EXTENSION 当我想运行自己的应用程序iOS9时无法在iPhone上验证应用程序 - Can't verify app on iPhone when I want to run my own app iOS9 iOS共享扩展程序:如何从主机应用程序一起共享URL和文本 - iOS Sharing Extension : How to share both URL and Text together from Host app iOS-如何使用FBGraph API从我的应用程序“赞/共享”特定页面的任何照片? - iOS - How can I “Like/Share” any Photos of particular Page from my App with FBGraph API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM