/
PhWebViewController.m
89 lines (71 loc) · 2.61 KB
/
PhWebViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// PhWebViewController.m
// PhFacebook
//
// Created by Philippe on 10-08-27.
// Copyright 2010 Philippe Casgrain. All rights reserved.
//
#import "PhWebViewController.h"
#import "PhFacebook_URLs.h"
#import "PhFacebook.h"
@implementation PhWebViewController
@synthesize window;
@synthesize webView;
@synthesize cancelButton;
@synthesize parent;
- (id) init
{
if ((self = [super init]))
{
}
return self;
}
- (void) dealloc
{
[super dealloc];
}
- (void) awakeFromNib
{
NSBundle *bundle = [NSBundle bundleForClass: [PhFacebook class]];
self.window.title = [bundle localizedStringForKey: @"FBAuthWindowTitle" value: @"" table: nil];
self.cancelButton.title = [bundle localizedStringForKey: @"FBAuthWindowCancel" value: @"" table: nil];;
}
#pragma mark Delegate
- (void) webView: (WebView*) sender didCommitLoadForFrame: (WebFrame*) frame;
{
NSString *url = [sender mainFrameURL];
NSComparisonResult res = [url compare: kFBUIServerURL options: NSCaseInsensitiveSearch range: NSMakeRange(0, [kFBUIServerURL length])];
if (res == NSOrderedSame)
{
// Facebook needs user input, show the window
[self.window makeKeyAndOrderFront: sender];
}
}
- (NSString*) extractParameter: (NSString*) param fromURL: (NSString*) url
{
NSString *res = nil;
NSRange paramNameRange = [url rangeOfString: param options: NSCaseInsensitiveSearch];
if (paramNameRange.location != NSNotFound)
{
// Search for '&' or end-of-string
NSRange searchRange = NSMakeRange(paramNameRange.location + paramNameRange.length, [url length] - (paramNameRange.location + paramNameRange.length));
NSRange ampRange = [url rangeOfString: @"&" options: NSCaseInsensitiveSearch range: searchRange];
if (ampRange.location == NSNotFound)
ampRange.location = [url length];
res = [url substringWithRange: NSMakeRange(searchRange.location, ampRange.location - searchRange.location)];
}
return res;
}
- (void) webView: (WebView*) sender didFinishLoadForFrame: (WebFrame*) frame
{
NSString *url = [sender mainFrameURL];
NSComparisonResult res = [url compare: kFBLoginSuccessURL options: NSCaseInsensitiveSearch range: NSMakeRange(0, [kFBLoginSuccessURL length])];
if (res == NSOrderedSame)
{
NSString *accessToken = [self extractParameter: kFBAccessToken fromURL: url];
NSString *tokenExpires = [self extractParameter: kFBExpiresIn fromURL: url];
NSString *errorReason = [self extractParameter: kFBErrorReason fromURL: url];
[parent setAccessToken: accessToken expires: tokenExpires error: errorReason];
}
}
@end