How to share a Note including Markup with Share Extensions Swift - swift

I've already figured out how to share the general content of a Note (text and pictures). But the way I currently approach the problem the markup of the note is not kept (Titles, Lists, etc.). I just receive the pure text of the note. When you share a note with Mail for instance, you can see that the markup is transferred. Is there a way to do that for your own apps?
My current solution where I only receive the pure text:
class ShareViewController: UIViewController{
override func viewDidLoad() {
if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
// Verify the provider is valid
if let contents = content.attachments as? [NSItemProvider] {
// look for images
for attachment in contents {
print(attachment.registeredTypeIdentifiers)
if attachment.hasItemConformingToTypeIdentifier("public.plain-text"){
attachment.loadItem(forTypeIdentifier: "public.plain-text", options: nil) { data, error in
let string = data as! String
print(string)
}
}
}
}
}
}
}
EDIT:
My current NSExtensionActivationRules:
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationDictionaryVersion</key>
<integer>2</integer>
<key>NSExtensionActivationSupportsImageWithMaxCount</key>
<integer>100</integer>
<key>NSExtensionActivationSupportsText</key>
<true/>
</dict>
</dict>

Use the attributedContentText property of your NSExtensionItem (content):
override func viewDidLoad() {
if let content = extensionContext!.inputItems[0] as? NSExtensionItem {
// move your content validation to `func isContentValid() -> Bool`
let attributedString = content.attributedContentText // yay NSAttributedString!
}
}

Related

In iOS, how can I get the contact shared from "Recent" calls list to my app in Flutter?

Please excuse me if I sound stupid, I'm new to flutter.
I have started learning flutter recently and wanted to create an app where anyone can share a contact from the "Recent" calls list to my app. I'm following this blog post which allows text share from any other app to my app.
What I have done so far:
This is my plist file, added the public.vcard to allow my app to appear on the tap of "Share Contact".
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<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.vcard"
).#count >= 1
).#count > 0
</string>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
</dict>
</dict>
</plist>
Here's my ShareViewController.swift
import Social
import MobileCoreServices
class ShareViewController: SLComposeServiceViewController {
override func isContentValid() -> Bool {
// Do validation of contentText and/or NSExtensionContext attachments here
print("Something is not right")
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.
let sharedSuiteName: String = "group.com.thelogicalbeing.whatsappshare"
let sharedDataKey: String = "SharedData"
let extensionItem = extensionContext?.inputItems[0] as! NSExtensionItem
let contentTypeText = kUTTypeText as String // Note, you need to import 'MobileCoreServices' for this
for attachment in extensionItem.attachments! {
print(attachment)
if attachment.hasItemConformingToTypeIdentifier(contentTypeText) {
attachment.loadItem(forTypeIdentifier: contentTypeText, options: nil, completionHandler: {(results, error) in
if let sharedText = results as! String? {
if let userDefaults = UserDefaults(suiteName: sharedSuiteName) {
userDefaults.set(sharedText, forKey: sharedDataKey)
}
}
})
}
}
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 []
}
}
Here's my AppDelegate.swift
import UIKit
import Flutter
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
let sharedSuiteName: String = "group.com.thelogicalbeing.whatsappshare"
let sharedDataKey: String = "SharedData"
let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
let methodChannel = FlutterMethodChannel(name: "com.thelogicalbeing.whatsappshare", binaryMessenger: controller.binaryMessenger)
methodChannel.setMethodCallHandler({
(call: FlutterMethodCall, result: #escaping FlutterResult) -> Void in
if call.method == "getSharedData" {
if let prefs = UserDefaults(suiteName: sharedSuiteName) {
if let sharedText = prefs.string(forKey: sharedDataKey) {
result(sharedText);
}
// clear out the cached data
prefs.set("", forKey: sharedDataKey);
}
}
})
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
What I'm trying to achieve is that I need to receive the phone number and display it in my app.
Don't know how to proceed. Any help will be appreciated.
1- Apple does not allow fetching call logs on iOS!
You can fetch all contacts with all their information. But not the calls log.
2- On Android you can use the pub.dev dependency plugin call_log to do that.
Have a look at this package called receive_sharing_intent, it allows you to to receive sharing photos, videos, text, urls or any other file types from another app. And it also supports iOS Share extension and launching the host app automatically.

Failing to make request to server using alamofire and DisabledTrustEvaluator

I am trying to get some data off a server that created I using asp.net core, however; when trying to get the data in my Swift iOS code it keeps on failing without any error showing. I have done all the TSL checks in my info.plist.
model.swift
import Foundation
struct User: Decodable {
let name:String
let userEmail: String
let firebaseId: String
let userMode: Int
let profileImg: String
let loginType: Int?
}
userController.swift
import UIKit
import ProgressHUD
class LoginViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
removeNavBar()
// UserApiService.shared.makeGetAPICall(user:{users, error in
// // ProgressHUD.dismiss()
// guard let users = users else {return}
// print(users)
// guard let errors = error else {return}
// print(errors)
// })
}
#IBAction func googleLogin(_ sender: Any) {
ProgressHUD.animationType = .circleSpinFade
ProgressHUD.show()
UserApiService.init().makeGetAPICall(user: {users, error in
ProgressHUD.dismiss()
guard let users = users else {return}
print(users)
guard let errors = error else {return}
print(errors)
})
}
private func removeNavBar(){
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
apiService.swift
import Foundation
import Alamofire
class UserApiService{
static let shared = UserApiService.init()
let session: Session = {
let manager = ServerTrustManager(allHostsMustBeEvaluated: false,evaluators: ["localhost:5001": DisabledTrustEvaluator()])
let configuration = URLSessionConfiguration.af.default
return Session(configuration: configuration, serverTrustManager: manager)
}()
//MARK:- GET
func makeGetAPICall(user completionHandler: #escaping ([User]?,Error?) -> Void) {
var urlComponent = URLComponents()
urlComponent.scheme = "https"
urlComponent.host = "localhost"
urlComponent.port = 5001
urlComponent.path = "/api/user"
print(urlComponent.url!)
guard let url = urlComponent.url else {
return
}
session.request(url, method: .get).validate().responseDecodable(of: [User].self) {(response) in
switch response.result{
case .success:
guard let users = response.value else {
return
}
print(users)
completionHandler(users, nil)
case .failure(let error):
completionHandler(nil, error)
}
}
}
}
info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>localhost</key>
<dict>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
<key>NSAllowsArbitraryLoadsForMedia</key>
<true/>
<key>NSAllowsLocalNetworking</key>
<true/>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
I debugged it with breakpoints, and I am getting <unavailable; try printing with "VO" or "PO">. I'm having trouble understanding what that means and how I should resolve this.
Also, I have changed my info.plist and it looks like this, however; I am still getting the same issue
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoadsForMedia</key>
<true/>
<key>NSAllowsLocalNetworking</key>
<true/>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
So I managed to fix my issue, Hooray!!!
What I did was, I changed evaluators: ["localhost:5001": DisabledTrustEvaluator() to evaluators: ["localhost": DisabledTrustEvaluator()
Also, when calling my method, I was referencing normally from a class but then after, I had to call it as a singleton, and then, HEY PRESTO, IT WORKED!
#IBAction func googleLogin(_ sender: Any) {
ProgressHUD.animationType = .circleSpinFade
ProgressHUD.show()
UserApiService.shared.makeGetAPICall(user: {users, error in
ProgressHUD.dismiss()
guard let users = users else {return}
print(users)
guard let errors = error else {return}
print(errors)
})
}
This link helped me figure out part of the reason for my issues https://github.com/Alamofire/Alamofire/issues/3336

How to read the string in an attached file, not sharing the string directly, using Share Extension from other APP to my APP in Swift?

In the following code I can read text in this way and print the text in the code.
But I do not know how to share txt file as an attachment and read it out in the following code.
import UIKit
import Social
import CoreServices
class ShareViewController: SLComposeServiceViewController {
private var textString: String?
override func isContentValid() -> Bool {
// Do validation of contentText and/or NSExtensionContext attachments here
if textString != nil {
if !contentText.isEmpty {
return true
}
}
return true
}
override func viewDidLoad() {
super.viewDidLoad()
let extensionItem = extensionContext?.inputItems[0] as! NSExtensionItem
let contentTypeText = kUTTypeText as String
for attachment in extensionItem.attachments! {
if attachment.isText {
attachment.loadItem(forTypeIdentifier: contentTypeText, options: nil, completionHandler: { (results, error) in
let text = results as! String
self.textString = text
_ = self.isContentValid()
})
}
}
}
override func didSelectPost() {
// This is called after the user selects Post. Do the upload of contentText and/or NSExtensionContext attachments.
print(String(textString!)) // <-- I cannot read txt file and print it
// 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 []
}
}
//MARK: NSItemProvider check
extension NSItemProvider {
var isText: Bool {
return hasItemConformingToTypeIdentifier(kUTTypeText as String)
}
}
Is there any method to share the txt file as an attachment in Share Extension?
Thanks for any help.
First of all, in info.plist of your share extension you have to set what type of attachments and how many your extension can get
Example:
<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>
All keys you can find here
When your extension recives attachment first you need to check it for type(is it what you are expecting to recive).
And then, in didSelectPost() you can get that attachment and do something with it (maybe store it in your app if that's what you want).
Here is nice article about shere extension for photos

iOS Swift: How to access selected text in WKWebView

I would like to be able to use a menu button to copy selected text from a web page in WKWebView to the pasteboard. I would like to get the text from the pasteboard into a text view in a second view controller. How do I access and copy the selected text in the WKWebView?
Swift 4
You can access the general pasteboard with the following line:
let generalPasteboard = UIPasteboard.general
In the view controller, you can add an observer to observe when something is copied to the pasteboard.
override func viewDidLoad() {
super.viewDidLoad()
// https://stackoverflow.com/questions/35711080/how-can-i-edit-the-text-copied-into-uipasteboard
NotificationCenter.default.addObserver(self, selector: #selector(pasteboardChanged(_:)), name: UIPasteboard.changedNotification, object: generalPasteboard)
}
override func viewDidDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(UIPasteboard.changedNotification)
super.viewDidDisappear(animated)
}
#objc
func pasteboardChanged(_ notification: Notification) {
print("Pasteboard has been changed")
if let data = generalPasteboard.data(forPasteboardType: kUTTypeHTML as String) {
let dataStr = String(data: data, encoding: .ascii)!
print("data str = \(dataStr)")
}
}
In the above pasteboardChanged function, I get the data as HTML in order to display the copied as formatted text in a second controller in a WKWebView. You must import MobileCoreServices in order to reference the UTI kUTTypeHTML. To see other UTI's, please see the following link: Apple Developer - UTI Text Types
import MobileCoreServices
In your original question, you mentioned you want to put the copied content into a second textview. If you want to keep the formatting, you will need to get the copied data as RTFD then convert it to an attributed string. Then set the textview to display the attributed string.
let rtfdStringType = "com.apple.flat-rtfd"
// Get the last copied data in the pasteboard as RTFD
if let data = pasteboard.data(forPasteboardType: rtfdStringType) {
do {
print("rtfd data str = \(String(data: data, encoding: .ascii) ?? "")")
// Convert rtfd data to attributedString
let attStr = try NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.rtfd], documentAttributes: nil)
// Insert it into textview
print("attr str = \(attStr)")
copiedTextView.attributedText = attStr
}
catch {
print("Couldn't convert pasted rtfd")
}
}
Because I don't know your exact project or use case so you may need to alter the code a little but I hope I provided you with pieces you need for project. Please comment if there's anything I missed.

iOS 8 Share extension loadItemForTypeIdentifier:options:completionHandler: completion closure not executing

I'm using the
loadItemForTypeIdentifier:options:completionHandler: method on an NSItemProvider object to extract a url from Safari via a Share extension in iOS 8.
In Objective-C, this code and works and the block runs.
[itemProvider loadItemForTypeIdentifier:(#"public.url" options:nil completionHandler:^(NSURL *url, NSError *error) {
//My code
}];
In Swift, it looks very similar, however the closure doesn't run. Also, itemProvider.hasItemConformingToTypeIdentifier("public.url") returns YES so there must be a valid object to parse the url from inside the itemProvider.
itemProvider.loadItemForTypeIdentifier("public.url", options: nil, completionHandler: { (urlItem, error) in
//My code
})
The Info.plist NSExtension portion is exactly the same for both Objective-C and Swift version and looks like this:
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
<integer>1</integer>
</dict>
<key>NSExtensionPointName</key>
<string>com.apple.share-services</string>
<key>NSExtensionPointVersion</key>
<string>1.0</string>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
</dict>
What am I doing wrong?
call
self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
at the end of completionHandler instead of calling it at the end of didSelectPost()
Since completeRequestReturningItems must be called after all completionHandlers are called back, below is what I do.
let group = dispatch_group_create()
for item: AnyObject in self.extensionContext!.inputItems {
let inputItem = item as! NSExtensionItem
for provider: AnyObject in inputItem.attachments! {
let itemProvider = provider as! NSItemProvider
if itemProvider.hasItemConformingToTypeIdentifier("public.url") {
dispatch_group_enter(group)
itemProvider.loadItemForTypeIdentifier("public.url", options: nil, completionHandler: {
(result: NSSecureCoding!, error: NSError!) -> Void in
//...
dispatch_group_leave(group)
});
}
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeImage as String) {
dispatch_group_enter(group)
itemProvider.loadItemForTypeIdentifier(kUTTypeImage as String, options: nil, completionHandler: { (result, error) -> Void in
if let resultURL = result as? NSURL {
if let image = UIImage(data: NSData(contentsOfURL: resultURL)!) {
// ...
}
}
dispatch_group_leave(group)
});
}
}
}
dispatch_group_notify(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
self.extensionContext!.completeRequestReturningItems([], completionHandler: nil)
})
I take no credit for this, but have a look at how this guy did it: https://github.com/oguzbilgener/SendToInstapaper/blob/master/ShareExtension/SendingViewController.swift
I was never managed completionHandler to work properly for Share extension with no user interface (in such case extension's class is a subclass on NSObject).
Despite the [itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL] returns YES the completionHandler is never called both on the device or simulator.
After trying different approaches I ended up with workaround based on javascript passing URL back to extension (sorry as I use ObjC not Swift for my example).
Info.plist NSExtension portion:
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
<integer>1</integer>
</dict>
<key>NSExtensionJavaScriptPreprocessingFile</key>
<string>Action</string>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.services</string>
<key>NSExtensionPrincipalClass</key>
<string>ActionRequestHandler</string>
</dict>
Javascript Action.js file:
var Action = function() {};
Action.prototype = {
run: function(arguments) {
arguments.completionFunction({ "currentURL" : window.location.href })
},
finalize: function(arguments) {
}
};
var ExtensionPreprocessingJS = new Action
ActionRequestHandler.h header file:
#interface ActionRequestHandler : NSObject <NSExtensionRequestHandling>
#end
ActionRequestHandler.m based on default Action extension template:
#import "ActionRequestHandler.h"
#import <MobileCoreServices/MobileCoreServices.h>
#interface ActionRequestHandler ()
#property (nonatomic, strong) NSExtensionContext *extensionContext;
#end
#implementation ActionRequestHandler
- (void)beginRequestWithExtensionContext:(NSExtensionContext *)context {
// Do not call super in an Action extension with no user interface
self.extensionContext = context;
BOOL found = NO;
// Find the item containing the results from the JavaScript preprocessing.
for (NSExtensionItem *item in self.extensionContext.inputItems) {
for (NSItemProvider *itemProvider in item.attachments) {
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypePropertyList]) {
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypePropertyList options:nil completionHandler:^(NSDictionary *dictionary, NSError *error) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self itemLoadCompletedWithPreprocessingResults:dictionary[NSExtensionJavaScriptPreprocessingResultsKey]];
}];
}];
found = YES;
}
break;
}
if (found) {
break;
}
}
if (!found) {
// We did not find anything - signal that we're done
[self.extensionContext completeRequestReturningItems:#[] completionHandler:nil];
// Don't hold on to this after we finished with it
self.extensionContext = nil;
}
}
- (void)itemLoadCompletedWithPreprocessingResults:(NSDictionary *)javaScriptPreprocessingResults
{
// Get the URL
if ([javaScriptPreprocessingResults[#"currentURL"] length] != 0) {
NSLog(#"*** URL: %#", javaScriptPreprocessingResults[#"currentURL"]);
}
// Signal that we're done
[self.extensionContext completeRequestReturningItems:#[] completionHandler:nil];
// Don't hold on to this after we finished with it
self.extensionContext = nil;
}
#end
Hope it will help somebody to save couple of hours struggling with the completionHandler issue.
I had the same issue in my iOS 12.1. I'm calling
loadItemForTypeIdentifier:kUTTypeData
instead of kUTTypeImage etc. It worked for me.
I have been battled with this issue on and off for the last few weeks, and have finally found the issue. I has nothing to do with Objective C or Swift, it just appears to be a bug in Apple's code.
It seems that (as at iOS 8.0), the completion block is only called if you are using your own UIViewController subclass. If you are using a subclass of SLComposeServiceViewController, then the completion block is not called.
This is really annoying, as by default XCode creates you a ShareViewController with a subclass of SLComposeServiceViewController. To work around this issue, you just have to modify ShareViewController to inherit from UIViewController. This will still give access to the extensionContext property, but you'll obviously lose all of nice standard functionality and will have to implement your UI from scratch.
I've filed a radar with Apple, but have not had a reply yet. Hopefully this will be fixed in a future update.