When I want to open App2 in App1, I can do like this:
NSURL * myURL_APP_A = [NSURL URLWithString:#"openurl1://"];
if ([[UIApplication sharedApplication] canOpenURL:myURL_APP_A]) {
[[UIApplication sharedApplication] openURL:myURL_APP_A];
}
But when I want to open an App in a keyboard Extension, [UIApplication sharedApplication] cannot be used.So how can I make it?
Thanks for your help!
You can try this method :
NSURL *url = [NSURL URLWithString:#"myapp://"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
[self.view addSubview:webView];
[webView loadRequest:urlRequest];
[UIApplication sharedApplication] openURL is currently not allowed in custom keyboards. You should see an error message popping up that tells you exactly that.
Related
What is the best way to implement SFSafariViewController using Objective-C. I currently have a UITableView and each of them leads to a different website. May I know how can I code each of them to load a different website in SFSafariViewController using Objective-C, not Swift? Thanks in advance!
This will work with newer versions like 10.
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:#"http://google.com"]];
if ([SFSafariViewController class] != nil) {
SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
[self presentViewController:sfvc animated:YES completion:nil];
} else {
[[UIApplication sharedApplication] openURL:URL];
}
Here's a version that checks the iOS version to handle iOS 8, and open links in safari if SFSafariViewController isn't compatible
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:#"http://google.com"]];
if ([[UIDevice currentDevice].systemVersion hasPrefix:#"9"]) {
SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:URL];
[self presentViewController:sfvc animated:YES completion:nil];
} else {
[[UIApplication sharedApplication] openURL:URL];
}
Then also call this to handle the done button
- (void)safariViewControllerDidFinish:(nonnull SFSafariViewController *)controller {
[controller dismissViewControllerAnimated:YES completion:nil];}
I'm trying to add a video in xcode but it crashes: [NSURL initFileURLWithPath:]: nil string
(IBAction)Play:(id)sender {
/////
NSString *videoUrl=[[NSBundle mainBundle]pathForResource:#"tnween" ofType:#"mov"];
MPMoviePlayerController *player=[[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:videoUrl]];
[player play];
}
Maybe if you try:
NSString *videoUrl=[[NSBundle mainBundle]pathForResource:#"tnween" ofType:#"mov"];
NSURL *url = [NSURL urlWithString:videoUrl];
MPMoviePlayerController *player=[[MPMoviePlayerController alloc]initWithContentURL:url];
I am new to iOS and trying to add sound to my app. I have followed several tutorials about adding a simple sound file to my project, but ALL with the same result. I get no errors, but no sound plays. Any ideas on what I am doing wrong? Any help would be appreciated!
- (IBAction)playSound:(id)sender {
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
NSLog(soundFilePath);
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
player.numberOfLoops = 1;
[player play];
}
**UPDATE***
So I never had any luck with the above code which is very discouraging, but I did end up getting it to work using the AudioToolbox Framework instead, using this:
SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle]
pathForResource:#"test" ofType:#"mp3"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)
[NSURL fileURLWithPath:soundFile], & soundID);
AudioServicesPlaySystemSound(soundID);
Can anyone explain to me the difference, why this one works and the other does not?? I am curious more than anything. Would love some clarification.
Finally figured it out. I needed to create a property for AVAudioPlayer in my header file in order to get it to work. Here is what I did:
// .h file
#interface ThirdViewController : UIViewController {
AVAudioPlayer *audioPlayer;
}
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
- (void) playSound //method in .m file
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"test"
ofType:#"mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[audioPlayer play];
}
Simplify your code by doing the follow. Also drop your test.mp3 to the root of your project directory in xcode. Your mp3 placement and location is very important. Also ensure volume is up, and vibrate is off of your device, and it's unmuted.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"test"
ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
[audioPlayer play];
Make sure you install quartz.h, or a specific framework that supports audio players. Just double check.
Had the same problem a while back and the framework must be installed properly.
I have just re-designed my iPhone app using Storyboard... questin is: how do I address the the UIWebView so I can load the html? (I am a newbie when it comes to Obj-C, having done my previous app in MonoTouch, so I'm kinda lost). :D
- (void)viewDidLoad {
NSString *urlAddress = #”http://www.stackoverflow.com”;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
Apple Developer docs are really good resource. The usage of UIWebView is outlined here enter link description here
To load a network resource simply pass a NSURLRequest object to loadRequest.
[self.myWebView loadRequest:[NSURLRequest
requestWithURL:[NSURL URLWithString:#"http://www.apple.com/"]]];
You can also do this in viewDidLoad (a UIWebView is a subclass of UIView and inherits all its methods). For example, loading a local pdf resource.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"iPhone_User_Guide" ofType:#"pdf"];
if (thePath) {
NSData *pdfData = [NSData dataWithContentsOfFile:thePath];
[(UIWebView *)self.view loadData:pdfData MIMEType:#"application/pdf"
textEncodingName:#"utf-8" baseURL:nil];
}
}
To setup your UIWebView with a link, just use the following code.
var URLPath = "Insert email address here"
Then use...
loadAddressURL()
and finally...
func loadAddressURL(){
let requestURL = NSURL(string:URLPath)
let request = NSURLRequest(URL: requestURL!)
Webview.loadRequest(request)
}
I have a view controller that has a UIWebView object within it.
I would like to load and display a PDF from the internet within the UIWebView.
I have the code working to display the a PDF loaded from the resource bundle, but i am lost on having the PDF loaded from the internet.
I was thinking it would be as simple as changing the fileURLWithPath to point to the location on the internet... although the form displays, it does not show the contents. So I am assuming that I have the wrong URL in the string???
Does anyone have a code snippet to load and display a PDF from the internet within a UIWebView that they would be willing to share???
thanks
tony
I take it you are looking at iOS stuff, not Mac. On iOS, it should really be just as simple as
NSURL* url = [NSURL URLWithString:stringURL];
[aWebView loadRequest:[NSURLRequest requestWithURL:url]];
Use the following for xCode 5.
Interface section - myWebView is the outlet to the UIWebView in my storyboard.
#property (weak, nonatomic) IBOutlet UIWebView *myWebView;
#property NSURL *url;
The rest goes into viewDidLoad section of the ViewController class.
- (void)viewDidLoad
{
[super viewDidLoad];
_url = [[NSURL alloc] init];
_url = [NSURL URLWithString:#"http://deimos3.apple.com/WebObjects/Core.woa/FeedEnclosure/utah.edu.1668842900.01668842919.1681195338/enclosure.pdf"];
[_myWebView loadRequest:[NSURLRequest requestWithURL: _url]];
}
This worked for me but there maybe a more efficient way of doing this.
Here is how i implemented it....
visitNoteViewController *controller = [[visitNoteViewController alloc] initWithNibName:#"visitNoteView" bundle:nil];
NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
controller.pdfUrl = [NSURL URLWithString:#"http://deimos3.apple.com/WebObjects/Core.woa/FeedEnclosure/utah.edu.1668842900.01668842919.1681195338/enclosure.pdf"];
[self presentModalViewController:controller animated:YES];
[controller release];
got it working.