2014-07-23 63 views
0

我正在开发一个iOS应用程序,我必须在两个应用程序之间共享一些数据,我正在使用UIPastboard,数据在iOS 7.0以下的iOS中成功共享,但不能在iOS 7中使用。下面是我使用的代码:UIPastboard不适用于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 [email protected]"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在应用程序'A'中设置。

请为我提出解决此问题的任何解决方案。

回答

0

试试这个,

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

此代码也没有工作:( –

+0

这不是没有归档的代码工作? –

+0

尝试,即通过直接传递的字典.. – NKB

相关问题