4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

cocos2d-x AndroidとiOSでブラウザを起動する方法

Last updated at Posted at 2014-11-14

この記事を自分向けにしたものです。

新しいファイルを作らなくてよいので楽かなと…

対象

  • cocos2d-x 3.2.0
  • Xcode 6.1
  • Eclipse Juno (Android Developer Tools)

iOS対応

cocos2d_libs.xcodeprojの次のファイルを編集します。

platform/ios/CCApplication

// CCApplication.h

class CC_DLL Application : public ApplicationProtocol {

public:
  void openURL(const char* url);

};
// CCApplication.mm
void Application::openURL(const char *urlStr) {
    NSString* str = [NSString stringWithUTF8String:urlStr];
    NSURL* url = [NSURL URLWithString:str];
    [[UIApplication sharedApplication] openURL:url];
}

Android対応

cocos2d/cocos/platform/androidの以下のファイルとsrcのjavaファイルを編集します。

// CCApplication.h

class CC_DLL Application : public ApplicationProtocol {
  virtual void openURL(const char* pszUrl);
};

// CCApplication.cpp

void Application::openURL(const char* pszUrl) {
    JniMethodInfo minfo;
    if(JniHelper::getStaticMethodInfo(minfo,
        "org/cocos2dx/cpp/AppActivity",
        "openURL",
        "(Ljava/lang/String;)V"))
    {
        jstring StringArg1 = minfo.env->NewStringUTF(pszUrl);
        minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID, StringArg1);
        minfo.env->DeleteLocalRef(StringArg1);
        minfo.env->DeleteLocalRef(minfo.classID);
    }
}
// /src org.cocos2dx.cpp.AppActivity.java

public class AppActivity extends Cocos2dxActivity {

private static AppActivity me = null;

    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        me = this;   // => added!
    }
    
    public static void openURL(String url) { 
        Intent i = new Intent(Intent.ACTION_VIEW);  
        i.setData(Uri.parse(url));
        me.startActivity(i);
    }
}

利用例

// SampleScene.cpp

void SampleScene::tapped(Ref* pSender) {
  Application::getInstance()->openURL("http://www.abc.def/~~~");
}
4
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?