【问题标题】:ios - How to get access token?ios - 如何获取访问令牌?
【发布时间】:2013-04-26 06:57:34
【问题描述】:

我正在关注this link 以获取访问令牌,但在下面的代码中有些泄漏,尤其是在 url 和内容中。

 -(void)connectionDidFinishLoading:(NSURLConnection *)connection

 {

  if(_data)
  {
    NSString* content = [[NSString alloc] initWithData:_data
                                              encoding:NSUTF8StringEncoding];

    [_data release];
    _data = nil;

    NSString *jsString = @"<script type='text/javascript'>\
    window.external =\
    {\
    'Notify': function(s) { document.location = 'acs://settoken?token=' + s; },\
    'notify': function(s) { document.location = 'acs://settoken?token=' + s; }\
    }\
    </script>";

   //Here appending the above javascript with content

    content = [jsString stringByAppendingString:content];

    NSURL *url = [[NSURL alloc] initWithString:@"https://converse.accesscontrol"];

    [webView loadHTMLString:content baseURL:url];
   }
   }

登录到 Gmail 或 Yahoo 时,将触发以下代码然后检查 url,但在这里(如果条件失败)。如果您无法理解,我想问什么。请参阅上面给出的链接。

 - (BOOL)webView:(UIWebView *)webView
  shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType

{

_url = [[NSURL alloc] initWithString:@"https://converse.accesscontrol.windows"];

  if(_url)
  {

    if([_url isEqual:[request URL]])
    {
        return YES;
    }

    [_url release];
  }

  _url = [[request URL] retain];
   NSString* scheme = [_url scheme];

  if([scheme isEqualToString:@"acs"])
   {
    // parse the JSON URL parameter into a dictionary
    NSDictionary* pairs = [self parsePairs:[_url absoluteString]];
    if(pairs)
    {
        WACloudAccessToken* accessToken;
        accessToken = [[WACloudAccessToken alloc] initWithDictionary:pairs];
        [WACloudAccessControlClient setToken:accessToken];

        [self dismissModalViewControllerAnimated:YES];
    }

    return NO;
   }

   [NSURLConnection connectionWithRequest:request delegate:self];

   return NO;

  }

有什么想法吗?提前致谢。

【问题讨论】:

    标签: ios objective-c access-token


    【解决方案1】:

    您已成功获得设备令牌.....

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        
        
      
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
       [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    
    }
    
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
        
        self.strToken = [[[[deviceToken description]
                           stringByReplacingOccurrencesOfString: @"<" withString: @""]
                          stringByReplacingOccurrencesOfString: @">" withString: @""]
                         stringByReplacingOccurrencesOfString: @" " withString: @""];
        
        NSLog(@"%@",deviceToken);
    }
    
    
    - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
        NSString *str = [NSString stringWithFormat: @"Error: %@", error];
       
    }
    
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
        
        NSLog(@"user info  %@",userInfo);
        UIApplicationState state = [application applicationState];
        if (state == UIApplicationStateActive) {
            NSString *cancelTitle = @"OK";
            //NSString *showTitle = @"Show";
            NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
                                                                message:message
                                                               delegate:self
                                                      cancelButtonTitle:cancelTitle
                                                      otherButtonTitles: nil];
            [alertView show];
            [alertView release];
            
            
        } 
       else {
            //Do stuff that you would do if the application was not active
        }
    }
    

    【讨论】:

    • 感谢您的回复,为什么我们需要这个设备,我走对了吗?
    • 您在Safari中加载内容的代码,获取令牌太复杂了,我正在尝试在UIWebview中加载
    猜你喜欢
    • 2011-08-26
    • 1970-01-01
    • 2022-01-07
    • 2012-02-15
    • 2012-02-28
    • 2012-10-09
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    相关资源
    最近更新 更多