UIPastboard在iOS 7中不能正常工作吗? [英] UIPastboard is not working in iOS 7?

查看:125
本文介绍了UIPastboard在iOS 7中不能正常工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个iOS应用程序,其中我必须在两个应用程序之间共享一些数据,并且我正在使用UIPastboard,并且数据在7.0以下的iOS版本中成功共享,但是在iOS 7中却无法运行.这是我的代码使用:

I am working on a iOS application in which I have to share some data between two applications and I am using UIPastboard and data is sharing successfully in iOS below 7.0 but not working in iOS 7. Here is the code I am using:

//编写代码....在应用程序"A"中使用

// Code for writing .... Used in application "A"

NSString * name = @"peter";
NSNumber *age = [NSNumber numberWithInteger:[@"33" integerValue]];

NSMutableDictionary * dict =[[NSMutableDictionary alloc]init];
[dict setObject:name forKey:@"name"];
[dict setObject:age forKey:@"age"];

UIPasteboard * pb = [UIPasteboard pasteboardWithName:@"mypasteboard" create:YES];
[pb setPersistent:YES];
[pb setData:[NSKeyedArchiver archivedDataWithRootObject:dict] forPasteboardType:@"mydata"];

//用于读取的代码.....在应用程序"B"中使用

//Code for reading..... Used in application "B"

 UIPasteboard * pb=[UIPasteboard pasteboardWithName:@"mypasteboard" create:NO];
    NSData * data=[[NSData alloc] init];


    data=[pb valueForPasteboardType:@"mydata"];
    NSDictionary * dict;

    if (!data) {
        return nil;
    }
    @try {
        dict = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    }
    @catch (NSException* exception)
    {
        NSLog(@"Exception: %@",exception);
        return nil;
    }


         if(dict)  //In iOS 7 dict contains 'nil' but not in iOS 6.
       {
           NSString * name = [dict objectForKey:@"name"];
           NSNumber * age = [dict objectForKey:@"age"];
           message =[NSString stringWithFormat:@"name =%@,\nage=%@",name,age];
       }
       else
       {
           message =@"No data from Pasteboard";
       }


        UIAlertView *alertView2 = [[UIAlertView alloc] initWithTitle:@"Pasteboard Data"
                                                             message:message
                                                            delegate:self
                                                   cancelButtonTitle:@"Ok"
                                                   otherButtonTitles:Nil, nil];

        alertView2.alertViewStyle = UIAlertViewStyleDefault;
        [alertView2 show];

在iOS 7中,字典包含零",而在iOS 6中,字典包含在应用程序"A"中设置的数据.

In iOS 7 dict contains 'nil' but in iOS 6 contains data which is set in application 'A'.

请为我建议解决此问题的任何方法.

Please suggest me any solution for this issue.

推荐答案

尝试一下,

UIPasteboard * pb = [UIPasteboard pasteboardWithName:@"mypasteboard" create:YES];
[pb setPersistent:YES];
[pb setValue:[NSKeyedArchiver archivedDataWithRootObject:dict] forPasteboardType:@"mydata"];

这篇关于UIPastboard在iOS 7中不能正常工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆