React Native Clipboard - how to copy an image or anything other than text? - clipboard

In React Native, with the Clipboard, how can I place an image in the Clipboard? The only method provided to set Clipboard content is "setString". Can you not set images or other content than strings?

It is possible to bridge native iOS clipboard API and expose the setImage method. To do that you need:
Add native module header file Clipboard.h:
#import "RCTBridgeModule.h"
#interface Clipboard : NSObject <RCTBridgeModule>
#end
Add native module implementation file Clipboard.m. We needed to copy base64 encoded images but you can adjust the code use any other image representation:
#import <UIKit/UIKit.h>
#import <MobileCoreServices/UTCoreTypes.h>
#import "Clipboard.h"
#implementation Clipboard
RCT_EXPORT_MODULE(BetterClipboard); // this is how our native module will be named
RCT_EXPORT_METHOD(addBase64Image:(NSString *)base64Image) {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setPersistent:YES];
NSData *imageData = [[NSData alloc]initWithBase64EncodedString:base64Image options:NSDataBase64DecodingIgnoreUnknownCharacters];
[pasteboard setImage:[UIImage imageWithData:imageData]];
}
#end
And then you can use it in your React application:
import { NativeModules } from 'react-native';
NativeModules.BetterClipboard.addBase64Image(base64EncodedImage);
Unfortunately, I don't know how to do the same for Android.

Related

How to wrap integrate Microsoft Appcenter's Code-Push in Xcode

I have a react-native (v0.59.9) application published on the App Store and the Play Store. The react-native-code-push library has already been successfully added to the binaries of these initial App Store submissions.
My goal is to actually start using the code-push servers. to do that Microsoft's doc say:
For React Native 0.59 - 0.59.10: Find the following line of code,
which sets the source URL for bridge for production releases:
objective-c
return [[NSBundle mainBundle]
URLForResource:#"main"withExtension:#"jsbundle";
Replace it with this line:
return [CodePush bundleURL];
However my project is RN-v0.59.9, and my appDelegate.m file looks like this:
#import "AppDelegate.h"
#import <CodePush/CodePush.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <ReactNativeNavigation/ReactNativeNavigation.h>
#import "SplashScreen.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:#"index" fallbackResource:nil];
[ReactNativeNavigation bootstrap:jsCodeLocation launchOptions:launchOptions];
[SplashScreen show];
return YES;
}
#end
What changes do I need to make to integrate the library completely?
You can use [CodePush bundleURL] instead of [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:#"index" fallbackResource:nil]
Please refer to the example app for more info.

Restkit multipartFormRequestForObject example does not build for me

My viewController has the following method, which gets called after an image gets selected via a UIImagePickerController. I'd like to upload the selected image to my web service, however, when attempting to follow the samples provided by RestKit I get the following error:
No visible #interface for 'RKObjectManager' declares the selector 'multipartFormRequestForObject:method:path:parameters:constructingBodyWithBlock:'
I'm using the most recent version of restkit, and right clicked went to definition to check the signature which seems correct.
It is worth noting that AFMultipartFormData is not highlighting in XCode. I tried including #import AFNetworking/AFHTTPClient.h but it still shows as plain text, which i suspect might be the problem?
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image];
ImageRecord *imageRecord = [ImageRecord new];
NSDictionary *params = #{#"param1" : #"value1",
#"param2" : #"value2",
#"param3" : #"value3"};
// Serialize the Article attributes then attach a file
NSMutableURLRequest *request = [[RKObjectManager sharedManager] multipartFormRequestForObject:imageRecord method:RKRequestMethodPOST path:#"stuff" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:UIImagePNGRepresentation(image)
name:#"article[image]"
fileName:#"photo.png"
mimeType:#"image/png"];
}];
RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] objectRequestOperationWithRequest:request success:nil failure:nil];
[[RKObjectManager sharedManager] enqueueObjectRequestOperation:operation]; // NOTE: Must be enqueued rather than started
[self dismissViewControllerAnimated:YES completion:NULL];
}
Thanks for any pointers!
That method is multipartFormRequestWithObject: (note that the method name you're using has ForObject). You shouldn't need to import any additional headers.

displaying an image in cocoa

I am totally new at this, but here goes:
I want to press a button in my application, and have an image display (I am planning to read it off of a camera, but to start with, I will open a .TIF file.) However, within the interface builder, I can make buttons within an NSObject object, but the literature makes it sound like I need to make an NSView object to display a file. The problem is, when I do this, the NSObject object does not seem to talk to the NSView object. I am trying to do something like:
NSString *inFilePath;
inFilePath = #"/Volumes/Data/Temp/Smiles.tiff";
NSImage *TestImage = [[NSImage alloc] initWithContentsOfFile:inFilePath];
[MyView setImage:TestImage];
Here, MyView is the NSView object. I get warnings that MyView may not respond to setImage. I have tried to define an IBOutlet within the NSObject object, and although I can connect it within the interface builder, console gives me the error:
unrecognized selector sent to class 0x1e080
So, it's not clear what the next step is. Is there an easy way to get two different objects to "talk to" each other?
Thanks
You want an NSImageView object. In the Interface Builder library this is called an Image Well, but you can configure it so that it doesn't have a bezel.
NSImageView is a subclass of NSView that is optimised for displaying images.
In your header (.h) file, you should have something like this:
#interface MyController : NSObject
{
//this declares an outlet so you can hook up the
//image view in Interface Builder
IBOutlet NSImageView* imageView;
}
#end
And in your implementation (.m) file:
#implementation MyController
//called once the nib is loaded and all outlets are available
- (void)awakeFromNib
{
NSString *inFilePath = #"/Volumes/Data/Temp/Smiles.tiff";
NSImage *testImage = [[NSImage alloc] initWithContentsOfFile:inFilePath];
#if !__has_feature(objc_arc)
[testImage autorelease];
#endif
//imageView is your outlet
[imageView setImage:testImage];
}
#end
In Interface Builder you should hook up the imageView outlet of your class to point to the NSImageView you placed on your view.

Adding a sound to load with a spash screen in xcode

I have set up a splash screen for my app project and would like to have an sound (mp3) play just after the splash screen loads. I am new to using xcode and wondered whether some one could advise on what code I will need to input/where to input it..
Many Thanks
You implement a yourintrosound.wav system sound in the first view controller, then play it with the appDelegate just after the view controller is loaded:
in your firstViewController.h
#interface...
CFURLRef yourSound;
SystemSoundID soundFileObject;
#property (readwrite) CFURLRef yourSound;
#property SystemSoundID soundFileObject;
in your firstViewController.m viewDidLoad
NSURL *rightSound = [[NSBundle mainBundle] URLForResource: #"yourintrosound" withExtension: #"wav"];
self.yourSound = (CFURLRef) [rightSound retain];
AudioServicesCreateSystemSoundID (yourSound, &soundFileObject);
[rightSound release];
in your AppDelegate.m application: didFinishLaunchingWithOptions:
(just after [window makeKeyAndVisible] or any splash screen display you implemented)
AudioServicesPlaySystemSound (firstViewController.soundFileObject);
and do dot forget the AudioToolbox framework!

How to clear a text field in Cocoa

I'm trying to make a simple Cocoa application using the newest version of XCode. In interface builder I added NSTextField and NSButton. When I press the button, I want it to clear whatever is in the text field.
I made a new class called AppController.h. This is the contents:
#import <Foundation/Foundation.h>
#interface AppController : NSObject {
IBOutlet id textView;
}
- (IBAction) clearText: sender;
#end
AppController.m looks like this:
#import "AppController.h"
#implementation AppController
- (IBAction) clearText: sender
{
[textView setString: #" "];
}
#end
I connected the button to clearText and the textbox to textView.
The program compiles and runs. But when I press the button, nothing happens. Why is that?
here's the run down.
1.Create an IBAction in your appController class header.
- (IBAction)someMethod:(id)sender;
2.Then create an IBOutlet for you text field
IBOutlet NSTextField *textFieldname;
You then connect the IBAction to the button in interface builder, and your IBOutlet to your textfield.
Inside the implementation file (.m) IBAction method do
- (IBAction)someMethod:(id)sender{
textFieldname.stringValue = #"";
}
This is very basic. I suggest you google for some tutorials. There's plenty out there. Here's something that may help. Chapter 8 describes how to do exactly what you're asking.
link text

Resources