I am trying to implement the new ASWebAuthenticationSession in MacOS 10.15 and I'm getting a bit confused about the callbackURLScheme.
The header file for ASWebAuthenticationSession says:
The callback URL usually has a custom URL scheme. For the app to
receive the callback URL, it needs to either register the custom URL
scheme in its Info.plist, or set the scheme to callbackURLScheme
argument in the initializer.
So I initialize like this:
self.webAuthSession = [[ASWebAuthenticationSession alloc] initWithURL:self.authURL
callbackURLScheme:#"myurlscheme://"
completionHandler:^(NSURL * _Nullable callbackURL, NSError * _Nullable error) {
}];
The oauth provider I am trying to access, does not support custom URL schemes, so I have it set to redirect to a web address which in turn redirects to my URL Scheme.
So, after clicking Login in my MacOS app, Safari presents a window that allows me to login to the service. After successfully logging in the redirect happens but the web page just goes to a screen saying
Safari can't open the specified address.
I can see that the address is myurlscheme://?code=1234567890abcdefgetc... which is correct but it's obviously not calling the ASWebAuthenticationSession callback.
If I add the URL Scheme to the plist, then the safari window shows a prompt asking if you want to open it in the my app.
I know I can then fetch the code from the App Delegate's application:openURLs: method, but this is fiddly, has an unnecessary prompt for the user to click, and also leaves the ASWebAuth... safari window open. This should all be automatic through the callback.
The only way I get the callback to fire, is by closing the safari window in which case it calls back with error.
I have just returned to this to try again and it seems to be working fine on MacOS 10.15.1 and 10.15.2 beta.
I have noticed that the callbackURLScheme parameter must not include ://
I'm not able to go back and test it again on 10.15 but i'm happy to use this now assuming everyone on 10.15 would have updated to 10.15.1
Darren is right in MacOS you must remove the :// from the callbackURLScheme. iOS works fine no matter if you :// or not. However, in MacOS Catalyst there seems to be a bug. My app is getting focused once I login, but isn't getting the callback. Haven't found a way to fix it yet.
Related
I am setting up web push notification with OneSignal by reading their documentation. The follwoing code block is my configuration example
<script>
var OneSignal = window.OneSignal || [];
OneSignal.push(["init", {
appId: "my-app-id",
safari_web_id: 'my-safari-web-id',
autoRegister: false,
notifyButton: {
enable: true /* Set to false to hide */
},
}]);
</script>
It work on Google Chrome vs Firefox. But does not work on safari and when I click on Subscribe to notifications button I got the error like the following
What's wrong on my configuration?
I use safari Version 10.0.1 (12602.2.14.0.7)
Here are some of the reasons why this might be happening on Safari. From https://github.com/OneSignal/OneSignal-Website-SDK/wiki/Safari-Registration-Bug:
Ordered by most likely to least likely:
The Safari Site URL's origin must match the origin in your address bar. Even if your hosts file allows for an alias, you must use the actual origin listed in the Site URL when subscribing. The Site URL is even case-sensitive.
The safari_web_id parameter must match the Safari Web ID in the app's platform settings. Make sure it isn't being overridden by another value, or that the web.onesignal.auto prefix wasn't missed if they're using our autogenerated certificate.
The icon must be square (width equal to height).
Make sure you're not browsing Safari in a Private window.
Make sure you're not using Safari on iOS. Safari on Windows is not supported. Some users might download Safari 5.1.7 to test out push notifications, but the only Safari that is supported is Safari 7.1+ on the Mac OS X operating system.
The icon's DPI must be 72x72.
This can be checked using ImageMagick, with the command identify -format "%w x %h %x x %y" PATH_TO_FILE.
If a custom certificate is used, it may be invalid. Recreate the Safari platform using an auto-generated certificate. Contact OneSignal for this.
Finally, the auto-generated certificate may not be working correctly. Recreate the Safari platform using another auto-generated certificate. Contact OneSignal for this.
You can use OneSignal.log.setLevel('trace'); to trace your problem.
It may tell you exact problem like icon is missing or p12 is wrong.
Try to Paste the following Code in your Console and Check for the Error what you are getting
window.safari.pushNotification.requestPermission(OneSignal._API_URL + 'safari', OneSignal.config.safari_web_id, {app_id: OneSignal.config.appId}, function(response) {
console.warn('pushNotification.requestPermission Safari Registration Result:', response);
});
If in your Console it says Push Notification Prompt Has been disabled
Follow the below steps
Go to Preference in Menu Safari
Go to Notifications
Verify Allow Websites to ask for permission to send push notifications is Checked
How do you change the user agent used by WKWebview?
With the older WebView, I could write the following to change the user agent:
[myWebView setCustomUserAgent:#"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4)
AppleWebKit/537.77.4 (KHTML,like Gecko) Version/7.0.5 Safari/537.77.4"];
Very simple in Swift. Just place the following into your App DelegatedidFinishLaunchingWithOptions.
NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent" : "Custom Agent"])
If you want to append to the existing agent string then:
let userAgent = UIWebView().stringByEvaluatingJavaScriptFromString("navigator.userAgent")! + " Custom Agent"
NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent" : userAgent])
Note: You will need to uninstall and reinstall the App to avoid appending to the existing agent string.
I don't have an answer for this. However, some pointers from my research so far:
In iOS, it's possible to set a custom user agent for a UIWebView like this:
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:agent, #"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
In OSX, there was a setCustomUserAgent method for WebView elements that did the trick.
However, this doesn't work for WKWebView (at least, in OSX). I couldn't find any documentation about it from Apple, either.
Hope somebody can help!
I ran into the same issue, but managed to work around it using a combination of loadHTMLString on the WKWebView and a NSMutableURLRequest to do the heavy lifting.
My search on how to call some method on the WKWebView itself lead me to http://trac.webkit.org/changeset/165594, which implies there is a private method _setCustomUserAgent to do this. I'm not proficient enough in cocoa/swift to figure this one out.
I ended up using the code below, as I really only need to fetch the contents of a single URL and display it, but it may be helpful in some way.
What it does is simply loading the contents of an URL into the WKWebView as string, I suspect you may lose back/forward navigation and such, and it will only work for the initial page display, as the WKWebView will take over clicks and asset loading.
(please note, this example is written in Swift and not Objective-C)
self.webView = WKWebView(frame: webViewRect, configuration: webViewConfig)
// create the request
let url = NSURL(string: "https://example.com/")
let request = NSMutableURLRequest(URL: url!)
request.setValue("YourUserAgent/1.0", forHTTPHeaderField: "User-Agent")
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
let content = NSString(data: data, encoding: NSUTF8StringEncoding)
self.webView!.loadHTMLString(content!, baseURL: url)
}
It wouldn't be ideal but you could likely implement a custom NSURLProtocol handler to intercept HTTP requests and modify them with your custom user-agent header. I don't think this would work on iOS since WKWebView makes requests out-of-process and bypasses any registered NSURLProtocols. But it might work on OS X?
With a user entered URL in a text field, you can completely control the NSURLRequest using an NSMutableURLRequest object, and set the header field for it.
However, with things the user actually clicks on within the web view, you're kind of not in control from obvious and clearly documented Objective-C land.
I do not see any documented way beyond what WKWebView seems to push things toward, JavaScript. So, that means you can do things like posted here:
Mocking a useragent in javascript?
Using the script injection APIs.
This is why I do not like WKWebView. I want to like it, but I do not want to learn to do half of everything in JavaScript.
So, you can create a WKUserScript object to do this, and set its injection time to WKUserScriptInjectionTimeAtDocumentStart. That will enable you to handle things requested from page elements within the document, as long as the page itself is not loading other scripts that conflict.
First Quit Safari. Then open Terminal and paste this command, and press enter:
defaults write com.apple.Safari CustomUserAgent "\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/538.46 (KHTML, like Gecko) Version/7.1 Safari/537.85.7\""
Open Safari and you're done.
To undo this change, close Safari and paste this command into terminal:
defaults delete com.apple.Safari CustomUserAgent
Sometimes a restart may be required to get these changes to stick, not sure why, could be a cache thing.
I have3 Estimote beacons that can be seen with the App store Estimate App.
Now I am trying to run the Apple demo app AirLocation AirLocate
I have changed the UUID in the APLDefaults.m file to the default Estimote UUID _supportedProximityUUIDs = #[[[NSUUID alloc] initWithUUIDString:#"B9407F30-F5F8-466E-AFF9-25556B57FE6D"]];
I have enabled the Region to start startMonitoringForRegion as this stackoverflow says.
But they are not showing up, have you seen this ? Or am I missing some Estimate specific.
Regards
The problem is that AirLocate was written for iOS7, and in iOS8, the permissions model for iBeacons and other location operations has changed. In order to get the program to work on iOS 8 when compiled from XCode 6, you need to add code that requests permission in your AppDelegate. Like this:
if([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
This will prompt the user to authorize location operations including beacons. You also need to edit the info.plist for the app, and add a new string key called NSLocationAlwaysUsageDescription with a value like "This app needs access to location services" so the OS can prompt the user for this permission.
After you run your app, you can check in settings to see if this permission has been granted properly.
Another problem I have noticed in iOS 9 is that the calibration sometimes does not work. Seems to be an NSNumber conversion issue. The following edit in APLCalibrationCalculator.m fixed it:-
//measuredPower = [[sample valueForKeyPath:#"#avg.rssi"] integerValue];
measuredPower = [[sample valueForKeyPath:#"#avg.rssi"] intValue];
I encountered a facebook login problem. I installed FB SDK 3.0 and sample projects seemly run well --- not quite sure though. When I create a button in my own project and try to log in to Facebook, I always get FBSessionStateClosedLoginFailed from sessionStateChanged method, although I apparently get the page "You have already authorized this app. Press Okay to continue". I also tried log out through Simulator Safari facebook page, and re-launched my project and I got new username/password page and I then logged in, but I still get FBSessionStateClosedLoginFailed!
A few more observations.
1, I got FBSessionStateClosedLoginFailed thru NSLog, the log comes up right after I clicked Okay button on the Facebook page "You have already authorized this app.
2, I tried clean xcode project cache, but no help.
Kind of frustrated. I wonder if any of you can help on this problem. Appreciated!
-Tony
Addition:
I did a bit more research, and I found what I have is probably a typical problem, as Facebook highlight here ("Pro-tip 3: Complete all iOS App Settings" section in page http://developers.facebook.com/docs/howtos/login-with-facebook-using-ios-sdk/). The image with a big red cross over iPhone is exactly what I have. From xcode project, I click my login button, and I'm directed to this page, and every time I have to click "Okay" on this page (let me call it Okay_page) to go back to my app in Simulator, however with login status as FBSessionStateClosedLoginFailed. I checked bundle ID, I have it correct. I don't have a 'YOUR_IPHONE_APP_STORE_ID' yet, which I believe I need pay Apple to get and I will do it later but not while I'm still working on it.
Now get a couple of questions:
1, Do I have to have YOUR_IPHONE_APP_STORE_ID to skip this Okay_page?
2, Does this (not automatically re-directed back to my app from this Okay_page) have anything to do with that I always get a login status of FBSessionStateClosedLoginFailed when I manually click Okay in this Okay_page and return to my app in Simulator? In another word, is (that I have to manually click Okay to get back to my app in Simulator) (the reason I got a status of FBSessionStateClosedLoginFailed)?
Did you have set this function in AppDelegate?
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [FBSession.activeSession handleOpenURL:url];
}
I had the same issue:
We created the facebook app (developer.facebook.com) setting our
bundle id (ex. com.MyCompany.MyApp)
Off course, created our iOS app with the same bundle id
After delivered our app to the client, he changed the bundle id
without notify us.
Obviously he started to get FBSessionStateClosedLoginFailed errors!
So, you probably have different bundle id's between your iOS project and your Facebook App.
I had this error and couldn't resolve it for quite some time. It turns out that if you are using the native iOS Facebook account in settings and choose "no" for allow Facebook for this app, the app is blocked by the OS. You will always get the FBSessionStateClosedLoginFailed error and there is nothing you can do inside the code to fix this (99% sure). The best you can do is alert the user to check out the settings.
i am typing wrong line in given below method. self.session was nil
return [self.session handleOpenURL:url];
it should be
return [[FBSession activeSession] handleOpenURL:url];
Use Following Method.
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// attempt to extract a token from the url
return [[FBSession activeSession] handleOpenURL:url];
// return [self.session handleOpenURL:url];
}
I fixed this problem after I reset the simulator. iOS Simulator - Reset Content and Settings..., hope to help.
I had this issue because I was calling [FBSession.activeSession close] in my applicationDidEnterBackground: app delegate callback. This closed the session so that when the redirect brought the app back up to open the session, it failed because it had been put into the closed state.
Make sure that you're not closing the session anywhere before you get to the Okay page.
go to Facebook.com. Open your application on the left panel -> Status & Review -> there is a toggle with text "Do you want to make this app and all its live features available to the general public?". Turn it on
well. I followed FBLoginCustomUISample somewhere on github.com and it was a working example of facebook login implementation from facebook itself. You can also read my workaround while creating facebook login feature in a iOS app. Here is a link form me:
Native iOS Facebook SSO won't return to app
What method must I implement in my cocoa application’s delegate so that on launch, it’ll open a url? (http/https, in this case) I’ve already implemented the url schemes, I just need to know how I can get my application to open on a url notification.
Update: I’m sorry, I wasn’t very clear. My application IS a browser that support https/http urls, but can only open them when it’s already running. What can I do to implement support for open urls in my app on launch?
When an application finishes launching on OS X, NSApp (the global NSApplication instance for the program) sends its delegate the applicationDidFinishLaunching: message (via the notification system). You can implement that method in your delegate to handle the notification and open a browser window in response, using NSWorkspace. Something like the following would work:
// Your NSApp delegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:#"http://www.example.com/"]];
}
It's not a delegate method. You need to implement an Apple Event handler for the getURL event.
As luck would have it, this is exactly the case Apple uses to demonstrate implementing an Apple Event handler.
I already had implemented the getURL event, so that alone isn’t enough to get the application to open a url on launch. The trick is that the AppleEvent must be installed in applicationWillFinishLaunching: not applicationDidFinishLaunching:. Otherwise, the event isn’t sent at all because the app hasn’t registered it in time.
To implement a protocol handler that you can select (in Safari preferences, for example) as the "default browser" and which will launch in response to HTTP / HTTPS, you need to do a few things.
Add .scriptSuite and .scriptTerminology files to your project resources. These will tell Mac OS X that you'll be handling the GetURL command.
Add a CFBundleURLTypes key to your Info.plist file listing the "URL Schemes" that your app will handle.
Also in Info.plist, add the NSAppleScriptEnabled key with the value YES.
Add a new class to your application as a subclass of NSScriptCommand and implement the -(id)performDefaultImplementation selector. From within this function you will find the clicked URL in [self directParameter]. Pass this on to your app's URL handler!
For the full details check out the article:
http://www.xmldatabases.org/WK/blog/1154_Handling_URL_schemes_in_Cocoa.item