Sharing data between an iOS 8 share extension and main app

后端 未结 8 500
爱一瞬间的悲伤
爱一瞬间的悲伤 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];
    

提交回复
热议问题