objective-c - iOS 和 Objective-C : How to keep an object globally

标签 objective-c ios global-variables oauth-2.0

为 iOS 开发应用程序时,我需要知道如何在用户验证时实例化和使用创建的对象。

我正在使用 OAuth2 方法正确实现 gtm-oauth2 框架。用户输入,查看 Web View 中显示的登录表单并正确进行身份验证。在那一刻,如文档中所述,我是这样的:

if (error != nil){
    // Do whatever to control the error
}
else
{
    // Authentication succeeded

    // Assign the access token to the instance property for later use
    self.accessToken = myAuth.accessToken;
    [myAuth setShouldAuthorizeAllRequests:YES];
    [self setAuth:myAuth];

    // Display the access token to the user
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Authorization Succeeded"
                                                        message:[NSString stringWithFormat:@"Access Token: %@", myAuth.accessToken]
                                                       delegate:self
                                              cancelButtonTitle:@"Dismiss"
                                              otherButtonTitles:nil];
    [alertView show];
}

稍后,在同一个 Controller 中,我使用这样的 self.auth 对象在用户通过身份验证后访问我的 API:

[request setURL:getCartsURL];
[request setValue:self.accessToken forHTTPHeaderField:@"Authorization"];
[self.auth authorizeRequest:request
          completionHandler:^(NSError *error) {
              NSString *output = nil;
              if (error) {
                  output = [error description];
              } else {
                  // Synchronous fetches like this are a really bad idea in Cocoa applications
                  //
                  // For a very easy async alternative, we could use GTMHTTPFetcher
                  NSURLResponse *response = nil;
                  NSData *data = [NSURLConnection sendSynchronousRequest:request
                                                       returningResponse:&response
                                                                   error:&error];

                  if (data) {
                      // API fetch succeeded
                      output = [[NSString alloc] initWithData:data
                                                     encoding:NSUTF8StringEncoding];

                      SBJsonParser *jsonParser = [SBJsonParser new];

                      // Parse the JSON into an Object
                      id parsed = [jsonParser objectWithString:output];

                      NSArray *arrayResponse = [[NSArray alloc] initWithArray:parsed];

                  } else {
                      // fetch failed
                      output = [error description];
                  }
              }
          }];

到目前为止,我一直在使用 self.auth 对象的本地实例,如果我想从整个应用程序的任何位置全局访问该对象,那么这样做是不够的。初始 View Controller 没问题,但不是整个应用程序。

我想我可以以某种方式访问​​第一个 View Controller 以在我需要的任何时候获取对象。但我想我们有更好的方法让它全局实例化并从应用程序的任何位置访问。

你能帮我解决这个问题吗?

非常感谢。

最佳答案

你应该使用单例。 Here是一篇关于如何设置的好文章。

关于objective-c - iOS 和 Objective-C : How to keep an object globally,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12646948/

相关文章:

ios - UIImagePicker 禁用相机缩放

ios - 在 iOS 应用程序中更改部署目标后需要进行哪些更改

objective-c - 创建一个全局类 objective-c?

c++ - 在 c/c++ 中的源文件之间共享变量的最佳策略是什么?

c++ - 在 C++ 中适当使用全局 const 变量?

iPhone 内存不足,奇怪地崩溃

objective-c - 如何在 Swift 中使用带有关键字名称的 Objective-C 类

ios - dyld : Symbol not found: _NSURLAuthenticationMethodClientCertificate when trying to run iOS app

ios - UIViewController 可能不响应 showRootPopoverButtonItem

objective-c - RKObjectLoader使用数组参数截断我的路径