Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 覆盖UIWebView用户代理-但不适用于Facebook SDK_Ios_Facebook_Uiwebview - Fatal编程技术网

Ios 覆盖UIWebView用户代理-但不适用于Facebook SDK

Ios 覆盖UIWebView用户代理-但不适用于Facebook SDK,ios,facebook,uiwebview,Ios,Facebook,Uiwebview,我正在根据中的说明为我的应用程序中的WebView设置自定义UserAgent。具体来说,我正在设置 NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"MyApp/MyLongVersionInfoString", @"UserAgent", nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; 在应用程序

我正在根据中的说明为我的应用程序中的WebView设置自定义UserAgent。具体来说,我正在设置

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"MyApp/MyLongVersionInfoString", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
在应用程序发布时。(仅为UIWebView使用的NSMutableURLRequest设置适当的头—UserAgent、User Agent、User_Agent—不起作用。)

这会使我的嵌入式WebView使用正确的用户代理。但是,它也破坏了Facebook SDK用于对话框的嵌入式网络视图-例如,在我发布到我的墙上后,FB对话框网络视图的内容被类似于
window.location.href=“fbconnect:\/\/success?post\u id=100002469633196\u 43677789308119的文本所取代…
并且网络视图不会像通常那样关闭(用户必须手动将其关闭)。这仅在设置了自定义用户代理时发生

我以为我可以在Facebook调用之前取消用户代理的设置,然后再重置它来规避这个问题,但似乎我无法取消默认设置;我尝试调用
[[NSUserDefaults standardUserDefaults]removeObjectForKey:@“UserAgent”]
[[NSUserDefaults standardUserDefaults]在每次Facebook调用之前移除PersistentDomainForName:nsrRegistrationDomain]
,并在调用的结果处理程序中再次设置它们,但我仍然看到相同的错误行为

我尝试将初始设置切换到
[[NSUserDefaults standardUserDefaults]setObject:newUA forKey:@“UserAgent”];
,但是我的Web视图没有选择用户代理


当然,当然,以前有人在非Facebook嵌入式网络视图的应用程序中使用过Facebook SDK。我错过了什么?我在这方面已经做了很多次了,每一次似乎都几乎解决了所有问题。

我最后不得不实现一个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{
NSURLConnection*连接;
}
@属性(非原子,保留)NSURLConnection*连接;
@结束
@MyProtocol的实现
@综合联系;
#布拉格标记-
#pragma标记自定义方法
+(NSString*)myUA{
返回[[NSUserDefaults standardUserDefaults]stringForKey:@“MyCustomUserAgent”];
}
+(BOOL)canInitWithRequest:(NSURLRequest*)request
{
NSString*scheme=[request.URL.scheme-stringByAppendingString:@://“];
NSString*baseRequestString=[[[request.URL absoluteString]stringByReplacingOccurrencesOfString:request.URL.path with String:@']
stringByReplacingOccurrencesOfString:scheme with string:@“”;
if(request.URL.query!=nil){
NSString*query=[@”?“stringByAppendingString:request.URL.query];
baseRequestString=[baseRequestString stringByReplacingOccurrencesOfString:query with String:@”“;
}
BOOL shoulintercept=[baseRequestString IsequalString:myWebHost];
BOOL alreadyIntercepted=[[NSURLProtocol propertyWorkey:@“UserAgent”inRequest:request]IsequalString:[self myUA]];
返回时应接受已接受的&;
}
+(NSURLRequest*)规范请求请求请求:(NSURLRequest*)请求
{
返回请求;
}
-(id)initWithRequest:(NSURLRequest*)请求缓存响应:(NSCachedURLResponse*)缓存响应客户端:(id)客户端
{
NSMutableURLRequest*mutableRequest=[NSMutableUrlRequestRequestWithURL:request.URL
cachePolicy:request.cachePolicy
timeoutInterval:request.timeoutInterval];
[mutableRequest setAllHTTPHeaderFields:[request allHTTPHeaderFields]];
[mutableRequest setHTTPMethod:[request HTTPMethod]];
如果([请求HTTPBody]!=nil)
[mutableRequest setHTTPBody:[请求HTTPBody]];
如果([请求HTTPBodyStream]!=nil)
[mutableRequest setHTTPBodyStream:[request HTTPBodyStream]];
[NSURLProtocol setProperty:[[self class]myUA]forKey:@“UserAgent”inRequest:mutableRequest];
[mutableRequest设置值:[[self class]myUA]用于HttpHeaderField:@“用户代理”];
[mutableRequest setValue:[[self class]myUA]用于HttpHeaderField:@“用户\代理”];
self=[super initWithRequest:mutableRequest-cachedResponse:cachedResponse-client:client];
回归自我;
}
-(无效)解除锁定
{
[连接释放];
}
#布拉格标记-
#pragma标记样板文件NSURLProtocol子类要求
-(空)惊人的
{
self.connection=[[NSURLConnection connectionWithRequest:[self-request]委托:self]retain];
}
-(无效)停止加载
{
[自连接取消];
}
-(无效)连接:(NSURLConnection*)连接接收方响应:(NSURLConnection*)响应
{
[[self-client]URLProtocol:self-didReceiveResponse:response-cacheStoragePolicy:[[self-request]cachePolicy]];
}
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
{
[[self-client]URLProtocol:self-didLoadData:data];
}
-(无效)连接完成加载:(NSURLConnection*)连接
{
[[self-client]URLProtocoldFinishLoading:self];
}
-(NSURLRequest*)连接:(NSURLConnection*)连接将发送请求:(NSURLRequest*)请求重定向响应:(NSURLResponse*)重定向响应
{
返回请求;
}
-(无效)连接:(NSURLConnection*)连接失败错误:(NSError*)错误
{
[[self-client]URLProtocol:self-didFailWithError:error];
}
@结束
请注意,在发出任何要应用该协议的web请求之前,必须使用
[NSURLProtocol registerClass:[MyProtocol class]];
注册该协议-例如,在
ApplicationIDFinishLaunchwithOptions: