Twitter SDK 授权: 解决无法切换账号的问题及无法显示

2023-11-23  本文已影响0人  蓝天白云_Sam

问题解决:

WKWebsiteDataStore *dateStore = [WKWebsiteDataStore defaultDataStore];
    NSSet * dataTypes = [[NSSet alloc] initWithObjects:@"WKWebsiteDataTypeCookies", nil];
    //[WKWebsiteDataStore allWebsiteDataTypes];
    [dateStore fetchDataRecordsOfTypes: dataTypes
                     completionHandler:^(NSArray<WKWebsiteDataRecord *> *__nonnull records) {
        for (WKWebsiteDataRecord *record in records) {
            if ([record.displayName containsString:@"twitter"]) {
                [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:record.dataTypes
                                                          forDataRecords:@[ record ]
                                                       completionHandler:^{
                    NSLog(@"Cookies for %@ deleted successfully", record.displayName);
                }];
            }
        }
    }];
WKWebViewConfiguration *wkWebViewConfiguration = [WKWebViewConfiguration new];
wkWebViewConfiguration.applicationNameForUserAgent = @"Safari";
[[[NSBundle bundleForClass:[WKWebView class]] infoDictionary] objectForKey:@"CFBundleVersion"]

源码

#import <UIKit/UIKit.h>


#define kTwtAuth_ErrorCode_Cancel -1000
#define kTwtAuth_ErrorCode_Denied -1001
#define kTwtAuth_ErrorCode_Other -1002
NS_ASSUME_NONNULL_BEGIN

typedef void (^TWTAuthCompletion)(NSString *_Nullable code, NSError *_Nullable error);


@interface WSTwitterAuthVC : UIViewController

+ (void)twtAuthWithCompletion:(TWTAuthCompletion)completion;

@end

NS_ASSUME_NONNULL_END
//
//  WSTwitterAuthVC.m
//  WSCalendarApp_Example
//
//  Created by Sammy Lan on 2023/10/18.
//  Copyright © 2023 SammyLan. All rights reserved.
//

#import "WSTwitterAuthVC.h"
#import <WebKit/WebKit.h>
#import <WSCommonUIKit/HookViewController.h>

#define kTwtAuth_Code @"code"
#define kTwtAuth_Error @"error"
#define kTwtAuth_Error_AccessDenied @"access_denied"


@interface WSTwitterAuthVC () <WKNavigationDelegate>
@property (nonatomic, strong) WKWebView *webview;
@property (nonatomic, strong) UIWindow *preWindow;
@property (nonatomic, strong) UIWindow *curWindow;
@property (nonatomic, strong) NSDictionary *resultInfo;
@property (nonatomic, strong) TWTAuthCompletion completion;
@end


@implementation WSTwitterAuthVC

+ (void)twtAuthWithCompletion:(TWTAuthCompletion)completion
{
    WSTwitterAuthVC *twtVC = [[WSTwitterAuthVC alloc] init];
    UIWindow *window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
    window.windowLevel = UIWindowLevelAlert - 1;
    [window makeKeyAndVisible];
    twtVC.curWindow = window;
    HookViewController *vc = [[HookViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    window.rootViewController = nav;
    twtVC.completion = completion;

    [nav pushViewController:twtVC animated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    WKWebsiteDataStore *dateStore = [WKWebsiteDataStore defaultDataStore];
    NSSet * dataTypes = [[NSSet alloc] initWithObjects:WKWebsiteDataTypeCookies, nil];
    //[WKWebsiteDataStore allWebsiteDataTypes];
    [dateStore fetchDataRecordsOfTypes: dataTypes
                     completionHandler:^(NSArray<WKWebsiteDataRecord *> *__nonnull records) {
        for (WKWebsiteDataRecord *record in records) {
            if ([record.displayName containsString:@"twitter"]) {
                [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:record.dataTypes
                                                          forDataRecords:@[ record ]
                                                       completionHandler:^{
                    NSLog(@"Cookies for %@ deleted successfully", record.displayName);
                }];
            }
        }
    }];


    WKWebViewConfiguration *wkWebViewConfiguration = [WKWebViewConfiguration new];
    WKUserContentController *wkUserContentController = [WKUserContentController new];
    wkWebViewConfiguration.userContentController = wkUserContentController;
    wkWebViewConfiguration.applicationNameForUserAgent = @"Mozilla/5.0";
    //    wkWebViewConfiguration.allowsInlineMediaPlayback = YES;
    //    wkWebViewConfiguration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
    //    wkWebViewConfiguration.preferences.javaScriptEnabled = YES;
    //    wkWebViewConfiguration.preferences.javaScriptCanOpenWindowsAutomatically = YES;
    //    wkWebViewConfiguration.suppressesIncrementalRendering = YES; // 是否支持记忆读取
    [wkWebViewConfiguration.preferences setValue:@(YES) forKey:@"allowFileAccessFromFileURLs"];
    //[wkWebViewConfiguration setValue:@(YES) forKey:@"allowUniversalAccessFromFileURLs"];


    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:wkWebViewConfiguration];
    webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    webView.navigationDelegate = self;
    //    webView.UIDelegate = self;
    webView.configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;


    NSDictionary *extraInfo = @{
        @"code_challenge" : @"challenge",
        @"code_challenge_method" : @"PLAIN",
        @"response_type" : @"code",
        @"client_id" : @"cE4yMWNfOVRsYTVMMVVuN185TXk6MTpjaQ",
        @"redirect_uri" : @"wesingapp://",
        @"scope" : @"offline.access tweet.read users.read",
        @"state" : @"state"
    };

    NSMutableArray *queryItems = [[NSMutableArray alloc] init];
    [extraInfo enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL *_Nonnull stop) {
        [queryItems addObject:[[NSURLQueryItem alloc] initWithName:key value:obj]];
    }];


    NSURLComponents *components = [[NSURLComponents alloc] initWithString:@"https://twitter.com/i/oauth2/authorize?"];
    [components setQueryItems:queryItems];
    NSString *url = components.URL.absoluteString;
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];

    [self.view addSubview:webView];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//在发送请求之前,决定是否跳转
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    NSLog(@"cmd = %@", NSStringFromSelector(_cmd));
    NSString *str = [navigationAction.request.URL absoluteString];
    if (decisionHandler) {
        if ([str hasPrefix:@"wesingapp://"]) {
            NSURLComponents *components = [NSURLComponents componentsWithString:str];

            NSMutableDictionary<NSString *, NSString *> *resultInfo = NSMutableDictionary.new;
            [components.queryItems enumerateObjectsUsingBlock:^(NSURLQueryItem *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
                resultInfo[obj.name] = obj.value;
            }];
            self.resultInfo = resultInfo;
            decisionHandler(WKNavigationActionPolicyCancel);
            [self.navigationController popViewControllerAnimated:YES];

        } else {
            decisionHandler(WKNavigationActionPolicyAllow);
        }
    }
}


- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.preWindow makeKeyAndVisible];
    self.curWindow.rootViewController = nil;
    self.curWindow = nil;
    NSError *error;
    NSString *code;
    if (self.resultInfo != nil) {
        code = [self.resultInfo objectForKey:kTwtAuth_Code];
        NSString *errorStr = [self.resultInfo objectForKey:kTwtAuth_Error];
        if (errorStr != nil) {
            if ([errorStr isEqual:kTwtAuth_Error_AccessDenied]) {
                error = [NSError errorWithDomain:@"twtauth.wesing.com" code:kTwtAuth_ErrorCode_Denied userInfo:nil];
            } else {
                error = [NSError errorWithDomain:@"twtauth.wesing.com" code:kTwtAuth_ErrorCode_Other userInfo:nil];
            }
        }
    } else {
        error = [NSError errorWithDomain:@"twtauth.wesing.com" code:kTwtAuth_ErrorCode_Cancel userInfo:nil];
    }
    self.completion(code, error);
}
@end

上一篇 下一篇

猜你喜欢

热点阅读