iOS ShareExtension Couldn't read values in CFPrefsPlistSource

  • ShareExtension中使用app groups NSUserDefaults获取储存的登录状态报错。信息如下:
   NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.xxx.sharextension"];
  if (![userDefaults objectForKey:@"isLogin"])
  {
   UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温馨提示"
                                                                   message:@"请登录"
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定"
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                            UIResponder* responder = self;
                                                            while ((responder = [responder nextResponder]) != nil)
                                                            {
                                                              if([responder respondsToSelector:@selector(openURL:)] == YES)
                                                              {
                                                                [responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:@"sharefile://"]];
                                                              }
                                                            }
                                                          }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * action) {}];
    [alert addAction:cancelAction];
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
  }
[User Defaults] Couldn't read values in CFPrefsPlistSource<0x1c010e340> (Domain:
 group.cn.com.mengniu.oa.sharextension, User: kCFPreferencesAnyUser, ByHost: 
 Yes, Container: (null), Contents Need Refresh: Yes): Using kCFPreferencesAnyUser with a container is only allowed for System Containers, 
 detaching from cfprefsd
  • 网上说在储存App Groups时要添加Team ID。之后取值时虽然不会再报错,但取出来的值为nil。
Change from
[[NSUserDefaults alloc] initWithSuiteName:@"group.com.xxx.xxx"];
to
[[NSUserDefaults alloc] initWithSuiteName:@"nnnnnnnnnn.group.com.xxx.xxx"];
nnnnnnnnn is your team number, the one which you using to sign your code.
Tested under Xcode 8 GM and iOS 10 GM, and worked!

解决办法:用写文件的方法代替,取值时也取文件中的值。

      //获取分组的共享目录
      NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.cn.com.mengniu.oa.sharextension"];   
      NSURL *fileURL = [groupURL URLByAppendingPathComponent:@"login.txt"];
      if (success) {
        resolve(@YES);
        [@"isLogin" writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:nil];
      } else {
        reject(@"101", @"login QuanShi SDK failed", error);
        [@"isNotLogin" writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:Nil];
      }

判断App是否登录,若未登录提示登录,已登录则加载发送框。

  NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.cn.com.mengniu.oa.sharextension"];
  NSURL *fileURL = [groupURL URLByAppendingPathComponent:@"login.txt"];
  NSString *isLoginStatus = [NSString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:nil];
  //如果未登录提示登录
  if (isLoginStatus && [isLoginStatus isEqualToString:@"isNotLogin"]) {
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"请先登录办随,再分享"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定"
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                            UIResponder* responder = self;
                                                            while ((responder = [responder nextResponder]) != nil)
                                                            {
                                                              if([responder respondsToSelector:@selector(openURL:)] == YES)
                                                              {
                                                                [responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:@"sharefile://"]];
                                                              }
                                                              [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
                                                            }
                                                          }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * action) {
                                                           [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
                                                         }];
    [alert addAction:cancelAction];
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
  }
  else//已登录加载发送框
  {
    [self.view addSubview:container];
  }
  • 在Share Extension中展示AlertView。
 UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"请先登录办随,再分享"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定"
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                            UIResponder* responder = self;
                                                            while ((responder = [responder nextResponder]) != nil)
                                                            {
                                                              if([responder respondsToSelector:@selector(openURL:)] == YES)
                                                              {
                                                                [responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:@"sharefile://"]];
                                                              }
                                                            }
                                                          }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * action) {
                                                         
                                                         }];
    [alert addAction:cancelAction];
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];

以上无法弹起AlertView,需要增加返回宿主App方法。 [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"请先登录办随,再分享"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定"
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                            UIResponder* responder = self;
                                                            while ((responder = [responder nextResponder]) != nil)
                                                            {
                                                              if([responder respondsToSelector:@selector(openURL:)] == YES)
                                                              {
                                                                [responder performSelector:@selector(openURL:) withObject:[NSURL URLWithString:@"sharefile://"]];
                                                              }
                                                              [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
                                                            }
                                                          }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * action) {
                                                           [self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
                                                         }];
    [alert addAction:cancelAction];
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];

*从ShareExtension中打开App的方法。因为openUrl只在Today Extension中可以用而在ShareExtension中不起作用。
解决办法:

    NSURL * container = [NSURL URLWithString:@"sharefile://"];
    
    UIResponder* responder = self;
    while ((responder = [responder nextResponder]) != nil)
    {
        if([responder respondsToSelector:@selector(openURL:)] == YES)
        {
            [responder performSelector:@selector(openURL:) withObject:container];
        }
    }
    NSURL *destinationURL = [NSURL URLWithString:@"sharefile://"];
//     Get "UIApplication" class name through ASCII Character codes.
    NSString *className = [[NSString alloc] initWithData:[NSData dataWithBytes:(unsigned char []){0x55, 0x49, 0x41, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E} length:13] encoding:NSASCIIStringEncoding];
    if (NSClassFromString(className)) {
      id object = [NSClassFromString(className) performSelector:@selector(sharedApplication)];
      [object performSelector:@selector(openURL:) withObject:destinationURL];
    }

猜你喜欢

转载自blog.csdn.net/weixin_33779515/article/details/86849328