gpt4 book ai didi

IOS- XMPP - 在登录按钮上进行身份验证和警报 View

转载 作者:行者123 更新时间:2023-11-28 18:57:41 26 4
gpt4 key购买 nike

目前我正在为聊天应用开发 iOS 中的 XMPP 协议(protocol)。我想在登录按钮上放置警报 View 和身份验证。如果身份验证成功,则用户可以看到主屏幕屏幕,否则将出现警报 View 请检查用户名和密码 我展示了安全聊天 开源项目,但我无法理解。

//appdelegate.m file .//
- (BOOL)connect
{
if (![xmppStream isDisconnected]) {
return YES;
// isauthenticate=YES;
}
NSString *myJID = [[NSUserDefaults standardUserDefaults] stringForKey:kXMPPmyJID];
NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:kXMPPmyPassword];

//
// If you don't want to use the Settings view to set the JID,
// uncomment the section below to hard code a JID and password.
//
// myJID = @"user@gmail.com/xmppframework";
// myPassword = @"";

if (myJID == nil || myPassword == nil) {
return NO;
}

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

NSError *error = nil;
if (![xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error])
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error connecting"
message:@"See console for error details."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];

DDLogError(@"Error connecting: %@", error);

return NO;
}

return YES;
}
- (void)disconnect
{
[self goOffline];
[xmppStream disconnect];
}

#pragma mark UIApplicationDelegate

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

DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);

#if TARGET_IPHONE_SIMULATOR
DDLogError(@"The iPhone simulator does not process background network traffic. "
@"Inbound traffic is queued until the keepAliveTimeout:handler: fires.");
#endif

if ([application respondsToSelector:@selector(setKeepAliveTimeout:handler:)])
{
[application setKeepAliveTimeout:600 handler:^{

DDLogVerbose(@"KeepAliveHandler");

// Do other keep alive stuff here.
}];
}
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}

- (void)applicationWillTerminate:(UIApplication *)application
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);

[self teardownStream];
}

#pragma mark XMPPStream Delegate

- (void)xmppStream:(XMPPStream *)sender socketDidConnect:(GCDAsyncSocket *)socket
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}

- (void)xmppStream:(XMPPStream *)sender willSecureWithSettings:(NSMutableDictionary *)settings
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);

NSString *expectedCertName = [xmppStream.myJID domain];
if (expectedCertName)
{
settings[(NSString *) kCFStreamSSLPeerName] = expectedCertName;
}

if (customCertEvaluation)
{
settings[GCDAsyncSocketManuallyEvaluateTrust] = @(YES);
}
}

- (void)xmppStream:(XMPPStream *)sender didReceiveTrust:(SecTrustRef)trust
completionHandler:(void (^)(BOOL shouldTrustPeer))completionHandler
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);

// The delegate method should likely have code similar to this,
// but will presumably perform some extra security code stuff.
// For example, allowing a specific self-signed certificate that is known to the app.

dispatch_queue_t bgQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(bgQueue, ^{

SecTrustResultType result = kSecTrustResultDeny;
OSStatus status = SecTrustEvaluate(trust, &result);

if (status == noErr && (result == kSecTrustResultProceed || result == kSecTrustResultUnspecified)) {
completionHandler(YES);
}
else {
completionHandler(NO);
}
});
}

- (void)xmppStreamDidSecure:(XMPPStream *)sender
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}

- (void)xmppStreamDidConnect:(XMPPStream *)sender
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);


isXmppConnected = YES;

NSError *error = nil;

if (![[self xmppStream] authenticateWithPassword:password error:&error])
{
DDLogError(@"Error authenticating: %@", error);
}
}

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender

{
isauthenticate=YES;

DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);

[self goOnline];
}

- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}

我尝试在 xmppStreamDidAuthenticate 方法中设置 BOOL(isauthenticate) 但没有成功。我可以重定向到主页,但如果我写错了详细信息,它仍然会重定向到主页。如果用户名或密码错误并且未通过服务器验证,我想设置它。

     //view controller.m file //
#import "ViewController.h"
#import "AppDelegate.h"
@interface ViewController ()<MBProgressHUDDelegate>
{
MBProgressHUD *HUD;

IBOutlet UITextField *mViewEmail;
IBOutlet UITextField *mViewPassword;
}

@end

NSString *const kXMPPmyJID = @"kXMPPmyJID";
NSString *const kXMPPmyPassword = @"kXMPPmyPassword";



@implementation ViewController

- (IBAction)checkLogin:(id)sender {
NSLog(@"Email: %@ Password: %@",mViewEmail.text,mViewPassword.text);
[self setField:mViewEmail forKey:kXMPPmyJID];
[self setField:mViewPassword forKey:kXMPPmyPassword];


// if (appdelegate.connect==YES) {


if([ [self appDelegate] connect]) {



// if (appdelegate.isauthenticate==YES) {



HUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES] ;
HUD.delegate = self;
HUD.color = [UIColor blackColor];
HUD.labelText = @"Please Wait";
HUD.dimBackground = YES;
// HUD.detailsLabelText = @"Close chat";


[self showHome];

//}


}
//}
else
{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry"
message:@"Please Check Username or Password"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];

}

}

- (void)showHome{
[self performSegueWithIdentifier:@"signIn" sender:self];
}

最佳答案

好的。所以我的问题终于解决了。我正在发布我的答案。首先我的错误是我忘了写

   appdelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
appdelegate.viewController = self; // very imp when you call method in app delegate.h

在我的 View Controller 中。因此,当我在 appdelegate 中调用 viewcontroller 的方法时,它会调用但由于缺少上述方法而不会执行(第二行)。然后我在 - (void)xmppStreamDidAuthenticate:(XMPPStream *)sender 方法中调用了 View Controller 的 segue 方法。然后完美地工作。所以我最终的解决方案是

    //app delegate.m file//
- (BOOL)connect
{
// Setup HUD(Activity Indicator) when Connect method call //

HUD = [[MBProgressHUD alloc] initWithWindow:[UIApplication sharedApplication].keyWindow]; [self.window.rootViewController.view addSubview:HUD];
[HUD setDetailsLabelText:@"Please wait..."];
[HUD setDimBackground:YES];
[HUD setOpacity:0.5f];
[HUD show:YES];

if (![xmppStream isDisconnected]) {
return YES;
}

NSString *myJID = [[NSUserDefaults standardUserDefaults] stringForKey:kXMPPmyJID];
NSString *myPassword = [[NSUserDefaults standardUserDefaults] stringForKey:kXMPPmyPassword];


if (myJID == nil || myPassword == nil) {
return NO;
}

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

NSError *error = nil;
if (![xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error])
{
HUD.hidden=YES;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error connecting"
message:@"See console for error details."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];

DDLogError(@"Error connecting: %@", error);

return NO;

}

return YES;
}

- (void)disconnect
{
[self goOffline];
[xmppStream disconnect];
}

#pragma mark UIApplicationDelegate

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


DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);

#if TARGET_IPHONE_SIMULATOR
DDLogError(@"The iPhone simulator does not process background network traffic. "
@"Inbound traffic is queued until the keepAliveTimeout:handler: fires.");
#endif

if ([application respondsToSelector:@selector(setKeepAliveTimeout:handler:)])
{
[application setKeepAliveTimeout:600 handler:^{

DDLogVerbose(@"KeepAliveHandler");

// Do other keep alive stuff here.
}];
}
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}

- (void)applicationWillTerminate:(UIApplication *)application
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);

[self teardownStream];
}

#pragma mark XMPPStream Delegate


- (void)xmppStream:(XMPPStream *)sender socketDidConnect:(GCDAsyncSocket *)socket
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}

- (void)xmppStream:(XMPPStream *)sender willSecureWithSettings:(NSMutableDictionary *)settings
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);

NSString *expectedCertName = [xmppStream.myJID domain];
if (expectedCertName)
{
settings[(NSString *) kCFStreamSSLPeerName] = expectedCertName;
}

if (customCertEvaluation)
{
settings[GCDAsyncSocketManuallyEvaluateTrust] = @(YES);
}
}


- (void)xmppStream:(XMPPStream *)sender didReceiveTrust:(SecTrustRef)trust
completionHandler:(void (^)(BOOL shouldTrustPeer))completionHandler
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);


dispatch_queue_t bgQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(bgQueue, ^{

SecTrustResultType result = kSecTrustResultDeny;
OSStatus status = SecTrustEvaluate(trust, &result);

if (status == noErr && (result == kSecTrustResultProceed || result == kSecTrustResultUnspecified)) {
completionHandler(YES);
}
else {
completionHandler(NO);
}
});
}

- (void)xmppStreamDidSecure:(XMPPStream *)sender
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
}

- (void)xmppStreamDidConnect:(XMPPStream *)sender
{
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);


isXmppConnected = YES;

NSError *error = nil;

if (![[self xmppStream] authenticateWithPassword:password error:&error])
{
DDLogError(@"Error authenticating: %@", error);
}
}

- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender

{
HUD.hidden=YES; //Hud Will be hide when User Authenticated


DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
[self.viewController showHome]; // view controllers method to go to next view controller //

[self goOnline];
}

- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(NSXMLElement *)error
{
// HUD will be hidden and alertview will be shown //

HUD.hidden=YES;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry"
message:@"Please Check Username or Password"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];


isauthenticate=NO;
DDLogVerbose(@"%@: %@", THIS_FILE, THIS_METHOD);
{
NSLog(@"error = %@", error);
}

}

并且在 viewcontroller.m 中我们展示了 viewcontroller 也是 appdelegate Controller 。所以我们必须在 - (void)viewDidLoad 方法中声明它。

// viewcontroller.m file//
- (void)viewDidLoad
{
[super viewDidLoad];
appdelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
appdelegate.viewController = self;
}

- (AppDelegate *)appDelegate
{
return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}

- (IBAction)checkLogin:(id)sender {
[self dismissKeyboard];
NSLog(@"Email: %@ Password: %@",mViewEmail.text,mViewPassword.text);
[self setField:mViewEmail forKey:kXMPPmyJID];
[self setField:mViewPassword forKey:kXMPPmyPassword];

[[self appDelegate ]connect];//call when loginbutton pressed

}

//call in appdelegate.h // segue(push) to next view

- (void)showHome
{
[self performSegueWithIdentifier:@"signIn" sender:self];
}

注意:我已尽力解释。

关于IOS- XMPP - 在登录按钮上进行身份验证和警报 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30522542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com