3

我正在按照以下教程进行操作:http: //mobile.tutsplus.com/tutorials/iphone/building-a-jabber-client-for-ios-server-setup/使用 ejabberd 服务器设置 iOS 应用程序。到目前为止,我几乎已经将代码复制到了一个新项目中。

我的问题是 XMPP 委托函数 AppDelegate.m 在手机上运行时没有被调用。在模拟器中一切正常,下面的两个函数被调用。

  - (void)xmppStreamDidConnect:(XMPPStream *)sender {
    NSLog(@"in WSAppDelegate - xmppStreamDidConnect");
    isOpen = YES;
    NSError *error = nil;
    [[self xmppStream] authenticateWithPassword:password error:&error];

}

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
    NSLog(@"in WSAppDelegate - xmppStreamDidAuthenticate");

    [self goOnline];

}

我可以在电话和模拟器上连接,因为此呼叫运行没有错误:

[xmppStream connect:&error]

这是我的 AppDelegate.h 代码:

#import <UIKit/UIKit.h>
#import "XMPPRoster.h"
#import "XMPP.h"
#import "SMChatDelegate.h"
#import "SMMessageDelegate.h"

@class SMBuddyListViewController;

@interface WSAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    SMBuddyListViewController *viewController;

    XMPPStream *xmppStream;
    XMPPRoster *xmppRoster;

    NSString *password;

    BOOL isOpen;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet SMBuddyListViewController *viewController;


@property (nonatomic, readonly) XMPPStream *xmppStream;
@property (nonatomic, readonly) XMPPRoster *xmppRoster;

@property (nonatomic, assign) id  _chatDelegate;
@property (nonatomic, assign) id  _messageDelegate;

- (BOOL)connect;
- (void)disconnect;

@end

和 AppDelegate.m:

#import "WSBuddyListViewController.h"


@interface WSAppDelegate()

- (void)setupStream;

- (void)goOnline;
- (void)goOffline;

@end


@implementation WSAppDelegate

@synthesize xmppStream;
@synthesize xmppRoster;
@synthesize window;
@synthesize viewController;
@synthesize _chatDelegate;
@synthesize _messageDelegate;


- (void)applicationWillResignActive:(UIApplication *)application {

    [self disconnect];

}

- (void)applicationDidBecomeActive:(UIApplication *)application {

    [self setupStream];
    BOOL connected = NO;
    connected = [self connect];
    NSLog(@"*** connected = %i", connected);


}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    return YES;
}


- (void)setupStream {
    NSLog(@"in WSAppDelegate - setupStream");

    xmppStream = [[XMPPStream alloc] init];
    [xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
    [xmppStream setHostName:@"localhost"];

}

- (void)goOnline {
    NSLog(@"in WSAppDelegate - goOnline");

    XMPPPresence *presence = [XMPPPresence presence];
    [[self xmppStream] sendElement:presence];
}

- (void)goOffline {
    XMPPPresence *presence = [XMPPPresence presenceWithType:@"unavailable"];
    [[self xmppStream] sendElement:presence];
}

- (BOOL)connect {
    NSLog(@"in WSAppDelegate - connect");

    [self setupStream];

    NSString *jabberID = [[NSUserDefaults standardUserDefaults] stringForKey:@"userID"];
    NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:@"userPassword"];

    if (![xmppStream isDisconnected]) {
        NSLog(@"in WSAppDelegate - connect - if (![xmppStream isDisconnected]) ");

        return YES;
    }


    if (jabberID == nil || myPassword == nil) {
        NSLog(@"in WSAppDelegate - connect - if (jabberID == nil || myPassword == nil)");

        return NO;
    }

    [xmppStream setMyJID:[XMPPJID jidWithString:jabberID]];
    password = myPassword;

    NSError *error = nil;
    if (![xmppStream connect:&error])
    {
        NSLog(@"in WSAppDelegate - connect - if (![xmppStream connect:&error]))");

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                            message:[NSString stringWithFormat:@"Can't connect to server %@", [error localizedDescription]]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];


        return NO;
    }

    return YES;
}

- (void)disconnect {

    [self goOffline];
    [xmppStream disconnect];
    [_chatDelegate didDisconnect];
}



#pragma mark -
#pragma mark XMPP delegates


- (void)xmppStreamDidConnect:(XMPPStream *)sender {
    NSLog(@"in WSAppDelegate - xmppStreamDidConnect");
    isOpen = YES;
    NSError *error = nil;
    [[self xmppStream] authenticateWithPassword:password error:&error];

}

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender {
    NSLog(@"in WSAppDelegate - xmppStreamDidAuthenticate");

    [self goOnline];

}


- (BOOL)xmppStream:(XMPPStream *)sender didReceiveIQ:(XMPPIQ *)iq {

    return NO;

}

- (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message {
    NSLog(@"in WSAppDelegate - xmppStream:(XMPPStream *)sender didReceiveMessage");


    NSString *msg = [[message elementForName:@"body"] stringValue];
    NSString *from = [[message attributeForName:@"from"] stringValue];

    NSMutableDictionary *m = [[NSMutableDictionary alloc] init];
    [m setObject:msg forKey:@"msg"];
    [m setObject:from forKey:@"sender"];

    [_messageDelegate newMessageReceived:m];

}

- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence {
    NSLog(@"in WSAppDelegate - xmppStream:(XMPPStream *)sender didReceivePresence:");

    NSString *presenceType = [presence type]; // online/offline
    NSString *myUsername = [[sender myJID] user];
    NSString *presenceFromUser = [[presence from] user];

    if (![presenceFromUser isEqualToString:myUsername]) {

        if ([presenceType isEqualToString:@"available"]) {

            [_chatDelegate newBuddyOnline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"localhost"]];

        } else if ([presenceType isEqualToString:@"unavailable"]) {

            [_chatDelegate buddyWentOffline:[NSString stringWithFormat:@"%@@%@", presenceFromUser, @"localhost"]];

        }

    }

}


- (void)dealloc {

    [xmppStream removeDelegate:self];
    [xmppRoster removeDelegate:self];

    [xmppStream disconnect];
}
@end
4

2 回答 2

8

如果您查看您的setupStream方法,您使用的名称是“localhost”。这让我相信服务器在您的开发机器上,并且设备正在尝试连接到自身(本地主机)。您必须将其替换为您的服务器名称。

这可能在模拟器中有效,因为客户端和服务器是一体的。

更新

我刚刚通读了教程,它在描述它在真实设备上的工作方式方面做得并不好。

于 2013-03-07T01:02:16.617 回答
0

正如 Mike D 所说,您正在连接到您机器上的服务器([xmppStream setHostName:@"localhost"];) 为了连接到“localhost”,您必须更改设备上的主机文件(/etc /hosts),但这是 Apple 禁止的,因为您的应用程序无法更改沙箱之外的内容。(除非设备已越狱)。过去遇到类似问题时,我在安卓手机上做过这些事情。检查这个讨论(iPhone上是否存在hosts文件?如何更改它?

于 2014-10-14T12:38:43.080 回答