UIPastboard在iOS 7中不起作用?

时间:2014-07-23 07:15:21

标签: ios uipasteboard

我正在开发一个iOS应用程序,我必须在两个应用程序之间共享一些数据,而我正在使用UIPastboard,数据在iOS 7.0以下成功共享,但在iOS 7中无效。这是代码我正在使用:

//写作代码....用于应用程序" 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"

 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中,dict包含' nil'但在iOS 6中包含在应用程序中设置的数据' A'。

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

1 个答案:

答案 0 :(得分:0)

试试这个,

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