[英]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中添加共享扩展名是最简单的部分,但是我很难
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?
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.