How to share image through WhatsApp? - image

I'm using this code, but it's not working, how can it fit it?
Anyone help? Thanks!
UIImage *iconImage = [UIImage imageNamed:#"Home01-A01.png"];
NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/whatsAppTmp.wai"];
[UIImageJPEGRepresentation(iconImage, 1.0) writeToFile:savePath atomically:YES];
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:savePath]];
_documentInteractionController.UTI = #"net.whatsapp.image";
_documentInteractionController.delegate = self;
[_documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];

Hi You can share your file on WhatsApp using UIDocumentationView Controller, do the following to achieve the same,
In your viewcontroller.h file,
add Delegate for
UIDocumentInteractionControllerDelegate
declare UIDocumentInteractionController object like this
UIDocumentInteractionController *controller;
In your viewcontroller.m file,
add following Delegate methos for UIDocumentationView controller to track the status for success and failure
pragma mark - Delegate Methods
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
NSLog(#"Starting to send this puppy to %#", application);
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application {
NSLog(#"We're done sending the document.");
}
add following code to share your file,
-(void)openDocument {
// here's a URL from our bundle
NSURL *documentURL = [[NSBundle mainBundle]URLForResource:#"chat" withExtension:#"png”];// You can set your filename and extention here
// pass it to our document interaction controller
controller = [[UIDocumentInteractionController alloc]init];
controller.delegate = self;
controller.URL = documentURL;
// present the preview
[controller presentPreviewAnimated:YES];
}
Note: the URL that you specify needs to be local.
The goal of the UIDocumentInteractionController is to share (local) documents
between applications.if your file is not stored locally then you can
achive this by storing the file in temp or local caching directory then accessing it from the
main bundle.

Related

XPC Between two cocoa applications in workspace, the NSXPCConnection is immediately being invalidated

I have two Cocoa Applications, one is going to be the sender and another the receiver in this XPC relationship.
In the applicationDidFinishLaunching in the sender, I first open the second receiver application
NSError* error = nil;
NSURL* url = [[NSBundle mainBundle] bundleURL];
url = [url URLByAppendingPathComponent:#"Contents" isDirectory:YES];
url = [url URLByAppendingPathComponent:#"MacOS" isDirectory:YES];
url = [url URLByAppendingPathComponent:#"TestXPCHelper.app" isDirectory:YES];
[[NSWorkspace sharedWorkspace] launchApplicationAtURL:url
options:NSWorkspaceLaunchWithoutActivation
configuration:[NSDictionary dictionary]
error:&error];
if ( error )
{
NSLog(#"launchApplicationAtURL:%# error = %#", url, error);
[[NSAlert alertWithError:error] runModal];
}
Then I create my NSXPCConnection
assert([NSThread isMainThread]);
if (self.testConnection == nil) {
self.testConnection = [[NSXPCConnection alloc] initWithMachServiceName:NEVER_TRANSLATE(#"com.TechSmith.TestXPCHelper") options:NSXPCConnectionPrivileged];
self.testConnection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:#protocol(TestXPCProtocol)];
self.testConnection.interruptionHandler = ^{
NSLog(#"Connection Terminated");
};
self.testConnection.invalidationHandler = ^{
self.testConnection.invalidationHandler = nil;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
self.testConnection = nil;
}];
};
[self.testConnection resume];
}
Then I try to send a message over the connection (the connection is already invalidated by here)
id<TestXPCProtocol> testRemoteObject= [self.testConnection remoteObjectProxy];
[testRemoteObject testXPCMethod2];
[[self.testConnection remoteObjectProxyWithErrorHandler:^(NSError * proxyError){
NSLog(#"%#", proxyError);
}] testXPCMethod:^(NSString* reply) {
NSLog(#"%#", reply);
}];
And here is the app delegate for my receiver application:
#interface AppDelegate () <NSXPCListenerDelegate, TestXPCProtocol>
#property (weak) IBOutlet NSWindow *window;
#property NSXPCListener *xpcListener;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
NSLog(#"TESTING123");
self.xpcListener = [[NSXPCListener alloc] initWithMachServiceName:#"com.TechSmith.TestXPCHelper"];
self.xpcListener.delegate = self;
[self.xpcListener resume];
}
- (void)applicationDidBecomeActive:(NSNotification *)notification {
NSLog(#"ACTIVE234");
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
- (void)run
{
NSLog(#"RUNNING");
// Tell the XPC listener to start processing requests.
[self.xpcListener resume];
// Run the run loop forever.
[[NSRunLoop currentRunLoop] run];
}
- (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection
{
NSLog(#"LISTENING");
assert(listener == self.xpcListener);
#pragma unused(listener)
assert(newConnection != nil);
newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:#protocol(TestXPCProtocol)];
newConnection.exportedObject = self;
[newConnection resume];
return YES;
}
- (void)testXPCMethod:(void(^)(NSString * version))reply
{
NSLog(#"HEY");
reply(#"REPLY HERE");
}
- (void)testXPCMethod2
{
NSLog(#"TWO!");
}
Here is the proxyError when I try to send a message over the connection:
Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service
named com.TechSmith.TestXPCHelper was invalidated." UserInfo={NSDebugDescription=The
connection to service named com.TechSmith.TestXPCHelper was invalidated.}
So I think I am doing something wrong with my instantiation of the NSXPCConnection. I can't find a good example of two applications speaking to eachother-- it's always one application and a service. Is that what my problem is? I need a service inbetween the applications talking?
Is there any way to get more information on why this connection is being invalidated? That would also help a lot
So pretty straight forward problem here,
Turns out initWithMachServiceName is explicitly looking for a mach service. I was using an identifier of another application process.
If I actually use an identifier of a valid mach service, there is no issue
Note that there are two other ways to create an NSXPCConnection,
with an NSXPCEndpoint or with a XPCService identifier

Xcode Open target=_blank links in Safari

I'm pretty new to Xcode... I have a single page iOS app that just has a UIWebView opening a specific URL. I would like any links within the pages that have target="_blank" to open in Safari, rather than inside the app.
Can someone tell me how to accomplish this? (I've searched everywhere) and also tell me in which files and where to put the code? Thank you SOOO much!!
EDIT
I implemented the following code in my ViewController.m file:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Add line below so that external links & PDFs will open in Safari.app
webView.delegate = self;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.example.com/"]]];
}
// Add section below so that external links & PDFs will open in Safari.app
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:request.URL];
return false;
}
return true;
}
But for the line webView.delegate = self; I am getting a yellow warning that says:
Assigning to 'id'from incompatible type 'UIWebViewViewController *const_strong'
What is this error, and how can I fix it in Xcode?
Perhaps following answer on SO can solve your problem or atleast give you some ideas on how to achieve what you are trying to do:
UIWebView open links in Safari
This is the way we solved it:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
first.delegate = (id)self;
[first loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.website.com"]]];
}
// Add section below so that external links & PDFs will open in Safari.app
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeOther) {
NSString *checkURL = #"http://www.linkyouwanttogotoviasafari.com";
NSString *reqURL = request.URL.absoluteString;
if ([reqURL isEqualToString:checkURL])
{
[[UIApplication sharedApplication] openURL:request.URL];
return false;
}
else {
return true;
}
}
return true;
}

RestKit: Multiple RKClients / Changing baseURL

I initialize RKClient's sharedClient in my application delegate in applicationDidFinishLaunching and it works great. I am using this clients destination URL for most of the application, however at 1 point, in 1 class (Player), I need to load user's avatars from gravatar.com. So, I had the Player class define it's own RKClient and conform to the RKRequestDelegate protocol. It then makes the request, through this new RKClient instantiation and sets the delegate of the request to self. The problem is that I never receive a response; that is
- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response
Is never called. Here is the whole code sample:
// Player.m
#import "Player.h"
#implementation Player
# pragma mark - Accessor Synthesizers
#synthesize identifier = _identifier;
#synthesize name = _name;
#synthesize story = _story;
#synthesize emailHash = _emailHash;
#synthesize pointPercentage = _pointPercentage;
#synthesize hitPercentage = _hitPercentage;
#synthesize lastCups = _lastCups;
#synthesize shotCount = _shotCount;
#synthesize hitCount = _hitCount;
#synthesize wins = _wins;
#synthesize losses = _losses;
#synthesize gravatar = _gravatar;
# pragma mark - Instance Methods
- (void)getGravatar {
RKClient *gClient = [RKClient clientWithBaseURL:[NSURL URLWithString:#"http://gravatar.com"]];
NSString *path = [self gravatarLink];
NSLog(#"Getting Gravatar With Link: http://gravatar.com%#", path);
[gClient get:path delegate:self];
}
- (NSString *)description {
return [NSString stringWithFormat:#"{Season: [id=%i, name=%#, story=%# ]}", _identifier, _name, _story];
}
# pragma mark - RKRequest Delegate Methods
- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
NSLog(#"Gravatar Back: %#", response.bodyAsString);
self.gravatar = response.body;
[[NSNotificationCenter defaultCenter] postNotificationName:#"GravatarLoaded" object:self];
}
# pragma mark - Private Methods
- (NSString *)gravatarLink {
NSString *path;
path = [NSString stringWithFormat:#"/avatar/%#?d=monsterid&r=x", _emailHash];
if([[UIScreen mainScreen] respondsToSelector:#selector(scale)] && [[UIScreen mainScreen] scale]==2)
return [path stringByAppendingString:#"&s=200"];
else
return [path stringByAppendingString:#"&s=100"];
}
#end
Also, I have tried changing the base URL of the sharedClient and just using the sharedClient for the gravatar requests. But whenever I try to change the baseURL property of the RKClient sharedClient, either of these ways:
[[RKClient sharedClient] setBaseURL:[NSURL URLWithString:#"http://gravatar.com"]];
or
[RKClient sharedClient].baseURL: [NSURL URLWithString:#"http://gravatar.com"];
I get a runtime error:
2012-04-07 15:37:23.565 MLP[34403:fb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL URLByAppendingResourcePath:queryParameters:]: unrecognized selector sent to instance 0x8bcdd50'
*** First throw call stack:
(0x1b7c022 0x1f58cd6 0x1b7dcbd 0x1ae2ed0 0x1ae2cb2 0x23eb5 0x24032 0x6ddb 0xdda2 0xc465c5 0xc467fa 0x14db85d 0x1b50936 0x1b503d7 0x1ab3790 0x1ab2d84 0x1ab2c9b 0x20407d8 0x204088a 0xbb5626 0x27cd 0x2735)
terminate called throwing an exception(lldb)
I used:
[[RKClient sharedClient] setBaseURL:[RKURL URLWithString:#"http://gravatar.com"]];
That worked for me.
The clientWithBaseURL: method expects an NSString, not a NSURL. Try to use
[[RKClient sharedClient] setBaseURL:#"http://gravatar.com"];
you just need to go to Targets-> build Settings —> other Linker Flags
As specified in the RestKit Tutorial we changed added a flag called “-ObjC-all_load” now edit that to just display “-ObjC”
this must wok for you.

EXC_BAD_ACCESS while working with Core Data

I'm new into Cocoa and am writing a simple app to learn working with Core Data, but it crashes with EXC_BAD_ACCESS. Tried several things and haven't find the solution yet. As I said, I'm not very experienced in Cocoa.
I have followed the usual Core Data tutorials.
This is my Model:
I've added these two entities as NSArrayController in my Nib file and have two NSTableViews with Value Binding to the entity objects.
And here's the code:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSManagedObjectContext *context = [self managedObjectContext];
TaskList *list = [NSEntityDescription
insertNewObjectForEntityForName:#"TaskList"
inManagedObjectContext: context]; // EXC_BAD_ACCESS happens here
[list setTitle:#"Inbox"];
Task *task = [NSEntityDescription
insertNewObjectForEntityForName:#"Task"
inManagedObjectContext: context];
[task setKey:#"Remember the milk"];
[task setList:list];
NSError *error;
if (![context save:&error]) {
NSLog(#"Error: %#", [error localizedDescription]);
}
}
That's it! That's all my program. I am using Xcode 4.2, developing a Mac app, and ARC is enabled.
UPDATE: jrturton asked me to include implementation of [self managedObjectContext]. I didn't write this code, but here's what I found in AppDelegate.h:
#property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
And this is from AppDelegate.m:
#synthesize managedObjectContext = __managedObjectContext;
...
/**
Returns the managed object context for the application (which is already
bound to the persistent store coordinator for the application.)
*/
- (NSManagedObjectContext *)managedObjectContext {
if (__managedObjectContext) {
return __managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:#"Failed to initialize the store" forKey:NSLocalizedDescriptionKey];
[dict setValue:#"There was an error building up the data file." forKey:NSLocalizedFailureReasonErrorKey];
NSError *error = [NSError errorWithDomain:#"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
[[NSApplication sharedApplication] presentError:error];
return nil;
}
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
return __managedObjectContext;
}
Check your managed object model. Make sure all the entity and attribute names are spelled correctly. Also check your object class files and make sure they contain what you expect.
Maybe the debugger does not show you the correct row when crashing: I noticed, that you have a method setKey:, but no attribute called keyin your Task entity. Try setting all the attributes with the dot notation, like list.title = #"Inbox". (This is generally easier to read and avoids similar errors.)
As suggested, before the line inserting the new entity, set a breakpoint and make sure the managed object context is not null.
Finally, perhaps you have to cast your object. insertNewObjectForEntityForName: returns an object of type NSManagedObject, but you are assigning it to a type TaskList. Try adding the cast before you use the object:
TaskList *list = (TaksList *) [NSEntityDescription
insertNewObjectForEntityForName:#"TaskList"
inManagedObjectContext: context];
I had this same issue. I resolved it like Mostafa said above. If you create a project with Core Data enabled, it will automatically create a file for you. Use this .xcdatamodeld file instead of a custom one. If you have one already created, just delete the originally created file and rename your datamodel file to the originally created file name.

NSMutableDictionary setObject:forKey - custom class

I am using this in a UINavigation environment.
I have customClassA. It inherits customClassB and one of its object is a NSMutableDictionary.
I alloc and init customClassA in a viewController, then for adding data, I am pushing a new viewController into the stack. The addNewDataViewController sends the newly added data, a customClassB object back by its delegate. Everything works fine so far.
customClassA has to store the returned object (customClassB) into its NSMutableDictionary object with a key (an NSString created from NSDate).
I get "mutating method sent to immutable object" error and can't think of any solution.
Any help is appreciated.
Thanks.
===========================
interface customClassA : NSObject
{
NSDate date;
NSArray *array; // will contain only NSString objects
}
// and the rest as customary
...
#import "customClassA.h"
interface customClassB : NSObject
{
NSString *title;
NSMutableDictionary *data; // will contain values of customClassA with keys of NSString
}
// and the rest as customary
...
#import "customClassB"
#interface firstViewController : UITableViewController <SecondViewControllerDelegate>
- (void)viewDidLoad
{
self.customClassB_Object = [customClassB alloc] init];
// and the rest...
}
- (void)secondViewControllerDidSaveData:(customClassA *)aData
{
[self.customClassB_Object.data setObject:aData forKey:[NSString stringWithFormat:#"%#", aData.date]];
// update tableView
}
Make sure you are initializing the NSMutableDictionary with something like
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
It would appear that your NSMutableDictionary is getting created with an NSDictionary instance instead of a NSMutableDictionary
Althoguh I added the following code to customClassB implementation, it still didn't work.
#implementation customClassB
- (id)init
{
self = [super init];
if (self)
self.data = [NSMutableDictionary alloc] init];
return self;
}
so I added two custom methods to my customClassB implementation, as well as in the header file:
- (void)appendData:(customClassA *)aData;
- (void)removeDataWithKey:(NSString *)aKey;
and instead of manipulating the data dicionary of customClassB in my viewController, I simply call that method and pass the data object to the class and it did the trick.

Resources