UIPasteboard - copy a few string from other app at one time - ios

In an app called 'A', I copyed three strings to system pasteboard at different times. how can I copy them to my app at once.
go to safari copy a string: “a”
then still in the safari, copy another string: “ab”
go to my app. I already know I can get string “ab” very easy, just like you said. but I want to get both “a” and “ab”.
I konw an app called 'Pin' can store the pasteboard history items.how does it do that?
Any help would be very much appreciated.

You should see more UIPasteboard doc
var strings: [String]?
An array of strings in all pasteboard items.
Usage:
UIPasteboard.generalPasteboard().strings = ["hello", "www.abc.com"]

Related

Swift iOS check if a file has been downloaded from json

I have an app that displays and plays a list of podcasts that is fetched from a json file, I would like to add a download feature but to do this I would like to only show a download icon if the podcast has not been downloaded already, is there a way that I can save something like the id element as well as the podcast title so I could then check to see if its been downloaded and saved on the phone already? Or is there an easier way? Obviously I would like to do this before the list is shown.
You can use UserDefaults for that.
Here's an example on how to read an array from UserDefaults
// Retrieves an array of strings from disk
func getDowloadedPodcasts() -> [String] {
UserDefaults.standard.array(forKey: "downloadedPodcasts") as? [String] ?? []
}
And here's an example on how to append a new value to an array on UserDefaults
func addDownloadedPodcast(podcastId: String) {
let downloadedPodcasts = getDowloadedPodcasts()
downloadedPodcasts.append(podcastId)
UserDefaults.standard.setValue(podcastId, forKey: "downloadedPodcasts")
}
Note that this functions alone won't solve your problem nor are the best solution of your problem, they are here jsut to show how easy it can be to work with UserDefaults and to read/write from non-volatile memory

iOS Notification Content Extension - How to pass data to app?

I wrote a custom Notification Content Extension for my Push Notifications like this:
The thing is, whenever the user is on a certain item in the carousel, I want the GO TO APP button to send a String to the app when it's opening, and from there, handle that string to move the user to the correct ViewController.
I already have the handling part inside the app, I just need to know how to pass that String from the Notification Content Extension to the container app.
Thanks! :)
Enable app groups in capabilities and use suite userDefaults to write the key and read it in the app
NSUserDefaults*defaults= [[NSUserDefaults alloc] initWithSuiteName:#"group.com.company.appName"];
// Write in extension
[defaults setObject:#"anyThing" forKey:#"sharedContent"];
// Read in app
[defaults objectForKey:#"sharedContent"];
If your app is configured for Universal Links or you have defined a Custom URL Scheme for your app, you can also open your app's URL (e.g. with data in query parameters) by calling
extensionContext?.open(url)
in your NotificationViewController.
iOS 13, Swift 5.
Based on the answer by Sh_Khan, here is some Swift Syntax. Obviously I have added App Group as a capability to the target of the main app + the target of the extension, naming the group as "group.ch.Blah" for this example.
Setting your app group, saving a string in our case, needed to set the type as Any cause strings not a type that is available in groups.
let localK = getPrivateKey64() as Any
let defaults = UserDefaults.init(suiteName: "group.ch.Blah")
defaults?.set(localK, forKey: "privateK")
Setting your app group, and reading the string back, needed to recast it back to string.
let defaults = UserDefaults.init(suiteName: "group.ch.Blah")
let localK = defaults?.object(forKey: "privateK") as? String
Worked perfectly with a notification service extension.

How to get data(string,url,image,etc) from UIPasteboard while App is in Background State

i was digging on "How to get data from UIPasteboard while App is in background state" but could not find something useful.i followed this Link
and this but not helpful. any suggestions will be appreciated.
Daan Raman had a tutorial here. It have everything you want.
There are total 4 type of UIPasteboardNotifications
UIPasteboardChangedNotification;
UIPasteboardChangedTypesAddedKey;
UIPasteboardChangedTypesRemovedKey;
UIPasteboardRemovedNotification;
UIPasteboardChangedNotification generate notification when the pasteboard item changed. UIPasteboardChangedTypesRemovedKey and UIPasteboardRemovedNotification are the changes which are mode in pasteboard dictionary. UIPasteboardRemovedNotification when an app remove the pasteboard object.

How can I get raw data of image from UIPasteboard if it's copied by another app (such as photos, safari, etc..)

Is there a way to get raw data of image from UIPasteboard instead of UIImage if it's copied by another app such as photos or mobile safari?
I'm currently facing wired differences between IOS 6.0 and IOS 6.1(7.0 also)
In IOS 6.0, UIPasteboard's item of the copied image by photos or mobile safari contains raw data of the image.
But In IOS 6.1 and above, it contains UIImage instead of raw data.
In IOS 6.0, copied item of UIPasteboard is below
Printing description of array:
<__NSArrayM 0x8a804c0>(
{
"com.compuserve.gif" = <47494638 39614002 ...... 3b3a2000 3b>;
"public.url" = "url of the image....";
}
)
In IOS 6.1 and above, it contains UIImage instead of raw data.
Printing description of array:
<__NSArrayM 0xa25b7b0>(
{
"com.compuserve.gif" = "<UIImage: 0x9429570>";
"public.url" = "url of the image...";
}
)
If that image format is PNG or JPEG, it's not that bad.
(I still have to compress again if it's JEPG though.)
But when I try to paste animated gif image, it becomes more complicated.
I don't know even it's passible to create animated gif image from normal UIImage.
I can download again from original url, but downloading data that I already have seems not good solution I think.
And also, if it's copied from photos app, there's no such url. (there's some mysterious uri named "com.apple.mobileslideshow.asset-object-id-uri" that is undocumented instead of url)
There seems a workaround, because when I try to do exactly same action between photos and email app, It works properly
Any suggestions?
Well now, I figured it out myself.
You can simply get raw (binary)data of the image form general pasteboard by sending
dataForPasteboardType:(NSSting*)PasteboardType message to general pasteboard, if it's copied from Apple's built-in Mobile Safari or Photos App. (#"com.compuserve.gif" for the pasteboard type in my case)
I myself feel a bit foolish for not having checked all the passible methods sooner. :(
My confusion comes from items property of the UIPasteboard.
I thought that items are containing all of data of current pasteboard. So I try to save that array from pasteboard and want to use it later, but I were totally wrong.
As documented in UIPasteboard Class Reference, the items property contains dictionary with key being the representation type and the "value" the object associated with that type.
At this point of time, The "value" refers really "value" of the representation, not the data of that type. This meaning of the "value" is the same as the value of thesetValue: forPasteboardType: method.
On the other words, you cannot retrieve raw(binary) data of the image from items property, even if you set the image to the pasteboard by sending setData: forPasteboardType: message.(I tested it on IOS 7)
In addition, raw data of the image from items property in IOS 6.0 seems a bug of that OS version. (This may not true, it's just my opinion)
You can get NSData from UIPasteboard if you specify right PasteboardType:
NSData* pasteData = [[UIPasteboard generalPasteboard] dataForPasteboardType:(NSString*)kUTTypeJPEG];
Do not forget to import
<MobileCoreServices/MobileCoreServices.h>
The UIPasteBoard will contain whatever is placed in it. It's up to the app that is copying to the paste board to put the contents in the proper format. The app can place items as raw binary data or as objects such as UIImage in the paste board.
If you're getting something different between iOS versions, you're probably using different versions of the app or it's simply copying things differently.
You're right that you can't represent an animated GIF in a UIImage because a UIImage only contains a single image. Perhaps the app is just copying the first frame's bitmap data in that case?
You can convert a UIImage to raw JPEG data using UIImageJPEGRepresentation.

Get copied data from UIPasteboard

i have copied image from UIwebView using clipboard and i want to mail it.For this,I use general pasteboard to get data,but there is a problem in retrieving data.When i check the pasteboard current data,it says the it has Apple Web Archive pasteboard type data,how to read this.here is my code of retriving text.
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
NSArray* array = [pasteboard pasteboardTypes];
for (NSString* type in array) {
NSLog(#"%#",type);
}
NSString* item = #"Apple Web Archive pasteboard type";
NSData* val = [pasteboard dataForPasteboardType:item];
I tried to create a UIImage using this data but that didn't work.
I don't understand what you mean by mail it? You can paste the webpage image copy right into the mail app and it will appear as an image.
You can rebuild the data from the Apple Web Archive pasteboard type if you need to manual. It is essentially a XML document with html and the actual image data all within. The html and accompanying images are base64 encoded. If you want to look at an archive example save this, or perhaps a simple webpage in safari as an archive. Open the archive file in something like Text wrangler. Text edit will probably try to render it.
I've written a post on how to make an Apple Web Archive pasteboard type that might help you understand the process.
http://mcmurrym.wordpress.com/2010/08/13/pasting-simplehtml-into-the-mail-app-ios/
I take it you are trying to mail it from within your app and not using the mail app?
If this is the case you will probably have to get the xml from the pasteboard, find the tag that holds the encoded image data, decode it and create an image from the decoded data.

Resources