How to display NSString content in WebView (Cocoa Mac app) - objective-c

Is it possible to load a WebView with the text from a NSString? If I save the string content into a file and then use something like:
[[main_browser mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"file:///Volumes/AA/myfile.txt"]]];
It works, but how can I do it without writing the content into a file?

Yeah you can load a NSString into a webview, just look at the following method:
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
You can also open files from the disk this way by creating a NSString object and then loading it into the webview. However using loadRequest might be better ( as it is doing the same thing, but then internally ).
If you want to write a NSString to a file you can:
[someStringObject writeToFile:_path_];
Did you actually take a look at the NSString class reference? It is pretty clear there.

Related

How do you set what about:home is on webView, Obj-C

I am creating a Cocoa web browser, and I noticed that if the webview loads a nil location, it just loads about:home. Since I have not set it, the page just appears white. Is there a way I can change what about:home looks like. Even if it is a simple .rtf file or something.
I looked around, but don't see any way to do this. Am I suppose to create a NSURL and set it to whatever file?
Thanks. Oh, and if code is ever needed, I would be glad to add it.
Try something like this:
// Inside your App Delegate
-(void)applicationDidFinishLaunching:(NSNotification *)notification {
// Assuming WebView is called myWebView
NSString *currentURL = [myWebView mainFrameURL];
if(!currentURL) {
NSString *homeResource = [[NSBundle mainBundle] pathForResource:#"home" ofType:#"html" inDirectory:#"default"];
NSURL *homeURL = [NSURL fileURLWithPath:homeResource];
NSURLRequest *request = [NSURLRequest requestWithURL:homeURL];
[myWebView loadRequest:request];
}
}
You'll need to have a pre-made file called home.html within a folder called default located in the Resources section of your project.
I suppose this isn't exactly replacing about:home, but you can always check for about:home and handle that appropriately as well.

Drag Files come across Sandbox(__CFPasteboardIssueSandboxExtensionForPath)

I processed drag operation from browser view to custom view.It work well in snow lepoard,but not in Mountain Lion with sandbox.
in browser view:
NSMutableArray* urls = [[[NSMutableArray alloc] init] autorelease];
..............put some NSUrl to urls array....................
[pasteboard writeObjects:[NSArray arrayWithArray:urls]];
in my receive custom view:
NSArray* pasteboardItems = [pasteboard readObjectsForClasses:[NSArray arrayWithObject:[NSString class]] options:nil];
NSArray* pasteboardItems2 = [pasteboard readObjectsForClasses:[NSArray arrayWithObject:[NSURL class]] options:nil];
NSArray* pasteboardItems3 = [pasteboard readObjectsForClasses:[NSArray arrayWithObject:[NSImage class]] options:nil];
NSLog(#"%#",pasteboardItems);
NSLog(#"%#",pasteboardItems2);
NSLog(#"%#",pasteboardItems3);
my log is:
2012-08-09 18:33:43.886 iCollage[6885:303] __CFPasteboardIssueSandboxExtensionForPath: error for [/Users/xxxx/Library/Containers/xxxxxxxxxxxx/Data/Downloads/1343902069.jpg]
2012-08-09 18:33:44.546 iCollage[6885:303] ( "file://localhost/Users/xxx/Library/Containers/xxxxxxxx/Data/Downloads/1343902069.jpg")
2012-08-09 18:33:44.547 iCollage[6885:303] ( "file://localhost/Users/xxxxx/Library/Containers/xxxxxx/Data/Downloads/1343902069.jpg")
2012-08-09 18:33:44.547 iCollage[6885:303] ()
my question is:
1.how to fix this error __CFPasteboardIssueSandboxExtensionForPath;I refer the docs and found nothing about that.I am ensuer that i have the permission to access the file!google says, may be "startAccessingSecurityScopedResource" will help me, then i try and failed
2.why pasteboardItems2 have value?i write to pasteboard only url but not string.It disgusted me that I can get the url both from NSString type and NSUrl type! (I try drag a file from iFinder, the url will only exist in pasteboardItems but not pasteboardItems2).Anybody know why? I think the first problem will auto fixed when some one help me fix this problem.
I believe Apple answer question 1:
Important: Although you can support dragging file paths, in general,
you should avoid doing so unless you are certain that the destination
app will never be run in an app sandbox. If you use an NSString, OS X
has no way to know whether that string should be interpreted as a
path; thus, OS X does not expand the destination app’s sandbox to
allow access to the file or directory at that location. Instead, use
an NSURL, a bookmark, or a filename pasteboard type.
WRT to question 2, it looks like you have pasted URLs so reading NSURL objects would seem to be correct. However I think you should implement the dragging using the following code (also from the link above):
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
NSPasteboard *pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
int numberOfFiles = [files count];
// Perform operation using the list of files
}
return YES;
}
You need to generate security-scoped URL bookmark data on the sender side, and turn that data back into a URL on the receiver side. There's some other stuff you have to do after that when you want to actually access the URL; the documentation elaborates.
The receiving application, when running in a sandbox, will not be able to handle bare paths. This is a core part of being sandboxed; you are not allowed to use bare paths or their corresponding URLs to access files that aren't in your sandbox container and haven't been explicitly handed to you by the user.
Your pasteboardItems read object of NSString type, but you dragged a file(with jpg extension), you should register for NSString type in your init method:
[self registerForDraggedTypes:[NSArray arrayWithObject:NSPasteboardTypeString]];
You need to have Document Types defined in your application so that the sandboxing mechanism knows your application should be opening files with those extensions. You can do this by clicking the project on the left in Xcode, and in the Info tab, under Document Types add a new document type for each extension.
You just need to fill in the name and extensions field.
Also if you want to persist your permission to access the files dragged onto your application, you can use this class to wrap up all that logic. https://github.com/leighmcculloch/AppSandboxFileAccess

Code=102 "Frame load interrupted" when I load a NSstring as a html Iphone

I am developing an app for iphone/ipad that I have some problems with.
I am building dynamically a nsmutablestring with html code and loading as html to a uiwebview. I also load a local js file to this html page where I have some logical functions.
In this html I got a form with a submit button at the end.
What I am trying to do is that at the moment the submit button is click, it will call a method that return a string back to the objective-c code. I have no problem with the javascript controlling the form. But when the submit button is click, the native code gives me
Error Domain=WebKitErrorDomain Code=102 "Frame load interrupted" UserInfo=0x6c60b20 {NSErrorFailingURLKey=file://
I have tried to redirect in js-function like window.location="myapp:custom:"+string; But I got the same problem. Even trying libraries like JSBridge api.
I suspect that is because I am loading a NSString to the webview that has the baseUrl the mainbundle path for loading the local js-file.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[self.webView loadHTMLString:string baseURL:baseURL];
I only had to change the input submit with a input button in the html code and works. So now I got the string from javascript back to objective-c.

Set Image on Application Startup

I'm currently developing an Obj-C Desktop application which gets a string from an URL and displays an image regarding to the recieved string.
//I know this one won't work, but it's just for example
NSString * text = [NSString stringFromUrl:#"http://example.com"];
NSString * imageName = [text splitStringIntoSome];
//imageName is now #"A.png"
[imageViewOutlet setImage:[NSImage imageNamed:imageName];
So, the problem is, I want to do that when the application launches, but when I copy this code into my applicationDidFinishLaunching, just nothing happens...
Unfortunately since URL requests are performed asynchronously, it requires a bit more code to do this. Here is a good example straight from Apple:
Using NSURLConnection
Except in - (void)connectionDidFinishLoading:(NSURLConnection *)connection you would create an image using NSImage's - (id)initWithData:(NSData *)data

Parse XML and generate UI in UIWebView iPad App

I will receive an XML from Server, and have to parse the xml file, and based on that have to populate textbox, listbox etc(a form) in UIWebView.
I know XML parsing for iOS using NSXML, I do not know how to fire events to generate Form/UI at runtime.
Any tutorials or idea pls.
If you have created your HTML as a NSString already, you can load it into the UIWebView by using the method loadHTMLString:baseURL:. For example:
NSString *htmlString = [[NSString alloc] init];
//Download XML, parse it and turn it into HTML.
[myWebView loadHTMLString:htmlString baseURL:nil];
//if you have to do any thing else to the HTML string do it here
[htmlString release];
Changing the htmlString after calling loadHTMLString:baseURL: won't update the UIWebView. You will need to call the method again, sending the modified string.
If you need further help, comment below and I'll edit my answer or comment back.