Macos NSConnectionDelegate方法不';不能执行
我正在尝试开发一个简单的OS X应用程序,它应该执行以下操作:Macos NSConnectionDelegate方法不';不能执行,macos,cocoa,Macos,Cocoa,我正在尝试开发一个简单的OS X应用程序,它应该执行以下操作: 读取文件以获取用户名和密码 从服务器获取Mac序列号 连接到远程服务以使用用户名、密码和Mac序列号登录 如果用户被授权与此服务连接,则应运行无限期 无限循环应该调用连接到另一个服务的服务来获取新闻 如果有新闻,它应该发送本地通知 这个应用程序应该在没有GUI的情况下工作,所以我使用的是Xcode命令行工具。该应用程序的组织方式如下: main.m读取带有用户名和密码的文件,并获取Mac序列号 m应该连接到登录服务并运行check
- main.m读取带有用户名和密码的文件,并获取Mac序列号
- m应该连接到登录服务并运行checkNews服务
- CheckNews.m应该连接到新闻服务,如果有新闻,应该运行一个类来发送本地通知
- NotificationCenterDelegate.m应该只发送包含新闻的本地通知
NSLog(@“Is%@main-thread”),([NSThread isMainThread]?@“YES”:@“NOT”)代码>并返回连接在主线程中,因此我不知道为什么它不运行NSConnectionDelegate方法。如果你需要,我可以发布我的Connection.m类的代码来理解为什么它不能正常工作。我希望你能帮我解决这个问题
连接.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"];
}
}
代理设置好了吗?是的,请看我添加代码的问题,这样您就可以看到我做了什么。在这里,我初始化了我发现的连接,它是这样写的:NSURLConnection*connection=[NSURLConnection connectionWithRequest:request delegate:self]代码>所以我设置了委托