关闭

iOS 网络编程 XMPP编程 创建 连接 发送口令 在线状态 等初步操作

标签: 网络编程ios
242人阅读 评论(0) 收藏 举报
分类:

//

//  AppDelegate.m

//  0708xcode6.3-01

//

//  Created by tianshangrenjian on 15/7/8.

//  Copyright (c) 2015 tianshangrenjian. All rights reserved.

//


#import "AppDelegate.h"


@interface AppDelegate ()<XMPPStreamDelegate>


@end


@implementation AppDelegate


- (void)createMPPStream

{

   XMPPStream *stream=[[XMPPStreamalloc] init];

   self.stream=stream;

    [stream addDelegate:selfdelegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)];

    

}

- (void)connectToServer

{

    [selfcreateMPPStream];

    XMPPJID *myJID=[XMPPJIDjidWithUser:@"test3"domain:@"tianshangrenjians-MacBook-Air.local"resource:@"iphone"];

   self.stream.myJID=myJID;

    self.stream.hostName=@"tianshangrenjians-MacBook-Air.local";

    [self.streamconnectWithTimeout:-1error:nil];

}

- (void)xmppStream:(XMPPStream *)sender didFailToSendPresence:(XMPPPresence *)presence error:(NSError *)error

{

    NSLog(@"on line error %@",error);

}

- (void)xmppStream:(XMPPStream *)sender didSendPresence:(XMPPPresence *)presence

{

    NSLog(@"on line ok");

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)),dispatch_get_main_queue(), ^{

        

       XMPPPresence *presence=[XMPPPresencepresenceWithType:@"unavailable"];

        [self.streamsendElement:presence];

        

    });

}

- (void)xmppStreamDidConnect:(XMPPStream *)sender

{

    NSLog(@"connect ok");

    [selfsendPwdToServer];

}


- (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error

{

   NSLog(@"cao:%@",error);

}

- (void)sendPwdToServer

{

    [self.streamauthenticateWithPassword:@"1"error:nil];

}


- (void)xmppStreamDidAuthenticate:(XMPPStream *)sender

{

    NSLog(@"login ok!");

    [selfsendOnLineToServer];

    

    

}

- (void)sendOnLineToServer

{

    NSLog(@"send ok");

    XMPPPresence *presence=[XMPPPresencepresence];

    [self.streamsendElement:presence];

}

- (void)xmppStream:(XMPPStream *)sender didNotAuthenticate:(DDXMLElement *)error

{

    NSLog(@"long failed!  %@",error);

}

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

    // Override point for customization after application launch.

    

   // [self createMPPStream];

    [selfconnectToServer];

   // [self sendPwdToServer];

    

    return YES;

}


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

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


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

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}


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

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}


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

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}


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

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

    // Saves changes in the application's managed object context before the application terminates.

    [selfsaveContext];

}


#pragma mark - Core Data stack


@synthesize managedObjectContext =_managedObjectContext;

@synthesize managedObjectModel =_managedObjectModel;

@synthesize persistentStoreCoordinator =_persistentStoreCoordinator;


- (NSURL *)applicationDocumentsDirectory {

    // The directory the application uses to store the Core Data store file. This code uses a directory named "com.yy._708xcode6_3_01" in the application's documents directory.

    return [[[NSFileManagerdefaultManager] URLsForDirectory:NSDocumentDirectoryinDomains:NSUserDomainMask]lastObject];

}


- (NSManagedObjectModel *)managedObjectModel {

    // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.

    if (_managedObjectModel !=nil) {

        return_managedObjectModel;

    }

    NSURL *modelURL = [[NSBundlemainBundle] URLForResource:@"_708xcode6_3_01"withExtension:@"momd"];

    _managedObjectModel = [[NSManagedObjectModelalloc] initWithContentsOfURL:modelURL];

    return_managedObjectModel;

}


- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.

    if (_persistentStoreCoordinator !=nil) {

        return_persistentStoreCoordinator;

    }

    

    // Create the coordinator and store

    

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinatoralloc] initWithManagedObjectModel:[selfmanagedObjectModel]];

    NSURL *storeURL = [[selfapplicationDocumentsDirectory] URLByAppendingPathComponent:@"_708xcode6_3_01.sqlite"];

   NSError *error = nil;

    NSString *failureReason =@"There was an error creating or loading the application's saved data.";

    if (![_persistentStoreCoordinatoraddPersistentStoreWithType:NSSQLiteStoreTypeconfiguration:nilURL:storeURL options:nilerror:&error]) {

        // Report any error we got.

        NSMutableDictionary *dict = [NSMutableDictionarydictionary];

        dict[NSLocalizedDescriptionKey] =@"Failed to initialize the application's saved data";

        dict[NSLocalizedFailureReasonErrorKey] = failureReason;

        dict[NSUnderlyingErrorKey] = error;

        error = [NSErrorerrorWithDomain:@"YOUR_ERROR_DOMAIN"code:9999 userInfo:dict];

        // Replace this with code to handle the error appropriately.

        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

       NSLog(@"Unresolved error %@, %@", error, [erroruserInfo]);

       abort();

    }

    

    return_persistentStoreCoordinator;

}



- (NSManagedObjectContext *)managedObjectContext {

    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)

    if (_managedObjectContext !=nil) {

        return_managedObjectContext;

    }

    

    NSPersistentStoreCoordinator *coordinator = [selfpersistentStoreCoordinator];

   if (!coordinator) {

       return nil;

    }

    _managedObjectContext = [[NSManagedObjectContextalloc] init];

    [_managedObjectContextsetPersistentStoreCoordinator:coordinator];

    return_managedObjectContext;

}


#pragma mark - Core Data Saving support


- (void)saveContext {

   NSManagedObjectContext *managedObjectContext =self.managedObjectContext;

   if (managedObjectContext != nil) {

       NSError *error = nil;

       if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {

            // Replace this implementation with code to handle the error appropriately.

            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

           NSLog(@"Unresolved error %@, %@", error, [erroruserInfo]);

           abort();

        }

    }

}


@end


0
0

猜你在找
【套餐】Hadoop生态系统零基础入门
【套餐】嵌入式Linux C编程基础
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【套餐】深度学习入门视频课程——唐宇迪
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】计算机视觉原理及实战——屈教授
【直播】机器学习之凸优化——马博士
【直播】机器学习&数据挖掘7周实训--韦玮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:205272次
    • 积分:4192
    • 等级:
    • 排名:第7286名
    • 原创:222篇
    • 转载:32篇
    • 译文:0篇
    • 评论:31条
    文章分类
    最新评论