[英]How to get response data from the Safari url in iOS app?
我想在Safari中登录Instagram,并希望在ios应用中对此做出响应。
我已从iPhone应用程序重定向到Safari,以便Instagram登录。 现在,我想在iPhone应用程序中获取Safari的Instagram登录的响应数据。
我对此一无所知。 那有可能吗? 我们可以在iOS应用中获得Safari URL的响应吗?
为此,您必须使用UIWebView及其委托方法。
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *responseURL = [request.URL absoluteString];
NSString *urlCallbackPrefix = [NSString stringWithFormat:@"%@/?code=", INSTAGRAM_CALLBACK_BASE];
//We received the code, now request the auth token from Instagram.
if([responseURL hasPrefix:urlCallbackPrefix])
{
NSString *authToken = [responseURL substringFromIndex:[urlCallbackPrefix length]];
NSURL *url = [NSURL URLWithString:@"https://api.instagram.com/oauth/access_token"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSDictionary *paramDict = [NSDictionary dictionaryWithObjectsAndKeys:authToken, @"code", INSTAGRAM_CALLBACK_BASE, @"redirect_uri", @"authorization_code", @"grant_type", INSTAGRAM_CLIENT_ID, @"client_id", INSTAGRAM_CLIENT_SECRET, @"client_secret", nil];
NSString *paramString = [paramDict urlEncodedString];
NSString *charset = (NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
[request setHTTPMethod:@"POST"];
[request addValue:[NSString stringWithFormat:@"application/x-www-form-urlencoded; charset=%@",charset] forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[paramString dataUsingEncoding:NSUTF8StringEncoding]];
self.tokenRequestConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[self.tokenRequestConnection start];
return NO;
}
return YES;
}
Github Instagram-Auth-iOS上的这个仓库将为您提供帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.