XMPPFramework委托函数在模拟器中调用,但不在手机上调用

时间:2013-03-06 22:13:56

标签: ios objective-c xcode ejabberd xmppframework

我正在关注教程: http://mobile.tutsplus.com/tutorials/iphone/building-a-jabber-client-for-ios-server-setup/使用ejabberd服务器设置iOS应用。到目前为止,我已经将代码复制到一个新项目中。

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

  - (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

2 个答案:

答案 0 :(得分:8)

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

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

<强>更新

我刚刚阅读了本教程,并且在描述它如何在真实设备上运行方面做得不好。

答案 1 :(得分:0)

正如Mike D所说,你正在连接到你机器上的服务器([xmppStream setHostName:@&#34; localhost&#34;];) 为了连接到&#34; localhost&#34;您必须更改设备上的主机文件(/ etc / hosts),但Apple禁止这样做,因为您的应用程序无法更改沙箱外的内容。(除非设备已越狱)。 我在Android手机上做过这个东西,过去遇到过类似的问题。 请查看此讨论(Does hosts file exist on the iPhone? How to change it?