簡體   English   中英

如何從iOS應用中的Safari URL獲取響應數據?

[英]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及其委托方法。

來自Instagram-Auth-iOS的代碼

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM