【问题标题】:ios - How to get access token from Google,Yahoo?ios - 如何从 Google、Yahoo 获取访问令牌?
【发布时间】:2013-05-08 20:39:07
【问题描述】:

我是 iPhone 开发的新手,我正在尝试获取访问令牌,我必须及时完成该任务。我得到了这个链接 http://www.stevesaxon.me/posts/2011/window-external-notify-in-ios-uiwebview/ 来做到这一点,但我无法清楚地理解。谁能帮帮我?

如何在下面的代码中正确调用给定的javascript?

<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>

- (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>";

    NSString *result = [webView stringByEvaluatingJavaScriptFromString: jsString];

    content = [content stringByAppendingString:result];

    [webView loadHTMLString:content baseURL:_url];
    }

}

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

 {

 if(_url)
 {           
if([_url isEqual:[request URL]])
{
    return YES;
}       
[_url release];
}
 //Here am getting http://LoginSuccess.aspx
_url = [[request URL] retain];
NSString* scheme = [_url scheme];//Here am getting http

//So here condition fails

if([scheme isEqualToString:@"acs"])
{
// parse the JSON URL parameter into a dictionary
_url = [NSURL URLWithString:@"https://converse.accesscontrol.windows"];
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;

}

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

【问题讨论】:

    标签: iphone ios objective-c


    【解决方案1】:

    试试这个:-

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
    
        if(_data)
    
        {
            NSString* content = [[NSString alloc] initWithData:_data
                                                      encoding:NSUTF8StringEncoding];
            [_data release];
            _data = nil;
    
            NSString* Html = @" <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 to call that javascript
    
            NSString *result = [_webView stringByEvaluatingJavaScriptFromString:Html];
    
            content = [ScriptNotify stringByAppendingString:result];
            [webView loadHTMLString:content baseURL:_url];
        }
    
    }
    

    【讨论】:

    • 感谢您宝贵的回复 Deephesh,但我在 NSString* Html 中收到了预期错误。您能纠正并发布吗?
    • 它工作正常,但如果上述代码中的条件失败(更新)。请仔细阅读,我认为 javascript 没有正确附加 Html 内容
    • 请检查 _url 并打印 _url
    猜你喜欢
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 2019-03-03
    • 2012-02-28
    • 2012-10-09
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多