2017-01-27 7 views
1

私は2つのCocoaアプリケーションを持っていますが、1つはこのXPC関係の送信者と受信者になります。送信側でapplicationDidFinishLaunchingXPCワークスペース内の2つのココアアプリケーションの間で、NSXPCConnectionが直ちに無効化されます

、私は第一、第二の受信機アプリケーション

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]; 
    } 

を開き、それから私は私の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]; 
    } 

を作成それから私は、接続(接続を介してメッセージを送信しようここで既に無効になっている)

id<TestXPCProtocol> testRemoteObject= [self.testConnection remoteObjectProxy]; 
    [testRemoteObject testXPCMethod2]; 

    [[self.testConnection remoteObjectProxyWithErrorHandler:^(NSError * proxyError){ 
    NSLog(@"%@", proxyError); 
    }] testXPCMethod:^(NSString* reply) { 
    NSLog(@"%@", reply); 
    }]; 

そして、ここでは私の受信機アプリケーション用のアプリデリゲートである:私は接続を介してメッセージを送信しようとすると、ここで

@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!"); 
} 

proxyErrorです:

エラードメイン= NSCocoaErrorDomainコード= 4099「接続サービスの com.TechSmith.TestXPCHelperは無効になりました。 UserInfo = {NSDebugDescription = com.TechSmith.TestXPCHelperという名前のサービスへ 接続が無効になりました。}

は、だから私は、私はNSXPCConnectionの私のインスタンス化と間違って何かをやっていると思います。お互いに話す2つのアプリケーションの良い例は見つけられません.1つのアプリケーションとサービスです。それは私の問題ですか?アプリケーションの中で話すサービスが必要ですか?

この接続が無効化されている理由の詳細については、方法はありますか?それはまた多くの助けになります

答えて

2

ここでは、かなりまっすぐ前方の問題 デッドアウトinitWithMachServiceNameは、明らかにマッハサービスを探しています。私は別のアプリケーションプロセスの識別子を使用していました。

私が実際に有効なマッハサービスの識別子を使用している場合は、何の問題

NSXPCEndpointXPCService識別子と

NSXPCConnection

を作成するには、2つの他の方法があることはありません

関連する問題