Why won't this WebView load an external URL with string? - objective-c

I'm using this project to get me started with the WebKit framework under OSX.
It loads resources locally, and I've been able to get it to work. However, when I alter awakeFromNib to load from a URL string like http://www.example.com/index.html It doesn't work, the view is blank, even though this is a valid URL.
Original:
- (void)awakeFromNib {
WebPreferences *prefs = [webView preferences];
[prefs _setLocalStorageDatabasePath:#"~/Library/WebViewExample/LocalStorage"];
NSString *resourcesPath = [[NSBundle mainBundle] resourcePath];
NSString *htmlPath = [resourcesPath stringByAppendingString:#"/htdocs/640x480_naked.html"];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];
}
Modified for external URL:
- (void)awakeFromNib {
WebPreferences *prefs = [webView preferences];
[prefs _setLocalStorageDatabasePath:#"/tmp"];
NSString *urlText = #"http://www.example.com/index.html";
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
}
What could be wrong?

Thanks to the comment and link from #danh. This solved it for me.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>example.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
</dict>
</dict>

Related

How to send multiple images Amazon S3 Server ios objective-c

I am trying to upload images to Amazon S3 using this logic:
- (void)uploadImageToS3: (UIImage *)image {
imageData = UIImageJPEGRepresentation(image, 0.7);
AWSS3GetPreSignedURLRequest *getPreSignedURLRequest = [AWSS3GetPreSignedURLRequest new];
getPreSignedURLRequest.bucket = #"dummyimages";
getPreSignedURLRequest.key = #"test.jpg";
getPreSignedURLRequest.HTTPMethod = AWSHTTPMethodPOST;
getPreSignedURLRequest.expires = [NSDate dateWithTimeIntervalSinceNow:3600];
NSString *fileContentTypeString = #"text/plain";
getPreSignedURLRequest.contentType = fileContentTypeString;
[[[AWSS3PreSignedURLBuilder defaultS3PreSignedURLBuilder] getPreSignedURL:getPreSignedURLRequest] continueWithBlock:^id(AWSTask *task) {
if (task.error) {
NSLog(#"Error: %#", task.error);
} else {
NSURL *presignedURL = task.result;
NSLog(#"upload presignedURL is \n%#", presignedURL);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:presignedURL];
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
[request setHTTPMethod:#"PUT"];
[request setValue:fileContentTypeString forHTTPHeaderField:#"Content-Type"];
NSURLSessionUploadTask *uploadTask = [[NSURLSession sharedSession] uploadTaskWithRequest:request fromData:imageData completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(#"Upload errer: %#", error);
}
NSLog(#"Done");
}];
[uploadTask resume];
}
return nil;
}];
}
I am getting an error like this:
'NSInternalInconsistencyException', reason: 'The service configuration is nil. You need to configure Info.plist or set defaultServiceConfiguration before using this method.
could please help this issue
Thanks
Have a look at http://docs.aws.amazon.com/mobile/sdkforios/developerguide/setup.html , in the section Preparing iOS 9 Apps.
You need to configure your service configuration in the class that is responsible for managing the Amazon Identity.
If you are using the newer version of MobileHub then the AWS constants
are configured in your info.plist and you need to check this
If you are using an older version MobileHub then the constants file is AWSConfiguration.
The class responsible for managing identity is AWSIdentityManager where the function that configures the service is
- (AWSTask *)initializeClients:(NSDictionary *)logins
If you are not using MobileHub you need to implement something similar in your own identity manager.
Add below code line for info.Plist file
<key>NSExceptionDomains</key>
<dict>
<key>amazonaws.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
<key>amazonaws.com.cn</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
</dict>

NSUserDefaults does not pick up setting

I am quite new to coding in Objective C and using the settings bundle but here is what I coded.
// Set the application defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:#"YES"
forKey:#"gameSave"];
[defaults registerDefaults:appDefaults];
[defaults synchronize];
// Get user preference
gameSave = [defaults boolForKey:#"saveGame"];
NSLog(#"Save Game = %#", gameSave ? #"YES" : #"NO");
This is my settings bundle:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Title</key>
<string>XXXXXXXXXXXXX</string>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>FooterText</key>
<string>www.xxxxxxxxxxxx.com</string>
</dict>
<dict>
<key>DefaultValue</key>
<true/>
<key>Key</key>
<string>gameSave</string>
<key>Title</key>
<string>Save Game</string>
<key>Type</key>
<string>PSToggleSwitchSpecifier</string>
</dict>
</array>
<key>StringsTable</key>
<string>Root</string>
</dict>
</plist>
When tested the gameSave value is NO or false. Can someone point me to a solution? Thanks.
#"YES" is a NSString, not a NSNumber containing a boolean.
You want #YES instead:
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:#YES
forKey:#"gameSave"];
Or the same, shorter:
NSDictionary *appDefaults = #{#"gameSave": #YES};
Edit: what if you do... this?
// Set the application defaults -- only if not written yet
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"gameSave"] == nil)
{
[defaults setBool:YES forKey:#"gameSave"];
[defaults synchronize];
}
// Get user preference
gameSave = [defaults boolForKey:#"gameSave"];
NSLog(#"Save Game = %#", gameSave ? #"YES" : #"NO");
This should be straight forward:
To save the bool:
// Write the value to NSUserDefault
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:#"gameIsSaved"];
[defaults synchronize];
To retrieve the boolean value
//Read the value from NSUserDefault
bool gameIsSaved = [defaults boolForKey:#"gameIsSaved"];
NSLog(#"gameIsSaved = %#", gameIsSaved ? #"YES" : #"NO");

Service launches my application again and hangs the requesting application

I'm trying to make my program provide a service to look up words. It works when I drag some text onto the icon but when I try to use the menu item it opens another instance of the application, freezes the application I sent the request from for about 30 seconds and then doesn't execute the method it was supposed to.
Here is my method:
- (void)serviceQuery:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error {
// Test for strings on the pasteboard.
NSArray *classes = [NSArray arrayWithObject:[NSString class]];
NSDictionary *options = [NSDictionary dictionary];
if (![pboard canReadObjectForClasses:classes options:options]) {
*error = NSLocalizedString(#"Error: couldn't encrypt text.",
#"pboard couldn't give string.");
return;
}
NSString *baseURL = #"http://labs.valtyrorn.com/bin/leit.php?q=";
NSString *q = [pboard stringForType:NSPasteboardTypeString];
NSString * encoded = [q stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *query = [NSString stringWithFormat:#"%#%#", baseURL, encoded];
[webView setMainFrameURL:query];
NSLog(query);
}
and the service part of the plist looks like this:
<key>NSServices</key>
<array>
<dict>
<key>NSSendTypes</key>
<array>
<string>NSStringPboardType</string>
</array>
<key>NSMessage</key>
<string>serviceQuery</string>
<key>NSMenuItem</key>
<dict>
<key>default</key>
<string>Fáðu beygingarlýsingu</string>
</dict>
<key>NSPortName</key>
<string>BIN</string>
<key>NSRequiredContext</key>
<dict></dict>
</dict>
</array>
Does anyone know how to fix this?

NSArray initWithContentsOfFile returns nil

I'm trying to read a plist file into NSArray using initWithContentsOfFile as described here
My code looks like this
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:
#"routes" ofType:#"plist" ];
routes = [[NSArray alloc] initWithContentsOfFile:plistPath ];
NSLog:(#"contents of myArray %#", routes);
routes.plist has a perfectly simple structure of array and 2 string values inside
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Root</key>
<array>
<string>1</string>
<string>2</string>
</array>
</dict>
</plist>
Everything seems to be fine. plistPath initializes with correct value, which means file is copied to the bundle and can be readed. But routes = [[NSArray alloc] initWithContentsOfFile:plistPath ]; returns 0x0 value. I think this is equal to nil
What is wrong with my code?
Actually your plist is a dictionary.
Change this:
<dict>
<key>Root</key>
<array>
<string>1</string>
<string>2</string>
</array>
</dict>
to this:
<array>
<string>1</string>
<string>2</string>
</array>
I think you want to do something like this:
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:
#"routes" ofType:#"plist"];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSArray *routes = [dictionary objectForKey:#"Root"];

Create <dict> in array with keys in .plist (Cocoa/iPhone SDK)

Hey guys, trying to add (object?) to a my plist programmatically, heres what I've cooked out so far:
NSString *documentsDirectory = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents/myfolder"]];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:#"Favourites.plist"];
NSMutableDictionary *rootDict = [[NSMutableDictionary alloc] initWithContentsOfFile:writablePath];
[rootDict setValue:#"My Third PDF" forKey:#"Title"];
[rootDict writeToFile:writablePath atomically: YES];
and heres my plist:
<plist version="1.0">
<dict>
<key>Rows</key>
<array>
<dict>
<key>SaveName</key>
<string>first.pdf</string>
<key>Title</key>
<string>My first PDF</string>
</dict>
<dict>
<key>SaveName</key>
<string>second.pdf</string>
<key>Title</key>
<string>My Second PDF</string>
</dict>
</array>
</dict>
</plist>
How do I go about adding another (object?) like
<dict>
<key>SaveName</key>
<string>third.pdf</string>
<key>Title</key>
<string>My third PDF</string>
</dict>
to my plist? Thanks!
You would fist have to make sure the Rows array is mutable:
[rootDict setObject:[[rootDict objectForKey:#"Rows"] mutableCopy] forKey:#"Rows"];
You would need to create a dict with that structure, e.g.
NSDictionary *third = [NDSictionary dictionaryWithObjectsAndKeys:
#"third.pdf", #"SaveName",
#"My third PDF", #"Title",
nil];
And add it to the array:
[[rootDict objectforKey:#"Rows"] addObject:third];