我想触发iOS7询问用户是否允许使用蓝牙和Twitter帐户

Coc*_*ica 3 bluetooth ios core-bluetooth cbperipheral ios7

这有点落后于大多数人的要求。我想故意触发对话框,请求用户许可:

  • 即使在离线状态下也可以连接到蓝牙设备。
  • 访问基于 iOS 的 Twitter 帐户。

我已经有一个类似的位置对话框,工作正常。我这样做是因为我想让请求许可的过程变得更加温和,就像 Heyday 一样,通过显示一个欢迎屏幕来解释为什么应用程序需要此服务,然后当用户点击“确定”时,启动请求并触发对话框。

我已经尝试过一些事情了。对于 Twitter,我尝试了以下方法:

- (void)triggerTwitterApprovalWithCompletion:(void (^)(BOOL, NSError *))completion  {
  self.accountStore = [[ACAccountStore alloc] init]; //setup as a property.
  ACAccountType *twitterAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
  [self.accountStore requestAccessToAccountsWithType:twitterAccountType  options:nil completion:^(BOOL granted, NSError *error) { //completion handling is on indeterminate queue so I force main queue inside
    dispatch_async(dispatch_get_main_queue(), ^{
      completion(granted, error); //triggers displaying the next screen, working
    });
  }];
}
Run Code Online (Sandbox Code Playgroud)

但是,尽管授予了权限,但它是自动的,而不是向用户显示对话框并批准它的结果。

对于蓝牙版本,我还没有找到一些可以触发它的代码。文档和编程指南在请求权限的问题上都相对安静。我能找到的唯一一个参考是,通过为bluetooth-peripheral用户提供 plist 密钥,将在应用程序启动时自动提示。这在流程中还为时过早,没有什么用处。

all*_*rog 5

只有外围角色需要用户权限才能运行。中央可以为所欲为。当您希望用户授予权限时,只需实例化 aCBPeripheralManager即可触发对话框。

pmanager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
Run Code Online (Sandbox Code Playgroud)

然后,您使用peripheralManagerDidUpdateState:委托方法来了解用户的决定。bluetooth-peripheral如果您的应用程序不在后台运行,则无需在 plist 中设置后台模式。

请记住,该对话框在应用程序的生命周期内只会弹出一次,并且删除并重新安装应用程序时不会重置该设置。

另请注意,鉴于这种行为,

我能找到的唯一一个参考是,通过蓝牙外设的 plist 密钥,将在应用程序启动时自动提示用户。

事实并非如此。

对于推特部分。我还没有尝试使用该 API,但发现了几个答案,这些答案表明授权要么自动为您处理,要么 iOS 只会在第一次询问。


Settings要测试弹出问题,您应该手动重置-> General-> Reset->中的设置Reset Location & Privacy

只是一个小补充:请求用户许可的最佳实践