Works for Safari, Doesn't work for Chrome
Maybe the question is simple and stupid but I am new to iOS Development and I cannot find any right solution to solve this issue.
I need to get:
1) page url
2) page name
Extension
Info.plist
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExrensionActivationSupportsText</key>
<true/>
<key>NSExtensionActivationSupportsFileWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsImageWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsMovieWithMaxCount</key>
<integer>20</integer>
<key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
<integer>0</integer>
</dict>
<key>NSExtensionJavaScriptPreprocessingFile</key>
<string>DemoPreprocessor</string>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
</dict>
I've added more to Chrome but it still does not work. For Safari, this is enough:
NSExtensionActivationSupportsWebPageWithMaxCount, NSExtensionActivationSupportsWebURLWithMaxCount, NSExrensionActivationSupportsText, NSExtensionJavaScriptPreprocessingFile
I try three approaches, with and without DemoPreprocessor.js. But all don't work for Chrome:
1. ShareViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
let items = extensionContext?.inputItems
var itemProvider: NSItemProvider?
if items != nil && items!.isEmpty == false {
let item = items![0] as! NSExtensionItem
if let attachments = item.attachments {
if !attachments.isEmpty {
itemProvider = (attachments[0] as? NSItemProvider)!
}
}
}
let urlType = kUTTypePropertyList as String
if ((itemProvider?.hasItemConformingToTypeIdentifier(urlType)) != nil) {
itemProvider?.loadItemForTypeIdentifier(urlType, options: nil, completionHandler: {
(item: NSSecureCoding?, error: NSError!) -> Void in
if let resultDict = item as? NSDictionary {
self.linkName = resultDict[NSExtensionJavaScriptPreprocessingResultsKey]!["URL"] as! String
self.linkUrl = resultDict[NSExtensionJavaScriptPreprocessingResultsKey]!["title"] as! String
}
})
}
}
2 ShareViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
let items = extensionContext?.inputItems
var itemProvider: NSItemProvider?
if items != nil && items!.isEmpty == false {
let item = items![0] as! NSExtensionItem
if let attachments = item.attachments {
if !attachments.isEmpty {
itemProvider = (attachments[0] as? NSItemProvider)!
}
}
}
let urlType = kUTTypeURL as NSString as String
if ((itemProvider?.hasItemConformingToTypeIdentifier(urlType)) != nil) {
itemProvider?.loadItemForTypeIdentifier(urlType, options: nil, completionHandler: {
item, error in
self.linkName = self.contentText
self.linkUrl = "\(item!)"
})
}
}
3 ShareViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
let items = extensionContext?.inputItems
var itemProvider: NSItemProvider?
if items != nil && items!.isEmpty == false {
let item = items![0] as! NSExtensionItem
if let attachments = item.attachments {
if !attachments.isEmpty {
itemProvider = (attachments[0] as? NSItemProvider)!
}
}
}
if self.linkUrl.characters.count < 1 {
if ((itemProvider?.hasItemConformingToTypeIdentifier("public.url")) != nil) {
itemProvider?.loadItemForTypeIdentifier("public.url", options: nil, completionHandler: {
item, error in self.linkUrl = "\(item!)"
})
}
}
}
DemoPreprocessor.js
var MyPreprocessor = function() {};
MyPreprocessor.prototype = {
run: function(arguments) {
arguments.completionFunction({"URL": document.URL, "pageSource": document.documentElement.outerHTML, "title": document.title, "selection": window.getSelection().toString()});
}
};
var ExtensionPreprocessingJS = new MyPreprocessor;