Chrome Packaged App - Set Custom User Agent - google-chrome-app

I have a published hybrid app that is available on IOS, Android and in what is left of the Chrome Web Store. The Chrome version is also the basis for the MacOS and Windows versions, built using NWJS.
In developing some new functionality for this app, I came across the need to customise the user agent header in direct XMLHttpRequest's from the client code to a third party site. I thought this would be easy, all I wanted to do was to add the app name to the end of the existing navigator.userAgent.
I could not have been more wrong. This results in a refusal to set unsafe headers. Given that using the Chrome debugger you can set any user agent you want, this seems very strange to me.
For IOS I found that I could put some Objective C code in the IOS Wrapper as follows :
// get original UserAgent string by using temporal UIWebView
UIWebView *tmp = [[UIWebView alloc] init];
NSString *originalUA = [tmp stringByEvaluatingJavaScriptFromString:#"navigator.userAgent"];
// create custom UserAgent string
NSString *customUA = [NSString stringWithFormat:#"%# %#", originalUA, #" WebViewApp myAppName/myAppVersion"];
// set custom UserAgent as default
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:customUA , #"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
and for Android some Java code in the wrapper, along the lines of :
string ua = webview.getSettings().getUserAgentString();
webview.getSettings().setUserAgentString(ua+" WebViewApp myAppName/myAppVersion");
and both of these approaches work fine.
I thought that I could use the chrome webRequest API to achieve the same in the Chrome app by changing the actual XHR headers each time, rather than the simpler approach used in IOS and Android, but I found that this API is only available to chrome extensions and not to chrome packaged apps.
Someone else asked a similar question here a few years ago, but it was never answered. Has anyone now got any suggestions on how to change the user agent for all XHR requests from inside a Chrome Packaged App, or do I need to channel these requests through a server.
Thinking outside the box, could I attach the Chrome debugger programatically, use that to set the user agent and then detach. I would be loath to leave the debugger attached from a security angle.
Thanks for any help.

Related

Getting iPhone Carrier Mobile Number in Xcode [duplicate]

Is there any way to get own phone number by standard APIs from iPhone SDK?
At the risk of getting negative marks, I want to suggest that the highest ranking solution (currently the first response) violates the latest SDK Agreement as of Nov 5, 2009. Our application was just rejected for using it. Here's the response from Apple:
"For security reasons, iPhone OS restricts an application (including its preferences and data) to a unique location in the file system. This restriction is part of the security feature known as the application's "sandbox." The sandbox is a set of fine-grained controls limiting an application's access to files, preferences, network resources, hardware, and so on."
The device's phone number is not available within your application's container. You will need to revise your application to read only within your directory container and resubmit your binary to iTunes Connect in order for your application to be reconsidered for the App Store.
This was a real disappointment since we wanted to spare the user having to enter their own phone number.
No, there's no legal and reliable way to do this.
If you find a way, it will be disabled in the future, as it has happened with every method before.
Update: capability appears to have been removed by Apple on or around iOS 4
Just to expand on an earlier answer, something like this does it for me:
NSString *num = [[NSUserDefaults standardUserDefaults] stringForKey:#"SBFormattedPhoneNumber"];
Note: This retrieves the "Phone number" that was entered during the iPhone's iTunes activation and can be null or an incorrect value. It's NOT read from the SIM card.
At least that does in 2.1. There are a couple of other interesting keys in NSUserDefaults that may also not last. (This is in my app which uses a UIWebView)
WebKitJavaScriptCanOpenWindowsAutomatically
NSInterfaceStyle
TVOutStatus
WebKitDeveloperExtrasEnabledPreferenceKey
and so on.
Not sure what, if anything, the others do.
Using Private API you can get user phone number on the following way:
extern NSString* CTSettingCopyMyPhoneNumber();
+(NSString *) phoneNumber {
NSString *phone = CTSettingCopyMyPhoneNumber();
return phone;
}
Also include CoreTelephony.framework to your project.
You cannot use iOS APIs alone to capture the phone number (even in a private app with private APIs), as all known methods of doing this have been patched and blocked as of iOS 11. Even if a new exploit is found, Apple has made clear that they will reject any apps from the app store for using private APIs to do this. See #Dylan's answer for details.
However, there is a legal way to capture the phone number without any user data entry. This is similar to what Snapchat does, but easier, as it does not require the user to type in their own phone number.
The idea is to have the app programmatically send a SMS message to a server with the app’s unique installation code. The app can then query the same server to see if it has recently received a SMS message from a device with this unique app installation code. If it has, it can read the phone number that sent it. Here’s a demo video showing the process. As you can see, it works like a charm!
This is not super easy to set up, but it be configured in a few hours at no charge on a free AWS tier with the sample code provided in the tutorial here.
As you probably all ready know if you use the following line of code, your app will be rejected by Apple
NSString *num = [[NSUserDefaults standardUserDefaults] stringForKey:#"SBFormattedPhoneNumber"];
here is a reference
http://ayeapi.blogspot.com/2009/12/sbformatphonenumber-is-lie.html
you can use the following information instead
NSString *phoneName = [[UIDevice currentDevice] name];
NSString *phoneUniqueIdentifier = [[UIDevice currentDevice] uniqueIdentifier];
and so on
#property(nonatomic,readonly,retain) NSString *name; // e.g. "My iPhone"
#property(nonatomic,readonly,retain) NSString *model; // e.g. #"iPhone", #"iPod Touch"
#property(nonatomic,readonly,retain) NSString *localizedModel; // localized version of model
#property(nonatomic,readonly,retain) NSString *systemName; // e.g. #"iPhone OS"
#property(nonatomic,readonly,retain) NSString *systemVersion; // e.g. #"2.0"
#property(nonatomic,readonly) UIDeviceOrientation orientation; // return current device orientation
#property(nonatomic,readonly,retain) NSString *uniqueIdentifier; // a string unique to each device based on various hardware info.
Hope this helps!
To get you phone number you can read a plist file. It will not work on non-jailbroken iDevices:
NSString *commcenter = #"/private/var/wireless/Library/Preferences/com.apple.commcenter.plist";
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:commcenter];
NSString *PhoneNumber = [dict valueForKey:#"PhoneNumber"];
NSLog([NSString stringWithFormat:#"Phone number: %#",PhoneNumber]);
I don't know if Apple allow this but it works on iPhones.
No official API to do it. Using private API you can use following method:
-(NSString*) getMyNumber {
NSLog(#"Open CoreTelephony");
void *lib = dlopen("/Symbols/System/Library/Framework/CoreTelephony.framework/CoreTelephony",RTLD_LAZY);
NSLog(#"Get CTSettingCopyMyPhoneNumber from CoreTelephony");
NSString* (*pCTSettingCopyMyPhoneNumber)() = dlsym(lib, "CTSettingCopyMyPhoneNumber");
NSLog(#"Get CTSettingCopyMyPhoneNumber from CoreTelephony");
if (pCTSettingCopyMyPhoneNumber == nil) {
NSLog(#"pCTSettingCopyMyPhoneNumber is nil");
return nil;
}
NSString* ownPhoneNumber = pCTSettingCopyMyPhoneNumber();
dlclose(lib);
return ownPhoneNumber;
}
It works on iOS 6 without JB and special signing.
As mentioned creker on iOS 7 with JB you need to use entitlements to make it working.
How to do it with entitlements you can find here:
iOS 7: How to get own number via private API?
AppStore will reject it, as it's reaching outside of application container.
Apps should be self-contained in their bundles, and may not read or write data outside the designated container area
Section 2.5.2 :
https://developer.apple.com/app-store/review/guidelines/#software-requirements

Opening external application inside my application

Can I invoke another installed applications within my application? Suppose I have installed AngryBirds, I have to invoke that application by tapping a button from my application. Is it possible?
You can do it if the application in question has a defined protocol, in example if you want to call a number via skype you can perform an action like
NSString *contactName = #"user123";
NSURL *skypeURL = [NSURL URLWithString:[NSString stringWithFormat:#"skype://%#?call", contactName]];
if ([[UIApplication sharedApplication] canOpenURL:skypeURL]) {
[[UIApplication sharedApplication] openURL:skypeURL];
} else {
// Display to the user how to install skype.
}
the same can happen for other programs which have a protocol definition that could be understood.
And just 4 the record, here's a list of know URL Schemes
http://wiki.akosma.com/IPhone_URL_Schemes
Then, if you want your app to be URL-Scheme Compatible, you can have a good start by reading this:
http://www.idev101.com/code/Objective-C/custom_url_schemes.html
and this other post is really good and detailed.
How do you set your Cocoa application as the default web browser?
it explains how to make your app handle a custom URLScheme, which turns out to be your case.
hope it could be helpful.
k
for other case scenarios I got no idea if it is possible.
No, it's not possible to do that, unless the apps in question support URL Scheme, then you can launch it using URL.
Not possible, because all iphone apps will run in a different individual process with their own runtime environment. There is only a limited IPC(Inter process communication) available, that too for system libs and resources.
No. There is no way to open an another app within app. However you can open an different app from your app vise versa by using
URL Scheema
Open_In functionality using CFBundleDocumentTypes and UIDocumentInteractionController.

App Store rejection due to programmatically adding application to all spaces

I have an app submitted to the app store that was rejected due to:
2.30 Apps that do not comply with the Mac OS X File System documentation will be rejected
They claim my app is modifying the ~/Library/Preferences/com.apple.spaces.plist file which is unsupported.
My app is in fact modifying that file, but only with NSUserDefaults via: (I'm omitting some code for brevity...)
NSMutableDictionary *spacesDefaults =
[[NSUserDefaults standardUserDefaults] persistentDomainForName:#"com.apple.spaces"];
NSMutableDictionary *dict = [spacesDefaults objectForKey:#"app-bindings"];
NSString *bundleId = [[[NSBundle mainBundle] bundleIdentifier] lowercaseString];
[dict setObject:#"AllSpaces" forKey:bundleId];
[spacesDefaults setObject:dict forKey:#"app-bindings"];
[[NSUserDefaults standardUserDefaults] setPersistentDomain:spacesDefaults
forName:#"com.apple.spaces"];
It seems to me that this falls under the first bullet of "File-System Usage Requirements for the Mac App Store" http://developer.apple.com/library/mac/#releasenotes/General/SubmittingToMacAppStore/_index.html#//apple_ref/doc/uid/TP40010572
* You may use Apple frameworks such as User Defaults, Calendar Store, and Address Book that implicitly write to files in specific locations, including locations is not allowed to access directly.
Does anyone know why this would get rejected? I just don't see it...
Thanks!
This is a really important issue that I bemoaned on the Apple Dev Forums...
And I suppose that filing a Feedback Request might have an effect, someday... But apparently this LAME restriction.. which limits an App's ability to do ANYTHING outside the sandbox.. EVEN IF it's something your user can do... This is a serious step backwards in the ability of third-party apps on the mac to perform SIMPLE, implicit commands by the user.
This seems like a fundamental paradigm shift on Apple's part. Here's the jist of the above forum listing..
NSUserDefaults can be used only for our own app,
and sandboxing will not allow to modify other apps defaults
Additionally, you cannot modify in any way ANY other app's .plist, via XML, or otherwise.
And, no, doing defaults write via a task or terminal on another domain, besides YOUR OWN APP is outlawed as well. Ugh, it's very annoying.

Can you access an iPhone's phone number via code in an app? [duplicate]

Is there any way to get own phone number by standard APIs from iPhone SDK?
At the risk of getting negative marks, I want to suggest that the highest ranking solution (currently the first response) violates the latest SDK Agreement as of Nov 5, 2009. Our application was just rejected for using it. Here's the response from Apple:
"For security reasons, iPhone OS restricts an application (including its preferences and data) to a unique location in the file system. This restriction is part of the security feature known as the application's "sandbox." The sandbox is a set of fine-grained controls limiting an application's access to files, preferences, network resources, hardware, and so on."
The device's phone number is not available within your application's container. You will need to revise your application to read only within your directory container and resubmit your binary to iTunes Connect in order for your application to be reconsidered for the App Store.
This was a real disappointment since we wanted to spare the user having to enter their own phone number.
No, there's no legal and reliable way to do this.
If you find a way, it will be disabled in the future, as it has happened with every method before.
Update: capability appears to have been removed by Apple on or around iOS 4
Just to expand on an earlier answer, something like this does it for me:
NSString *num = [[NSUserDefaults standardUserDefaults] stringForKey:#"SBFormattedPhoneNumber"];
Note: This retrieves the "Phone number" that was entered during the iPhone's iTunes activation and can be null or an incorrect value. It's NOT read from the SIM card.
At least that does in 2.1. There are a couple of other interesting keys in NSUserDefaults that may also not last. (This is in my app which uses a UIWebView)
WebKitJavaScriptCanOpenWindowsAutomatically
NSInterfaceStyle
TVOutStatus
WebKitDeveloperExtrasEnabledPreferenceKey
and so on.
Not sure what, if anything, the others do.
Using Private API you can get user phone number on the following way:
extern NSString* CTSettingCopyMyPhoneNumber();
+(NSString *) phoneNumber {
NSString *phone = CTSettingCopyMyPhoneNumber();
return phone;
}
Also include CoreTelephony.framework to your project.
You cannot use iOS APIs alone to capture the phone number (even in a private app with private APIs), as all known methods of doing this have been patched and blocked as of iOS 11. Even if a new exploit is found, Apple has made clear that they will reject any apps from the app store for using private APIs to do this. See #Dylan's answer for details.
However, there is a legal way to capture the phone number without any user data entry. This is similar to what Snapchat does, but easier, as it does not require the user to type in their own phone number.
The idea is to have the app programmatically send a SMS message to a server with the app’s unique installation code. The app can then query the same server to see if it has recently received a SMS message from a device with this unique app installation code. If it has, it can read the phone number that sent it. Here’s a demo video showing the process. As you can see, it works like a charm!
This is not super easy to set up, but it be configured in a few hours at no charge on a free AWS tier with the sample code provided in the tutorial here.
As you probably all ready know if you use the following line of code, your app will be rejected by Apple
NSString *num = [[NSUserDefaults standardUserDefaults] stringForKey:#"SBFormattedPhoneNumber"];
here is a reference
http://ayeapi.blogspot.com/2009/12/sbformatphonenumber-is-lie.html
you can use the following information instead
NSString *phoneName = [[UIDevice currentDevice] name];
NSString *phoneUniqueIdentifier = [[UIDevice currentDevice] uniqueIdentifier];
and so on
#property(nonatomic,readonly,retain) NSString *name; // e.g. "My iPhone"
#property(nonatomic,readonly,retain) NSString *model; // e.g. #"iPhone", #"iPod Touch"
#property(nonatomic,readonly,retain) NSString *localizedModel; // localized version of model
#property(nonatomic,readonly,retain) NSString *systemName; // e.g. #"iPhone OS"
#property(nonatomic,readonly,retain) NSString *systemVersion; // e.g. #"2.0"
#property(nonatomic,readonly) UIDeviceOrientation orientation; // return current device orientation
#property(nonatomic,readonly,retain) NSString *uniqueIdentifier; // a string unique to each device based on various hardware info.
Hope this helps!
To get you phone number you can read a plist file. It will not work on non-jailbroken iDevices:
NSString *commcenter = #"/private/var/wireless/Library/Preferences/com.apple.commcenter.plist";
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:commcenter];
NSString *PhoneNumber = [dict valueForKey:#"PhoneNumber"];
NSLog([NSString stringWithFormat:#"Phone number: %#",PhoneNumber]);
I don't know if Apple allow this but it works on iPhones.
No official API to do it. Using private API you can use following method:
-(NSString*) getMyNumber {
NSLog(#"Open CoreTelephony");
void *lib = dlopen("/Symbols/System/Library/Framework/CoreTelephony.framework/CoreTelephony",RTLD_LAZY);
NSLog(#"Get CTSettingCopyMyPhoneNumber from CoreTelephony");
NSString* (*pCTSettingCopyMyPhoneNumber)() = dlsym(lib, "CTSettingCopyMyPhoneNumber");
NSLog(#"Get CTSettingCopyMyPhoneNumber from CoreTelephony");
if (pCTSettingCopyMyPhoneNumber == nil) {
NSLog(#"pCTSettingCopyMyPhoneNumber is nil");
return nil;
}
NSString* ownPhoneNumber = pCTSettingCopyMyPhoneNumber();
dlclose(lib);
return ownPhoneNumber;
}
It works on iOS 6 without JB and special signing.
As mentioned creker on iOS 7 with JB you need to use entitlements to make it working.
How to do it with entitlements you can find here:
iOS 7: How to get own number via private API?
AppStore will reject it, as it's reaching outside of application container.
Apps should be self-contained in their bundles, and may not read or write data outside the designated container area
Section 2.5.2 :
https://developer.apple.com/app-store/review/guidelines/#software-requirements

How do you prompt the user to rate your iphone app without waiting for them to delete the app?

I am referring to the popup window that asks the user to submit a review/rating.
I know it can be done since the Aardark app does it...it asks several times in fact! (Almost too spammy.) But there has to be an API to trigger the rating request? Google is giving me no love on this one.
I would check out the Appirater project that Arash Payan has put together.
Pretty much handles the checking and displaying of the "rate my app" prompt, and brings the user right into the review portion of your app on the App Store.
It's a very clean, and elegant way to provide that minimum barrier so that your users are more likely to submit reviews of your app.
Hope this helps...
I have written about a way to open right into the review panel of the App Store.
http://www.memention.com/blog/2009/09/03/Open-Reviews.html
The actual code part if called through a button press could look like below
- (IBAction)gotoReviews:(id)sender
{
NSString *str = #"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa";
str = [NSString stringWithFormat:#"%#/wa/viewContentsUserReviews?", str];
str = [NSString stringWithFormat:#"%#type=Purple+Software&id=", str];
// Here is the app id from itunesconnect
str = [NSString stringWithFormat:#"%#289382458", str];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
I wrote a library to do this - similar to Appirater but a bit more configurable and with Mac App Store support:
https://github.com/nicklockwood/iRate
I have discovered the most subtle value of S.O. (already obvious to all elementary school teachers) --- often by simply forming your question, your neurons rearrange and you are more successful at answering it yourself! (Either that or Google optimized my search after spying on my keystrokes and indexing my question on S.O.)
Answer found after more productive googling:
http://www.mobileorchard.com/fighting-back-against-the-app-stores-negative-rating-bias/
this may not be what youre looking for EXACTLY but what i would do is just prompt them in the app somehow and somewhere not too annoying with a button or alert view and have that button link them to app url in safari which opens the appstore to your app. you can use the url http://www.itunes/apps/ (your app) as i believe is the synthax. i cant remember the exact code but i can get it for you if need it.
We AskingPoint have another way to do it (Im a founder) that provides BOTH App Analytics and the rating widget. You can adjust server side settings based on your analytics to prompt only your best users. Change settings on the fly. And it's translated into over 30 languages.
I wanted to do this when I roll the app out for the first time... looks like I'll have to wait and put it out as an update to ensure that it will work... let me know if I am wrong and there is some way I can test this before submitting to apple for distribution.