- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个应用程序来帮助我了解 OBJECTIVE-X/OSX。
该应用只需连接到 Facebook 并使用 NSUserNotification
发送通知。它工作正常,但现在我想添加一些 UI。
为了使示例更简单,我想更新标签 (NSTextField
) 以显示 Facebook 连接的状态。
正在连接...
已连接
失败
我在一个文件 FacebookRequest.m
- (void) connectFacebook{
if(self.account == nil){
self.account = [[ACAccountStore alloc]init];
}
ACAccountType *facebookAccount = [self.account
accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options = @{
ACFacebookAppIdKey: @"MY_CODE",
ACFacebookPermissionsKey: @[@"email",
@"user_about_me",
@"user_likes",
@"manage_notifications",
@"user_activities"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[self.account requestAccessToAccountsWithType:facebookAccount
options:options
completion:^(BOOL success, NSError *error){
if(success){
NSArray *accounts = [self.account accountsWithAccountType:facebookAccount];
self.account = [accounts lastObject];
}
else{
NSLog(@"Erro %@", [error description]);
}
}];
}
以及我的AppDelegate.m
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self.statusFacebook setStringValue:@"Connecting…"];
FacebookRequest *request = [[FacebookRequest alloc]init];
[request connectFacebook];
}
请求完成并且我拥有帐户后更新 UI 的最佳方式是什么?
我遇到了麻烦,因为请求是异步的,并且我无法在 requestAccessToAccountsWithType
block 内返回任何值。另一点是,如果我在后面添加一些“if”来检查我的帐户是否为零,那么它将在 block 执行完毕之前执行,因此帐户仍然是nil
。
谢谢!
PS:如果英文不够清晰,请见谅。
最佳答案
您可以使用 NSNotificationCenter
来实现此目的:
[self.account requestAccessToAccountsWithType:facebookAccount
options:options
completion:^(BOOL success, NSError *error){
if(success){
NSArray *accounts = [self.account accountsWithAccountType:facebookAccount];
self.account = [accounts lastObject];
// You post a notification that the UI should update here
[[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateUI" object:nil];
}
else{
NSLog(@"Erro %@", [error description]);
}
}];
然后,您添加应该更新其 UI 的 viewController 作为此通知的观察者:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUI) name:@"UpdateUI" object:nil];
}
- (void)updateUI {
// Here you actually update your UI
}
附:如果您不使用 arc,您还可以在 dealloc 中删除观察者:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
关于facebook - requestAccessToAccountsWithType 后更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16410546/
我正在开发一个应用程序来帮助我了解 OBJECTIVE-X/OSX。 该应用只需连接到 Facebook 并使用 NSUserNotification 发送通知。它工作正常,但现在我想添加一些 UI。
据我了解,当我调用 [ACAccountStore requestAccessToAccountsWithType:options:completion] 时,用户应该会看到一个 UIAlert,询问
当我运行 [_accountStore requestAccessToAccountsWithType: ...] 时,我没有收到任何响应。我的完成处理程序没有被调用。但是我确实可以访问 Twitte
我正在使用此代码连接到 Facebook。它是第一次工作。然后我进入我的设置应用程序并撤销权限。然后我回到我的应用程序并再次运行此代码。 granted 返回 NO 但 error 不包含任何数据,我
-(void)getTwittersFollowers{ ACAccountStore *store=[[ACAccountStore alloc]init]; ACAccountType *twit
我一直在搜索 Google 和 SO,但找不到 com.apple.accounts 错误代码 8 的含义。我正在尝试使用 iOS 6 和 Facebook SDK。 我运行这个请求; if (!
我是一名优秀的程序员,十分优秀!