程序间的通信

程序见的通信,是对于两个程序间的传送数据的操作.下面是在AppDelegate里实现的方法,总共建立两个工程 步骤一模一样.
考虑到有的童鞋对xcode接触不久,我将整个.m文件全部复制了,对照者打就可以了.

记得实现的时候先让两个都运行一遍

import “AppDelegate.h”

@interface AppDelegate ()

@property (nonatomic , retain) UITextField *field;

@property (nonatomic , retain) UILabel *label;

@end

@implementation AppDelegate

  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 400, 100, 30);
    button.backgroundColor = [UIColor redColor];
    [self.window addSubview:button];

    self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 300, 222, 30)];
    self.label.backgroundColor = [UIColor grayColor];
    [self.window addSubview:self.label];

    self.field = [[UITextField alloc] initWithFrame:CGRectMake(100, 200, 222, 30)];
    self.field.backgroundColor = [UIColor colorWithRed:0.468 green:0.500 blue:0.470 alpha:1.000];
    self.field.placeholder = @”shuruxinxi”;
    [self.window addSubview:self.field];

    [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];

    return YES;
    }

  • (void)button:(UIButton *)button
    {
    NSString *string = [self.field.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    这里就是我在下面改的东西.用它找到另一个工程,进行通信(BB就是另一个工程的,这个工程我起名叫AA)

    NSString *str = [NSString stringWithFormat:@”BB://%@”,string];

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:str]]) {

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    

    }
    else{

    NSLog(@"faile");
    

    }

}

  • (BOOL)application:(UIApplication )application openURL:(NSURL )url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    {
    self.label.text = [[url host] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    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.
    [self saveContext];
    }

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.Try.__” in the application’s documents directory.
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains: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 = [[NSBundle mainBundle] URLForResource:@”__” withExtension:@”momd”];
    _managedObjectModel = [[NSManagedObjectModel alloc] 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 = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@”__.sqlite”];
    NSError *error = nil;
    NSString *failureReason = @”There was an error creating or loading the application’s saved data.”;
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
    // Report any error we got.
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSLocalizedDescriptionKey] = @”Failed to initialize the application’s saved data”;
    dict[NSLocalizedFailureReasonErrorKey] = failureReason;
    dict[NSUnderlyingErrorKey] = error;
    error = [NSError errorWithDomain:@”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, [error userInfo]);
    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 = [self persistentStoreCoordinator];
    if (!coordinator) {
    return nil;
    }
    _managedObjectContext = [[NSManagedObjectContext alloc] init];
    [_managedObjectContext setPersistentStoreCoordinator: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, [error userInfo]);
    abort();
    }
    }
    }

@end

完成以上代码还有一个关键地方

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值