ios - 如何通过UIwebView完成用户oauth授权
PHP中文网
PHP中文网 2017-04-17 13:01:55
0
0
322

我用Tumblr SDK for iOS实现了一个Tumblr iOS客户端,但被Apple给拒绝了,其理由是用户授权是通过direct用户在Safari中登录而后跳转回来实现的,破坏了用户体验。

我想授权在webView中完成,不用跳转到Safari中,我看了下TMTumblrSDK的代码,准备将该库中负责authenticate的代码(整个方法的代码如下)中的这句,[[UIApplication sharedApplication] openURL:authURL]; 改为创建UIwebView -> 添加到self.view中,然后[webView loadRequest:[NSURLRequest requestWithURL:authURL]];,但发现在这个第三方库中无法访问到viewController(从而无法self.view addSubview: webView),尝试了通过appDelegate来找到viewController,但也不行,请教这种情况该如何做呢?

- (void)authenticate:(NSString *)URLScheme callback:(TMAuthenticationCallback)callback {
    // Clear token secret in case authentication was previously started but not finished
    self.OAuthTokenSecret = nil;

    NSString *tokenRequestURLString = [NSString stringWithFormat:@"http://www.tumblr.com/oauth/request_token?oauth_callback=%@",
                                       TMURLEncode([NSString stringWithFormat:@"%@://tumblr-authorize", URLScheme])];

    NSMutableURLRequest *request = mutableRequestWithURLString(tokenRequestURLString);
    [self signRequest:request withParameters:nil];

    NSURLConnectionCompletionHandler handler = ^(NSURLResponse *response, NSData *data, NSError *error) {
        if (error) {
            if (callback)
                callback(nil, nil, error);

            return;
        }

        int statusCode = ((NSHTTPURLResponse *)response).statusCode;

        if (statusCode == 200) {
            self.authCallback = callback;

            NSDictionary *responseParameters = formEncodedDataToDictionary(data);
            self.OAuthTokenSecret = responseParameters[@"oauth_token_secret"];

            NSURL *authURL = [NSURL URLWithString:
                              [NSString stringWithFormat:@"https://www.tumblr.com/oauth/authorize?oauth_token=%@",
                               responseParameters[@"oauth_token"]]];

#if __IPHONE_OS_VERSION_MIN_REQUIRED
#跳转到Safari
            [[UIApplication sharedApplication] openURL:authURL];
#else
            [[NSWorkspace sharedWorkspace] openURL:authURL];
#endif

        } else {
            if (callback)
                callback(nil, nil, errorWithStatusCode(statusCode));
        }
    };

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:handler];
}
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(0)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template