Sharing data between an iOS 8 share extension and main app

后端 未结 8 463
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 20:08

Recently, I\'ve been making a simple iOS 8 share extension to understand how the system works. As Apple states in its App Extension Programming Guide:

<
相关标签:
8条回答
  • 2020-11-28 20:54

    You may share data by Following below steps:

    1) Select your project -> Select Capabilities tab -> Enable App Groups -> Click on '+' -> paste your bundle Id after 'group.'

    2) Select your Extension -> Select Capabilities tab -> Enable App Groups -> Click on '+' -> paste your bundle Id after 'group.'

    3) Place below code in your main app for which data you want to share:

    NSUserDefaults * appGroupData = [[NSUserDefaults alloc]initWithSuiteName:@"group.com.appname"];
    NSData * data = [NSKeyedArchiver archivedDataWithRootObject:[self allData]]; // Get my array which I need to share
    [appGroupData setObject:data forKey:@"Data"];
    [appGroupData synchronize];
    

    4) You may get object in extension:

    NSUserDefaults * appGroupData = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.appname"];
    NSData * data = [appGroupData dataForKey:@"Data"];
    NSArray * arrReceivedData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    
    0 讨论(0)
  • 2020-11-28 21:00

    You should use NSUserDefaults like this following and make sure you must have enabled app group in your provisional profile and app group must configure as a green symbol and it should add to your provisional profile & BundleID.

    NSUserDefaults *sharedUserDefault = [[NSUserDefaults alloc] initWithSuiteName:@"group.yougroup"];
    [sharedUserDefault setObject:object forKey:@"yourkey"];
    [sharedUserDefault synchronize];
    
    NSUserDefaults *sharedUserDefault = [[NSUserDefaults alloc] initWithSuiteName:@"group.yougroup"];
    sharedUserDefault value = [sharedUserDefault valueForKey:@"yourkey"];
    
    0 讨论(0)
提交回复
热议问题