0

この他の質問の指示に従って、アプリの webviews にカスタム UserAgent を設定しています。具体的には、私は設定しています

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"MyApp/MyLongVersionInfoString", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

アプリ起動時。(NSMutableURLRequest の適切なヘッダーを UIWebView で使用するように設定するだけでは、UserAgent、User-Agent、User_Agent は機能しません。)

これにより、埋め込まれた Web ビューが正しいユーザー エージェントを使用するようになります。ただし、ダイアログ用に Facebook SDK で使用される埋め込み Web ビューも壊れますwindow.location.href="fbconnect:\/\/success?post_id=100002469633196_43677789308119...ユーザーは手動で閉じる必要があります)。これは、カスタム ユーザー エージェントが設定されている場合にのみ発生します。

Facebook が呼び出す前にユーザー エージェントの設定を解除し、後でリセットすることで問題を回避できると考えましたが、デフォルトのデフォルト設定を解除できないようです。[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"UserAgent"]各 Facebook 呼び出しの前にandを呼び出して、呼び出しの結果ハンドラーでそれらを再度設定しようと[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:NSRegistrationDomain]しましたが、それでも同じ誤動作が見られます。

初期設定を に切り替えてみまし[[NSUserDefaults standardUserDefaults] setObject:newUA forKey:@"UserAgent"];たが Web ビューがユーザー エージェントを取得しません。

確かに、Facebook以外の埋め込みWebビューを備えたアプリで、以前に誰かがFacebook SDKを使用したことがあります。私は何が欠けていますか?私はこれについて多くのラウンドを行ってきましたが、それぞれがほとんどすべてを修正しているようには見えません。

4

1 に答える 1

1

NSURLProtocol サブクラスを実装することで、これを行う必要がありました。私の実装はごちゃごちゃしていましたが、StackOverflow で既に例を見つけることができなかったことに驚いたので、以下にスクラブされたバージョンを含めます。

#import <Foundation/Foundation.h>

@interface MyProtocol : NSURLProtocol {
    NSURLConnection *connection;
}

@property (nonatomic, retain) NSURLConnection *connection;

@end

@implementation MyProtocol

@synthesize connection;

#pragma mark -
#pragma mark custom methods

+ (NSString *)myUA {
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"MyCustomUserAgent"];
}
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
    NSString *scheme = [request.URL.scheme stringByAppendingString:@"://"];
    NSString *baseRequestString = [[[request.URL absoluteString] stringByReplacingOccurrencesOfString:request.URL.path withString:@""]
                                   stringByReplacingOccurrencesOfString:scheme withString:@""];
    if (request.URL.query != nil) {
        NSString *query = [@"?" stringByAppendingString:request.URL.query];
        baseRequestString = [baseRequestString stringByReplacingOccurrencesOfString:query withString:@""];
    }

    BOOL shouldIntercept = [baseRequestString isEqualToString:myWebHost];
    BOOL alreadyIntercepted = [[NSURLProtocol propertyForKey:@"UserAgent" inRequest:request] isEqualToString:[self myUA]];
    return shouldIntercept && !alreadyIntercepted;
}

+ (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request
{
    return request;
}

- (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id<NSURLProtocolClient>)client
{
    NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:request.URL
                                                       cachePolicy:request.cachePolicy
                                                   timeoutInterval:request.timeoutInterval];
    [mutableRequest setAllHTTPHeaderFields:[request allHTTPHeaderFields]];
    [mutableRequest setHTTPMethod:[request HTTPMethod]];
    if ([request HTTPBody] != nil)
        [mutableRequest setHTTPBody:[request HTTPBody]];
    if ([request HTTPBodyStream] != nil)
        [mutableRequest setHTTPBodyStream:[request HTTPBodyStream]];
    [NSURLProtocol setProperty:[[self class] myUA] forKey:@"UserAgent" inRequest:mutableRequest];
    [mutableRequest setValue:[[self class] myUA] forHTTPHeaderField:@"User-Agent"];
    [mutableRequest setValue:[[self class] myUA] forHTTPHeaderField:@"User_Agent"];

    self = [super initWithRequest:mutableRequest cachedResponse:cachedResponse client:client];
    return self;
}

- (void) dealloc
{
    [connection release];
}

#pragma mark -
#pragma mark boilerplate NSURLProtocol subclass requirements

- (void) startLoading
{
    self.connection = [[NSURLConnection connectionWithRequest:[self request] delegate:self] retain];
}

- (void)stopLoading
{
    [self.connection cancel];
}

- (void)connection:(NSURLConnection*)conn didReceiveResponse:(NSURLResponse*)response
{
    [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:[[self request] cachePolicy]];
}

- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    [[self client] URLProtocol:self didLoadData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection*)conn
{
    [[self client] URLProtocolDidFinishLoading:self];
}

- (NSURLRequest*)connection:(NSURLConnection*)connection willSendRequest:(NSURLRequest*)theRequest redirectResponse:(NSURLResponse*)redirectResponse
{
    return theRequest;
}

- (void)connection:(NSURLConnection*)conn didFailWithError:(NSError*)error
{
    [[self client] URLProtocol:self didFailWithError:error];
}

@end

[NSURLProtocol registerClass:[MyProtocol class]];プロトコルを適用する Web リクエストを作成する前に、プロトコルを登録する必要があることに注意してくださいapplicationDidFinishLaunchingWithOptions:

于 2013-05-09T02:41:26.877 に答える