NSConnectionDelegate methods doesn't be execute - macos

I'm trying to develop a simple OS X application that should do the following:
Read a file to get username and password
Obtain Mac serial number from server
Connect to a remote service to login with username, password and Mac serial number
If the user is authorized to be connected with this service it should run an infinite
Infinite loop should call a service that connect to another service to get news
If there are news it should send a local notification
This application should work without GUI, so I'm using an Xcode command line tool. The application is organized so:
main.m read file with username and password and obtain the Mac serial number
Connection.m should connect to the login service and run the checkNews service
CheckNews.m should connect to the news service and if there are a news should run a class to send local notification
NotificationCenterDelegate.m should simply send the local notification with news inside
My problem is that if I run the app it doesn't connect to the login service, because it doesn't calls the NSConnectionDelegate methods. I searched on the web and I found that the connection should be in the main thread, I tested it by using the following code: NSLog(#"Is%# main thread", ([NSThread isMainThread] ? #"YES" : #" NOT")); and it returned me that the connection is in the main thread, so I don't know why it doesn't run the NSConnectionDelegate methods. If you need I can post the code of my Connection.m class to understand why it doesn't working well. I hope you can help me to fix that issue.
Connection.m
#import "Connection.h"
#import "CheckNews.h"
#interface Connection() {
NSMutableData *responseData;
}
#property(nonatomic,strong)NSString *fakeBundleIdentifier;
#property BOOL installNSBundleHook;
#end
#implementation Connection
- (id)initWithFakeBundle:(NSString *)fakeBundleIdentifier andInstallNSbundleHook:(BOOL)installNSBundleHook {
if (self) {
self.fakeBundleIdentifier = fakeBundleIdentifier;
self.installNSBundleHook = installNSBundleHook;
}
return self;
}
- (id)sendRequestToURL:(NSString *)urlString withMethod:(NSString *)method withUsername:(NSString *)username withPassword:(NSString *)password andSerialNumber:(NSString *)serialNumber {
NSURL *finalURL;
if ([method isEqualToString:#"POST"]) {
finalURL = [NSURL URLWithString:urlString];
} else {
NSLog(#"Metodo no previsto");
}
NSString *post = [NSString stringWithFormat:#"username=%#&password=%#&serialNumber=%#", username, password, serialNumber];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:#"%lu", (unsigned long)postData.length];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setURL:finalURL];
[request setHTTPMethod:method];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection) {
[connection start];
}
return connection;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
responseData = [[NSMutableData alloc]init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(#"%#", error);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSDictionary *json;
NSError *error;
json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
if ([self loginWithSuccessFromJson:json]) {
[self checkNews];
}
}
- (BOOL)loginWithSuccessFromJson:(NSDictionary *)json {
int success = [[json objectForKey:#"success"]intValue];
if (success == 1) {
return YES;
} else {
return NO;
}
}
- (void) checkNews {
CheckNews *checkNews = [[CheckNews alloc]initWithFakeBundle:self.fakeBundleIdentifier andInstallNSbundleHook:self.installNSBundleHook];
for(;;) {
[checkNews sendRequestToURL:#"" withMethod:#"GET"];
}
}

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

how could i integrate via me social site into iphone app

hi i want to integrate Via Me social site into my iphone app,i googled but didn't find any samples.
The basic process is as follows:
Create a custom URL scheme for your app. Via Me will use this after the user has been authenticated, to return to your app. In my example, I created one called "robviame://"
Register your app at http://via.me/developers. This will give you a client id and a client secret:
When you want to authenticate the user, you call:
NSString *redirectUri = [[self redirectURI] stringByAddingPercentEscapesForURLParameterUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:#"https://api.via.me/oauth/authorize/?client_id=%#&redirect_uri=%#&response_type=code", kClientID, redirectUri];
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
What that will do is fire up your web browser and give the user a chance to log on and grant permissions to your app. When user finishes that process, because you've defined your custom URL scheme, it will call the following method in your app delegate:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
// do whatever you want here to parse the code provided back to the app
}
for example, I'll call a handler for my Via Me response:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
ViaMeManager *viaMeManager = [ViaMeManager sharedManager];
if ([[url host] isEqualToString:viaMeManager.host])
{
[viaMeManager handleViaMeResponse:[self parseQueryString:[url query]]];
return YES;
}
return NO;
}
// convert the query string into a dictionary
- (NSDictionary *)parseQueryString:(NSString *)query
{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
NSArray *queryParameters = [query componentsSeparatedByString:#"&"];
for (NSString *queryParameter in queryParameters) {
NSArray *elements = [queryParameter componentsSeparatedByString:#"="];
NSString *key = [elements[0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *value = [elements[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
value = [[value componentsSeparatedByString:#"+"] componentsJoinedByString:#" "];
[dictionary setObject:value forKey:key];
}
return dictionary;
}
That handler might, for example, save the code and then request the access token:
- (void)handleViaMeResponse:(NSDictionary *)parameters
{
self.code = parameters[#"code"];
if (self.code)
{
// save the code
[[NSUserDefaults standardUserDefaults] setValue:self.code forKey:kViaMeUserDefaultKeyCode];
[[NSUserDefaults standardUserDefaults] synchronize];
// now let's authenticate the user and get an access key
[self requestToken];
}
else
{
NSLog(#"%s: parameters = %#", __FUNCTION__, parameters);
NSString *errorCode = parameters[#"error"];
if ([errorCode isEqualToString:#"access_denied"])
{
[[[UIAlertView alloc] initWithTitle:nil
message:#"Via Me functions will not be enabled because you did not authorize this app"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] show];
}
else
{
[[[UIAlertView alloc] initWithTitle:nil
message:#"Unknown Via Me authorization error"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] show];
}
}
}
and the code to retrieve the token might look like:
- (void)requestToken
{
NSURL *url = [NSURL URLWithString:#"https://api.via.me/oauth/access_token"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
NSDictionary *paramsDictionary = #{#"client_id" : kClientID,
#"client_secret" : kClientSecret,
#"grant_type" : #"authorization_code",
#"redirect_uri" : [self redirectURI],
#"code" : self.code,
#"response_type" : #"token"
};
NSMutableArray *paramsArray = [NSMutableArray array];
[paramsDictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
[paramsArray addObject:[NSString stringWithFormat:#"%#=%#", key, [obj stringByAddingPercentEscapesForURLParameterUsingEncoding:NSUTF8StringEncoding]]];
}];
NSData *paramsData = [[paramsArray componentsJoinedByString:#"&"] dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:paramsData];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error)
{
NSLog(#"%s: NSURLConnection error = %#", __FUNCTION__, error);
return;
}
NSError *parseError;
id results = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
if (parseError)
{
NSLog(#"%s: NSJSONSerialization error = %#", __FUNCTION__, parseError);
return;
}
self.accessToken = results[#"access_token"];
if (self.accessToken)
{
[[NSUserDefaults standardUserDefaults] setValue:self.accessToken forKey:kViaMeUserDefaultKeyAccessToken];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}];
}
Hopefully this will be enough to get you going. This is described in greater detail at the http://via.me/developers page.

NSURLConnection response not complete

I am making call to a server requesting JSON data using NSURLConnection.
For some reason I get part of the response. If I hit the url through the browser the response its correct. The weird thing is that it happens only sometime. So I'm having a hard time debugging the issue.
Then when because the response is not complete I get the following error:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0xa4634a0 {NSDebugDescription=Invalid value around character 0.} {
NSDebugDescription = "Invalid value around character 0.";
}
I guess it could also be an issue with the server it self. Here's my code:
-(void) getShareHistory:(NSString *)range paging:(NSInteger *)page{
NSString *post = [NSString stringWithFormat:#"range=%#&paging=%#",
range,
[NSString stringWithFormat:#"%ld",(long)page]];
NSString *url = [NSString stringWithFormat:#"http://www/domai.com/handle_share_links.php?action=history"];
NSData *post_data = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [post_data length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:post_data];
self.shareHistoryConn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)response{
NSString *strData = [[NSString alloc]initWithData:response encoding:NSASCIIStringEncoding];
NSLog(#"response %#",strData);
NSError *jsonParsingError = nil;
if(connection == self.shareHistoryConn)
{
NSArray *data = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&jsonParsingError];
if(!jsonParsingError)
{
[[self delegate] onGetShareHistorySuccess:data];
}else{
[[self delegate] onGetShareHistoryFailed:jsonParsingError];
}
}
Thanks in advance.
What you're seeing is normal behavior. didReceiveData can be called any number of times. It is up to you to keep accumulating the data until you get connectionDidFinishLoading.
The standard delegate structure is like this:
- (void) connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response {
// connection is starting, clear buffer
[self.receivedData setLength:0];
}
- (void) connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data {
// data is arriving, add it to the buffer
[self.receivedData appendData:data];
}
- (void)connection:(NSURLConnection*)connection
didFailWithError:(NSError *)error {
// something went wrong, clean up interface as needed
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// all done, we are ready to rock and roll
// do something with self.receivedData
}
Always implement all four delegate methods.

RestKit iOS - Second Time fails

I'm using the RestKit for a simple request that returns JSON data.
I've implemented this code that works only the first time that the viewDidLoad is called, the second time the request is not performed. Why??
- (void)viewDidLoad
{
[super viewDidLoad];
[self planOtpTrip];
}
-(void) planOtpTrip{
NSLog(#"[INFO] planoOtpTrip");
client = [RKClient clientWithBaseURLString:#"http://myserver/opentripplanner-api-webapp/ws"];
[client setValue:#"application/json" forHTTPHeaderField:#"Accept"];
NSDictionary *queryParameters = [NSDictionary dictionaryWithObjectsAndKeys:#"45.028952,7.624598",#"fromPlace",#"45.06123,7.65981", #"toPlace",#"TRANSIT,WALK" ,#"mode", nil];
// Imposto il nome della API da richiamare
NSString *getResourcePath = RKPathAppendQueryParams(#"/plan", queryParameters);
[client get:getResourcePath delegate:self];
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
// where you handle response object
NSLog(#"Risposta ricevuta: %#", response.URL);
NSLog(#"Response Body: %#", response.bodyAsString);
id jsonParser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeJSON];
NSError *error = nil;
NSDictionary* parsedResponse = [jsonParser objectFromString:[response bodyAsString] error:&error];
if (error == nil)
{
NSLog(#"GET returned with HTTP Code %d and parsedContent: %#", [response statusCode], parsedResponse);
}
else
{
NSLog(#"Error: %#", error);
}
return [self renderResponse:parsedResponse];
}
I had this problem as well but i found a solution. Define the RKClient in your .h file like so:
#property (retain) RKClient *downloadClient;
Then in your .m file synthesize the downloadClient like so:
#synthesize downloadClient;
And then in your didLoadResponse method you release the downloadClient when your done with it, like so:
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response
{
//do something with your response
[downloadClient release];
}

how to stop nsurlconnection?

i'm using the url connection with nsfile handle to receive the data from server,
this is the code that i use,
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response {
filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"Ep1.mp4";
[[NSFileManager defaultManager] createFileAtPath:filename contents:nil attributes:nil];
file =[NSFileHandle fileHandleForUpdatingAtPath:filename] ;
if (file) {
[file seekToEndOfFile];
}}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if (file) {
[file seekToEndOfFile];
} [file writeData:data]; }
- (void)connectionDidFinishLoading:(NSURLConnection*)connection {
[file closeFile];
}
-(void)downloadFile{
targetURL = [NSURL URLWithString:#"http://some url "];
DownloadRequest = [NSURLRequest requestWithURL:targetURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:50.0];
DownloadConnection = [[NSURLConnection alloc] initWithRequest:DownloadRequest delegate:self];
if (DownloadConnection) {
receivedData = [NSMutableData data];
}
}
i want to stop the connection, when downloading file, so, i create the button
--->
- (void)buttonPressed:(id)sender
{
DownloadConnection = [[NSURLConnection alloc] initWithRequest: DownloadRequest delegate:self];
[ DownloadConnection cancel];
}
but doesn't work, the data still receiving, did i miss understand something?
or what can i do?
Edited :
this is the solution that i found:
- (void)buttonPressed:(id)sender
{
DownloadConnection = [[NSURLConnection alloc] initWithRequest: DownloadRequest delegate:self];
[ self.DownloadConnection cancel];
}
add the self to cancel the current connection...

Resources